{-# 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"