cleanup in Info.hs
This commit is contained in:
parent
5c3ca348c2
commit
04b7872a17
1 changed files with 30 additions and 26 deletions
|
@ -206,7 +206,7 @@ instance FromYAML Info where
|
||||||
parseYAML = YAML.withMap "info" \m ->
|
parseYAML = YAML.withMap "info" \m ->
|
||||||
Info <$> m .: "date"
|
Info <$> m .: "date"
|
||||||
<*> m .:? "sort" .!= ""
|
<*> m .:? "sort" .!= ""
|
||||||
<*> (maybe [] getUL <$> m .:? "updates")
|
<*> (m .:? "updates" >>= updateList)
|
||||||
<*> m .:? "show-updated" .!= True
|
<*> m .:? "show-updated" .!= True
|
||||||
<*> m .: "title"
|
<*> m .: "title"
|
||||||
<*> m .:? "artist"
|
<*> m .:? "artist"
|
||||||
|
@ -239,30 +239,31 @@ imageList y = pure <$> unlabelledImage y <|> parseYAML y
|
||||||
|
|
||||||
instance FromYAML Image where
|
instance FromYAML Image where
|
||||||
parseYAML y = unlabelledImage y <|> labelled y where
|
parseYAML y = unlabelledImage y <|> labelled y where
|
||||||
labelled y' = do
|
labelled = withPairM \label -> unlabelledImage' (Just label)
|
||||||
Pair label rest <- parseYAML y'
|
|
||||||
i <- unlabelledImage rest
|
|
||||||
pure $ i {label}
|
|
||||||
|
|
||||||
unlabelledImage :: YAML.Node YAML.Pos -> YAML.Parser Image
|
unlabelledImage :: YAML.Node YAML.Pos -> YAML.Parser Image
|
||||||
unlabelledImage y = asStr y <|> asObj y
|
unlabelledImage = unlabelledImage' Nothing
|
||||||
|
|
||||||
|
unlabelledImage' :: Maybe Text -> YAML.Node YAML.Pos -> YAML.Parser Image
|
||||||
|
unlabelledImage' label' y = asStr y <|> asObj y
|
||||||
where
|
where
|
||||||
asStr = YAML.withStr "path" \(Text.unpack -> path) ->
|
asStr = YAML.withStr "path" \(Text.unpack -> path) ->
|
||||||
pure $ Image {label = pathToLabel path, path, download = Nothing,
|
let label = fromMaybe (pathToLabel path) label' in
|
||||||
|
pure $ Image {label, path, download = Nothing,
|
||||||
nsfw = False, warning = Nothing}
|
nsfw = False, warning = Nothing}
|
||||||
asObj = YAML.withMap "image info" \m -> do
|
asObj = YAML.withMap "image info" \m -> do
|
||||||
path <- m .: "path"
|
path <- m .: "path"
|
||||||
download <- m .:? "download"
|
download <- m .:? "download"
|
||||||
nsfw <- m .:? "nsfw" .!= False
|
nsfw <- m .:? "nsfw" .!= False
|
||||||
warning <- m .:? "warning"
|
warning <- m .:? "warning"
|
||||||
pure $ Image {label = pathToLabel path, path, download, nsfw, warning}
|
let label = fromMaybe (pathToLabel path) label'
|
||||||
|
pure $ Image {label, path, download, nsfw, warning}
|
||||||
pathToLabel = Text.pack . dashToSpace . takeBaseName
|
pathToLabel = Text.pack . dashToSpace . takeBaseName
|
||||||
dashToSpace = map \case '-' -> ' '; c -> c
|
dashToSpace = map \case '-' -> ' '; c -> c
|
||||||
|
|
||||||
instance FromYAML Link where
|
instance FromYAML Link where
|
||||||
parseYAML y = do
|
parseYAML =
|
||||||
Pair title rest <- parseYAML y
|
withPairM \title rest -> asStr title rest <|> asObj title rest
|
||||||
asStr title rest <|> asObj title rest
|
|
||||||
where
|
where
|
||||||
asStr title = YAML.withStr "url" \url ->
|
asStr title = YAML.withStr "url" \url ->
|
||||||
pure $ Link {title, url, nsfw = False}
|
pure $ Link {title, url, nsfw = False}
|
||||||
|
@ -271,12 +272,10 @@ instance FromYAML Link where
|
||||||
nsfw <- m .:? "nsfw" .!= False
|
nsfw <- m .:? "nsfw" .!= False
|
||||||
pure $ Link {title, url, nsfw}
|
pure $ Link {title, url, nsfw}
|
||||||
|
|
||||||
newtype UpdateList = UL {getUL :: [Update]}
|
|
||||||
|
|
||||||
instance FromYAML UpdateList where
|
updateList :: Maybe (YAML.Node YAML.Pos) -> YAML.Parser [Update]
|
||||||
parseYAML y = do
|
updateList =
|
||||||
pairs <- YAML.withMap "updates" (pure . Map.toList) y
|
maybe (pure []) $ YAML.withMap "updates" $ traverse asEither . Map.toList
|
||||||
UL <$> traverse asEither pairs
|
|
||||||
where
|
where
|
||||||
asEither (date', rest) = do
|
asEither (date', rest) = do
|
||||||
date <- parseYAML date'
|
date <- parseYAML date'
|
||||||
|
@ -399,6 +398,11 @@ instance (FromYAML a, FromYAML b) => FromYAML (Pair a b) where
|
||||||
[(a, b)] -> Pair <$> parseYAML a <*> parseYAML b
|
[(a, b)] -> Pair <$> parseYAML a <*> parseYAML b
|
||||||
_ -> fail "expected exactly one pair"
|
_ -> fail "expected exactly one pair"
|
||||||
|
|
||||||
|
withPairM :: (FromYAML a, FromYAML b)
|
||||||
|
=> (a -> b -> YAML.Parser c)
|
||||||
|
-> (YAML.Node YAML.Pos -> YAML.Parser c)
|
||||||
|
withPairM k y = parseYAML y >>= \(Pair a b) -> k a b
|
||||||
|
|
||||||
withPair :: (FromYAML a, FromYAML b)
|
withPair :: (FromYAML a, FromYAML b)
|
||||||
=> (a -> b -> c)
|
=> (a -> b -> c)
|
||||||
-> (YAML.Node YAML.Pos -> YAML.Parser c)
|
-> (YAML.Node YAML.Pos -> YAML.Parser c)
|
||||||
|
|
Loading…
Reference in a new issue