a lot of stuff sorry

This commit is contained in:
Rhiannon Morris 2020-07-16 16:07:28 +02:00
parent adfc8b9a82
commit 375c6e833a
9 changed files with 297 additions and 151 deletions

View file

@ -1,8 +1,8 @@
{-# OPTIONS_GHC -Wno-orphans #-}
module Info
(Info (..), Artist (..), Image (..), Link (..),
GalleryInfo (..), GalleryFilters, Who (..),
readWho, matchWho, matchNsfw, matchFilters,
GalleryInfo (..), GalleryFilters (..), Whose (..),
readWhose, matchWhose, matchNsfw, matchFilters,
-- ** Reexports
Day (..), Text)
where
@ -103,59 +103,57 @@ instance FromYAML Link where
data GalleryInfo =
GalleryInfo {
title :: !Text,
prefix :: !FilePath,
name :: !Text,
filters :: !GalleryFilters,
ordering :: !Int -- sorted by @ordering@ on gallery list page
filters :: !GalleryFilters
}
deriving (Eq, Show)
data GalleryFilters =
GalleryFilters {
nsfw :: Maybe Bool,
who :: Who
nsfw :: Maybe Bool,
whose :: Whose
}
deriving (Eq, Show)
data Who = Mine | NotMine | All deriving (Eq, Show)
data Whose = Mine | NotMine | All deriving (Eq, Show)
matchNsfw :: Maybe Bool -> Info -> Bool
matchNsfw Nothing _ = True
matchNsfw (Just nsfw) i = #allNsfw i == nsfw
readWho :: (IsString str, Eq str, Alternative f) => str -> f Who
readWho "mine" = pure Mine
readWho "not-mine" = pure NotMine
readWho "all" = pure All
readWho _ = empty
readWhose :: (IsString str, Eq str, Alternative f) => str -> f Whose
readWhose "mine" = pure Mine
readWhose "not-mine" = pure NotMine
readWhose "all" = pure All
readWhose _ = empty
matchWho :: Who -> Info -> Bool
matchWho Mine = #mine
matchWho NotMine = #notMine
matchWho All = const True
matchWhose :: Whose -> Info -> Bool
matchWhose Mine = #mine
matchWhose NotMine = #notMine
matchWhose All = const True
noFilters :: GalleryFilters
noFilters = GalleryFilters {nsfw = Nothing, who = All}
noFilters = GalleryFilters {nsfw = Nothing, whose = All}
matchFilters :: GalleryFilters -> Info -> Bool
matchFilters (GalleryFilters {nsfw, who}) i =
matchNsfw nsfw i && matchWho who i
matchFilters (GalleryFilters {nsfw, whose}) i =
matchNsfw nsfw i && matchWhose whose i
instance FromYAML GalleryInfo where
parseYAML = YAML.withMap "gallery info" \m ->
GalleryInfo <$> m .: "prefix"
<*> m .: "name"
<*> m .:? "filters" .!= noFilters
<*> m .:? "ordering" .!= 0
GalleryInfo <$> m .: "title"
<*> m .: "prefix"
<*> m .:? "filters" .!= noFilters
instance FromYAML GalleryFilters where
parseYAML = YAML.withMap "gallery filters" \m ->
GalleryFilters <$> m .:? "nsfw"
<*> m .:? "who" .!= All
<*> m .:? "whose" .!= All
instance FromYAML Who where parseYAML = YAML.withStr "who" readWho
instance FromYAML Whose where parseYAML = YAML.withStr "whose" readWhose
data Pair a b = Pair !a !b