{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
module Imm.Callback (Callback(..), serializeMessage, deserializeMessage) where
import Imm.Feed
import qualified Data.Avro as Avro
import Data.Avro.Deriving
import Data.Text.Prettyprint.Doc
import Dhall hiding (maybe)
data Callback = Callback
{ _executable :: FilePath
, _arguments :: [Text]
} deriving (Eq, Generic, Ord, Read, Show)
instance FromDhall Callback
instance Pretty Callback where
pretty (Callback executable arguments) = pretty executable <+> sep (pretty <$> arguments)
deriveAvroWithOptions defaultDeriveOptions "idl/callback.json"
serializeMessage :: Feed -> FeedElement -> LByteString
serializeMessage feed element = Avro.encodeValue $ Message (renderFeed feed) (renderFeedElement element)
deserializeMessage :: MonadFail m => LByteString -> m (Feed, FeedElement)
deserializeMessage bytestring = do
Message feedText elementText <- Avro.decodeValue bytestring & either fail pure
feed <- parseFeed feedText & either (fail . displayException) pure
element <- parseFeedElement elementText & either (fail . displayException) pure
return (feed, element)