{-# LANGUAGE DeriveGeneric, FlexibleInstances #-} {- | Main types for debug-me - - Note that changing types in ways that change the JSON serialization - changes debug-me's wire format. -} module Types ( module Types, Val(..) ) where import Val import GHC.Generics (Generic) import Data.Aeson import qualified Data.Aeson.Types as Aeson -- | Things that the developer sees. data Seen = Seen { seenData :: Val } deriving (Show, Generic) -- | Things that the developer enters. data Entered = Entered { enteredData :: Val , echoData :: Val -- ^ Data that is expected to be Seen, but has not been received -- at the time this was entered. } deriving (Show, Generic) -- | High level protocol. data Proto a = Proto a -- ^ either Entered or Seen | Rejected (Activity Entered) -- ^ sent by user to indicate when an Entered value was rejected. deriving (Show, Generic) -- | A Proto activity (either Entered or Seen) with a pointer -- to the Activity before this one. -- -- The Signature is over both the data in the activity, and its pointer. data Activity a = Activity { activity :: Proto a , prevActivity :: (Maybe Hash) , signature :: Signature } deriving (Show, Generic) newtype Signature = Signature Val deriving (Show, Generic) data Hash = Hash { hashMethod :: HashMethod , hashValue :: Val } deriving (Show, Generic, Eq) -- | We use SHA256. (SHA3 is included to future proof, and because it -- improves the generated JSON.) data HashMethod = SHA256 | SHA3 deriving (Show, Generic, Eq) instance ToJSON Seen instance FromJSON Seen instance ToJSON Entered instance FromJSON Entered instance ToJSON (Activity Seen) instance FromJSON (Activity Seen) instance ToJSON (Activity Entered) instance FromJSON (Activity Entered) instance ToJSON Signature instance FromJSON Signature instance ToJSON Hash instance FromJSON Hash instance ToJSON HashMethod instance FromJSON HashMethod -- | Nicer JSON encoding for sum types. sumOptions :: Aeson.Options sumOptions = defaultOptions { Aeson.sumEncoding = Aeson.ObjectWithSingleField } instance ToJSON (Proto Seen) where toJSON = genericToJSON sumOptions toEncoding = genericToEncoding sumOptions instance FromJSON (Proto Seen) where parseJSON = genericParseJSON sumOptions instance ToJSON (Proto Entered) where toJSON = genericToJSON sumOptions toEncoding = genericToEncoding sumOptions instance FromJSON (Proto Entered) where parseJSON = genericParseJSON sumOptions