{-# LANGUAGE DeriveAnyClass #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Hercules.Agent.WorkerProtocol.Event where

import Control.Monad (fail)
import qualified Data.Aeson as A
import Data.Binary
import Data.UUID (UUID)
import Hercules.API.Agent.Evaluate.EvaluateEvent.OnPushHandlerEvent (OnPushHandlerEvent)
import Hercules.Agent.WorkerProtocol.Event.Attribute
import Hercules.Agent.WorkerProtocol.Event.AttributeError
import Hercules.Agent.WorkerProtocol.Event.BuildResult
import Protolude hiding (get, put)
import Prelude ()

data Event
  = Attribute Attribute
  | AttributeError AttributeError
  | EvaluationDone
  | Error Text
  | Build ByteString Text (Maybe UUID)
  | BuildResult BuildResult
  | EffectResult Int
  | JobConfig
  | OnPushHandler (ViaJSON OnPushHandlerEvent)
  | Exception Text
  deriving ((forall x. Event -> Rep Event x)
-> (forall x. Rep Event x -> Event) -> Generic Event
forall x. Rep Event x -> Event
forall x. Event -> Rep Event x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Event x -> Event
$cfrom :: forall x. Event -> Rep Event x
Generic, Get Event
[Event] -> Put
Event -> Put
(Event -> Put) -> Get Event -> ([Event] -> Put) -> Binary Event
forall t. (t -> Put) -> Get t -> ([t] -> Put) -> Binary t
putList :: [Event] -> Put
$cputList :: [Event] -> Put
get :: Get Event
$cget :: Get Event
put :: Event -> Put
$cput :: Event -> Put
Binary, Int -> Event -> ShowS
[Event] -> ShowS
Event -> String
(Int -> Event -> ShowS)
-> (Event -> String) -> ([Event] -> ShowS) -> Show Event
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Event] -> ShowS
$cshowList :: [Event] -> ShowS
show :: Event -> String
$cshow :: Event -> String
showsPrec :: Int -> Event -> ShowS
$cshowsPrec :: Int -> Event -> ShowS
Show, Event -> Event -> Bool
(Event -> Event -> Bool) -> (Event -> Event -> Bool) -> Eq Event
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Event -> Event -> Bool
$c/= :: Event -> Event -> Bool
== :: Event -> Event -> Bool
$c== :: Event -> Event -> Bool
Eq)

