From 3bfe0bdcb79b398a387e202c5150b5e6fd230d3a Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Thu, 16 Mar 2017 01:19:07 +0100 Subject: More complicated date handling --- Model/Types.hs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Model/Types.hs (limited to 'Model/Types.hs') 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 @@ +{-# LANGUAGE DeriveGeneric #-} + +module Model.Types + ( ItemDate(..) + , isNever, isUnknown, isKnown + ) where + +import ClassyPrelude.Yesod + +data ItemDate = DateUnknown | DateKnown Day | DateNever + deriving (Eq, Ord, Show, Read, Generic) + +isNever, isUnknown, isKnown :: ItemDate -> Bool +isNever DateNever = True +isNever _ = False +isUnknown DateUnknown = True +isUnknown _ = False +isKnown (DateKnown _) = True +isKnown _ = False + +unknownVerb :: IsString a => a +unknownVerb = "unknown" + +instance ToJSON ItemDate where + toJSON DateNever = Null + toJSON DateUnknown = String unknownVerb + toJSON (DateKnown d) = toJSON d +instance FromJSON ItemDate where + parseJSON Null = pure DateNever + parseJSON v@(String inp) + | unknownVerb == inp = pure DateUnknown + | otherwise = DateKnown <$> parseJSON v + parseJSON v = DateKnown <$> parseJSON v + +derivePersistFieldJSON "ItemDate" -- cgit v1.2.3