diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2017-03-16 01:19:07 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2017-03-16 01:19:07 +0100 |
commit | 3bfe0bdcb79b398a387e202c5150b5e6fd230d3a (patch) | |
tree | 8384b49048e84969a3a3440ed309b9c6e6b779de /Model/Types.hs | |
parent | 3ed9ec8ca70afb556f75d4e087043f4c67f50974 (diff) | |
download | bar-3bfe0bdcb79b398a387e202c5150b5e6fd230d3a.tar bar-3bfe0bdcb79b398a387e202c5150b5e6fd230d3a.tar.gz bar-3bfe0bdcb79b398a387e202c5150b5e6fd230d3a.tar.bz2 bar-3bfe0bdcb79b398a387e202c5150b5e6fd230d3a.tar.xz bar-3bfe0bdcb79b398a387e202c5150b5e6fd230d3a.zip |
More complicated date handling
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" | ||