summaryrefslogtreecommitdiff
path: root/Model/Types.hs
blob: 61bebfdc7eeb9936c8d9300951a14befc6a52b45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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"