diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2017-03-16 13:18:34 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2017-03-16 13:18:34 +0100 |
commit | 57d594818c14652681dce54d324b6b76941b2f4e (patch) | |
tree | f3e1e02e2fa41ff0d87eace034d6c883668841c5 /Model/Types.hs | |
parent | 3d828feba67f21ae62d1e6eb598a22ffaebf1174 (diff) | |
parent | 3bfe0bdcb79b398a387e202c5150b5e6fd230d3a (diff) | |
download | bar-57d594818c14652681dce54d324b6b76941b2f4e.tar bar-57d594818c14652681dce54d324b6b76941b2f4e.tar.gz bar-57d594818c14652681dce54d324b6b76941b2f4e.tar.bz2 bar-57d594818c14652681dce54d324b6b76941b2f4e.tar.xz bar-57d594818c14652681dce54d324b6b76941b2f4e.zip |
Merge branch 'feat/openYes'
Diffstat (limited to 'Model/Types.hs')
-rw-r--r-- | Model/Types.hs | 35 |
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 | |||
3 | module Model.Types | ||
4 | ( ItemDate(..) | ||
5 | , isNever, isUnknown, isKnown | ||
6 | ) where | ||
7 | |||
8 | import ClassyPrelude.Yesod | ||
9 | |||
10 | data ItemDate = DateUnknown | DateKnown Day | DateNever | ||
11 | deriving (Eq, Ord, Show, Read, Generic) | ||
12 | |||
13 | isNever, isUnknown, isKnown :: ItemDate -> Bool | ||
14 | isNever DateNever = True | ||
15 | isNever _ = False | ||
16 | isUnknown DateUnknown = True | ||
17 | isUnknown _ = False | ||
18 | isKnown (DateKnown _) = True | ||
19 | isKnown _ = False | ||
20 | |||
21 | unknownVerb :: IsString a => a | ||
22 | unknownVerb = "unknown" | ||
23 | |||
24 | instance ToJSON ItemDate where | ||
25 | toJSON DateNever = Null | ||
26 | toJSON DateUnknown = String unknownVerb | ||
27 | toJSON (DateKnown d) = toJSON d | ||
28 | instance 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 | |||
35 | derivePersistFieldJSON "ItemDate" | ||