2018-04-11 13:25:24 +02:00
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
2018-05-11 12:31:53 +02:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2018-04-11 13:25:24 +02:00
|
|
|
module Data (
|
2018-05-11 12:31:53 +02:00
|
|
|
Key(..)
|
|
|
|
, RW(..)
|
2018-04-11 13:25:24 +02:00
|
|
|
) where
|
|
|
|
|
2018-05-11 12:31:53 +02:00
|
|
|
import Data.Aeson (FromJSON(..), ToJSON(..), ToJSONKey(..), genericToEncoding)
|
|
|
|
import Data.Aeson.Types (toJSONKeyText)
|
|
|
|
import Data.Text (pack)
|
|
|
|
import GHC.Generics
|
|
|
|
import qualified JSON (defaultOptions)
|
|
|
|
|
2018-04-11 13:25:24 +02:00
|
|
|
class RW a b where
|
2018-05-11 12:31:53 +02:00
|
|
|
get :: b -> a
|
2018-04-11 13:25:24 +02:00
|
|
|
set :: a -> b -> b
|
2018-05-11 12:31:53 +02:00
|
|
|
update :: (a -> a) -> b -> b
|
|
|
|
update f v =
|
|
|
|
set (f (get v)) v
|
|
|
|
|
|
|
|
newtype Key a = Key Int deriving (Eq, Ord, Enum, Read, Show, Generic)
|
|
|
|
|
|
|
|
instance FromJSON (Key a)
|
|
|
|
instance ToJSON (Key a) where
|
|
|
|
toEncoding = genericToEncoding JSON.defaultOptions
|
|
|
|
|
|
|
|
instance ToJSONKey (Key a) where
|
|
|
|
toJSONKey = toJSONKeyText (pack . \(Key n) -> show n)
|