{-| Ministries can be encoded to and decoded from JSON: >>> import Data.Aeson (decode, encode) >>> import Data.ByteString.Lazy.Char8 (pack) >>> encode Defense "\"Defense\"" >>> encode [ Defense, Welfare ] "[\"Defense\",\"Welfare\"]" >>> decode . pack $ "[ \"Science\" ]" :: Maybe [Ministry] Just [Science] -} {-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DeriveGeneric #-} module Malodivo.Types.Ministry ( Ministry(..) , MinistryInfo , MinistryLimits , mi2ml ) where import Control.Arrow ((&&&)) import GHC.Generics (Generic) import Data.Aeson (FromJSON, ToJSON) import qualified Data.HashMap.Strict as HM import Data.Hashable (Hashable) -- | Ministry of the Kingdom of Malodivo. data Ministry = Defense | Science | Welfare deriving (Eq, Hashable, Show, Generic, FromJSON, ToJSON) -- | Convenient type. Describes maximum money a ministry can get. type MinistryLimits = HM.HashMap Ministry Integer {-| We use this data type instead of 'MinistryLimits' for JSON, because maps other than @HashMap Text _@ are hard to decode/encode see . Anyway, we still have type-checked typos-proof structure. -} data MinistryInfo = MinistryInfo { name :: Ministry , amount :: Integer } deriving (Generic, FromJSON) mi2ml :: [MinistryInfo] -> MinistryLimits mi2ml = HM.fromListWith (+) . map (name &&& amount)