{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
module Imm.Callback where
import Imm.Feed
import Data.Aeson
import Data.Aeson.Types
import Data.Text.Prettyprint.Doc
import Dhall
data Callback = Callback
{ _executable :: FilePath
, _arguments :: [Text]
} deriving (Eq, Generic, Ord, Read, Show)
instance Interpret Callback
instance Pretty Callback where
pretty (Callback executable arguments) = pretty executable <+> sep (pretty <$> arguments)
data Message = Message Feed FeedElement deriving(Eq, Generic, Ord, Show)
instance ToJSON Message where
toJSON (Message feed element) = object ["feed" .= renderFeed feed, "element" .= renderFeedElement element]
instance FromJSON Message where
parseJSON = withObject "Message" $ \v -> Message
<$> (v .: "feed" >>= (liftEither . parseFeed))
<*> (v .: "element" >>= (liftEither . parseFeedElement))
where liftEither :: Either e a -> Parser a
liftEither = either (const mempty) return