From 86f05803c7bbe67a6b8d1c8ee91ef865f37e73a0 Mon Sep 17 00:00:00 2001 From: Rhiannon Morris Date: Fri, 3 Dec 2021 11:16:05 +0100 Subject: [PATCH] check for unused (maybe misspelt) keys --- make-pages/Info.hs | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/make-pages/Info.hs b/make-pages/Info.hs index b495ca7..dfd5136 100644 --- a/make-pages/Info.hs +++ b/make-pages/Info.hs @@ -23,6 +23,8 @@ import Data.Hashable (Hashable) import Data.HashSet (HashSet) import qualified Data.HashSet as HashSet import qualified Data.Map.Strict as Map +import Data.Set (Set, (\\)) +import qualified Data.Set as Set import Data.Maybe (isJust, isNothing, fromMaybe, mapMaybe) import Data.List (nub, sortBy) import Data.Ord (comparing) @@ -241,8 +243,22 @@ addSuffix suf path = pre ++ suf ++ ext +getKeys :: YAML.Mapping YAML.Pos -> YAML.Parser (Set Text) +getKeys = fmap Set.fromList . traverse (YAML.withStr "key" pure) . Map.keys + +checkKeys :: YAML.Mapping YAML.Pos -> Set Text -> YAML.Parser () +checkKeys mapping wanted = do + keys <- getKeys mapping + let unused = Set.toList $ keys \\ wanted + unless (null unused) do + fail $ "unused keys: " <> show unused + + instance FromYAML Info where - parseYAML = YAML.withMap "info" \m -> + parseYAML = YAML.withMap "info" \m -> do + checkKeys m ["date", "sort", "updates", "show-updated", "unlisted", "title", + "artist", "nsfw-only", "tags", "nsfw-tags", "desc", + "nsfw-desc", "bg", "images", "thumb", "links", "extras"] Info <$> m .: "date" <*> m .:? "sort" .!= "" <*> (m .:? "updates" >>= updateList) @@ -264,7 +280,8 @@ instance FromYAML Info where instance FromYAML Artist where parseYAML y = justName y <|> withUrl y where justName = YAML.withStr "name" \name -> pure $ Artist {name, url = Nothing} - withUrl = YAML.withMap "full info" \m -> + withUrl = YAML.withMap "full info" \m -> do + checkKeys m ["name", "url"] Artist <$> m .: "name" <*> m .:? "url" instance FromYAML Desc where @@ -292,6 +309,7 @@ unlabelledImage' label' y = asStr y <|> asObj y pure $ Image {label, path, download = Nothing, nsfw = False, warning = Nothing} asObj = YAML.withMap "image info" \m -> do + checkKeys m ["path", "download", "nsfw", "warning"] path <- m .: "path" download <- m .:? "download" nsfw <- m .:? "nsfw" .!= False @@ -313,6 +331,7 @@ instance FromYAML Link where asStr title = YAML.withStr "url" \url -> pure $ Link {title, url, nsfw = False} asObj title = YAML.withMap "link info" \m -> do + checkKeys m ["url", "nsfw"] url <- m .: "url" nsfw <- m .:? "nsfw" .!= False pure $ Link {title, url, nsfw} @@ -328,6 +347,7 @@ updateList = asDesc date = YAML.withStr "desc" \desc -> pure $ Update {date, desc, nsfw = False, ignoreSort = False} asObj date = YAML.withMap "update info" \m -> do + checkKeys m ["desc", "nsfw", "ignore-sort"] desc <- m .: "desc" nsfw <- m .:? "nsfw" .!= False ignoreSort <- m .:? "ignore-sort" .!= False @@ -399,7 +419,8 @@ matchFilters (GalleryFilters {nsfw, artist, require, exclude}) i = instance FromYAML GalleryInfo where - parseYAML = YAML.withMap "gallery info" \m -> + parseYAML = YAML.withMap "gallery info" \m -> do + checkKeys m ["title", "desc", "prefix", "filters", "hidden"] GalleryInfo <$> m .: "title" <*> m .: "desc" <*> m .: "prefix" @@ -407,7 +428,8 @@ instance FromYAML GalleryInfo where <*> m .:? "hidden" .!= mempty instance FromYAML GalleryFilters where - parseYAML = YAML.withMap "gallery filters" \m -> + parseYAML = YAML.withMap "gallery filters" \m -> do + checkKeys m ["nsfw", "artist", "require", "exclude"] GalleryFilters <$> m .:? "nsfw" .!= AllN <*> m .:? "artist" .!= AllA <*> m .:? "require" .!= [] @@ -428,7 +450,8 @@ data IndexInfo = deriving Show instance FromYAML IndexInfo where - parseYAML = YAML.withMap "index info" \m -> + parseYAML = YAML.withMap "index info" \m -> do + checkKeys m ["title", "desc", "galleries", "links", "footer"] IndexInfo <$> m .: "title" <*> m .: "desc" <*> m .:? "galleries" .!= []