summaryrefslogtreecommitdiff
path: root/Model/Types.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Model/Types.hs')
-rw-r--r--Model/Types.hs35
1 files changed, 35 insertions, 0 deletions
diff --git a/Model/Types.hs b/Model/Types.hs
new file mode 100644
index 0000000..61bebfd
--- /dev/null
+++ b/Model/Types.hs
@@ -0,0 +1,35 @@
1{-# LANGUAGE DeriveGeneric #-}
2
3module Model.Types
4 ( ItemDate(..)
5 , isNever, isUnknown, isKnown
6 ) where
7
8import ClassyPrelude.Yesod
9
10data ItemDate = DateUnknown | DateKnown Day | DateNever
11 deriving (Eq, Ord, Show, Read, Generic)
12
13isNever, isUnknown, isKnown :: ItemDate -> Bool
14isNever DateNever = True
15isNever _ = False
16isUnknown DateUnknown = True
17isUnknown _ = False
18isKnown (DateKnown _) = True
19isKnown _ = False
20
21unknownVerb :: IsString a => a
22unknownVerb = "unknown"
23
24instance ToJSON ItemDate where
25 toJSON DateNever = Null
26 toJSON DateUnknown = String unknownVerb
27 toJSON (DateKnown d) = toJSON d
28instance FromJSON ItemDate where
29 parseJSON Null = pure DateNever
30 parseJSON v@(String inp)
31 | unknownVerb == inp = pure DateUnknown
32 | otherwise = DateKnown <$> parseJSON v
33 parseJSON v = DateKnown <$> parseJSON v
34
35derivePersistFieldJSON "ItemDate"