newtype ViaJSON a = ViaJSON {forall a. ViaJSON a -> a
fromViaJSON :: a}
  deriving (ViaJSON a -> ViaJSON a -> Bool
(ViaJSON a -> ViaJSON a -> Bool)
-> (ViaJSON a -> ViaJSON a -> Bool) -> Eq (ViaJSON a)
forall a. Eq a => ViaJSON a -> ViaJSON a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ViaJSON a -> ViaJSON a -> Bool
$c/= :: forall a. Eq a => ViaJSON a -> ViaJSON a -> Bool
== :: ViaJSON a -> ViaJSON a -> Bool
$c== :: forall a. Eq a => ViaJSON a -> ViaJSON a -> Bool
Eq, Eq (ViaJSON a)
Eq (ViaJSON a)
-> (ViaJSON a -> ViaJSON a -> Ordering)
-> (ViaJSON a -> ViaJSON a -> Bool)
-> (ViaJSON a -> ViaJSON a -> Bool)
-> (ViaJSON a -> ViaJSON a -> Bool)
-> (ViaJSON a -> ViaJSON a -> Bool)
-> (ViaJSON a -> ViaJSON a -> ViaJSON a)
-> (ViaJSON a -> ViaJSON a -> ViaJSON a)
-> Ord (ViaJSON a)
ViaJSON a -> ViaJSON a -> Bool
ViaJSON a -> ViaJSON a -> Ordering
ViaJSON a -> ViaJSON a -> ViaJSON a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {a}. Ord a => Eq (ViaJSON a)
forall a. Ord a => ViaJSON a -> ViaJSON a -> Bool
forall a. Ord a => ViaJSON a -> ViaJSON a -> Ordering
forall a. Ord a => ViaJSON a -> ViaJSON a -> ViaJSON a
min :: ViaJSON a -> ViaJSON a -> ViaJSON a
$cmin :: forall a. Ord a => ViaJSON a -> ViaJSON a -> ViaJSON a
max :: ViaJSON a -> ViaJSON a -> ViaJSON a
$cmax :: forall a. Ord a => ViaJSON a -> ViaJSON a -> ViaJSON a
>= :: ViaJSON a -> ViaJSON a -> Bool
$c>= :: forall a. Ord a => ViaJSON a -> ViaJSON a -> Bool
> :: ViaJSON a -> ViaJSON a -> Bool
$c> :: forall a. Ord a => ViaJSON a -> ViaJSON a -> Bool
<= :: ViaJSON a -> ViaJSON a -> Bool
$c<= :: forall a. Ord a => ViaJSON a -> ViaJSON a -> Bool
< :: ViaJSON a -> ViaJSON a -> Bool
$c< :: forall a. Ord a => ViaJSON a -> ViaJSON a -> Bool
compare :: ViaJSON a -> ViaJSON a -> Ordering
$ccompare :: forall a. Ord a => ViaJSON a -> ViaJSON a -> Ordering
Ord, Int -> ViaJSON a -> ShowS
[ViaJSON a] -> ShowS
ViaJSON a -> String
(Int -> ViaJSON a -> ShowS)
-> (ViaJSON a -> String)
-> ([ViaJSON a] -> ShowS)
-> Show (ViaJSON a)
forall a. Show a => Int -> ViaJSON a -> ShowS
forall a. Show a => [ViaJSON a] -> ShowS
forall a. Show a => ViaJSON a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ViaJSON a] -> ShowS
$cshowList :: forall a. Show a => [ViaJSON a] -> ShowS
show :: ViaJSON a -> String
$cshow :: forall a. Show a => ViaJSON a -> String
showsPrec :: Int -> ViaJSON a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> ViaJSON a -> ShowS
Show, ReadPrec [ViaJSON a]
ReadPrec (ViaJSON a)
Int -> ReadS (ViaJSON a)
ReadS [ViaJSON a]
(Int -> ReadS (ViaJSON a))
-> ReadS [ViaJSON a]
-> ReadPrec (ViaJSON a)
-> ReadPrec [ViaJSON a]
-> Read (ViaJSON a)
forall a. Read a => ReadPrec [ViaJSON a]
forall a. Read a => ReadPrec (ViaJSON a)
forall a. Read a => Int -> ReadS (ViaJSON a)
forall a. Read a => ReadS [ViaJSON a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ViaJSON a]
$creadListPrec :: forall a. Read a => ReadPrec [ViaJSON a]
readPrec :: ReadPrec (ViaJSON a)
$creadPrec :: forall a. Read a => ReadPrec (ViaJSON a)
readList :: ReadS [ViaJSON a]
$creadList :: forall a. Read a => ReadS [ViaJSON a]
readsPrec :: Int -> ReadS (ViaJSON a)
$creadsPrec :: forall a. Read a => Int -> ReadS (ViaJSON a)
Read)

-- | Orphan
instance (A.ToJSON a, A.FromJSON a) => Binary (ViaJSON a) where
  put :: ViaJSON a -> Put
put (ViaJSON a
a) = ByteString -> Put
forall t. Binary t => t -> Put
put (a -> ByteString
forall a. ToJSON a => a -> ByteString
A.encode a
a)
  get :: Get (ViaJSON a)
get = do
    ByteString
bs <- Get ByteString
forall t. Binary t => Get t
get
    case ByteString -> Either String a
forall a. FromJSON a => ByteString -> Either String a
A.eitherDecode ByteString
bs of
      Left String
s -> String -> Get (ViaJSON a)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
s
      Right a
r -> ViaJSON a -> Get (ViaJSON a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> ViaJSON a
forall a. a -> ViaJSON a
ViaJSON a
r)