module Hercules.Agent.WorkerProtocol.ViaJSON where

import Data.Aeson qualified as A
import Data.Binary (Binary (..))
import Prelude

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
$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
/= :: 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
$ccompare :: forall a. Ord a => ViaJSON a -> ViaJSON a -> Ordering
compare :: ViaJSON a -> ViaJSON a -> Ordering
$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
>= :: ViaJSON a -> ViaJSON a -> Bool
$cmax :: forall a. Ord a => ViaJSON a -> ViaJSON a -> ViaJSON a
max :: ViaJSON a -> ViaJSON a -> ViaJSON a
$cmin :: forall a. Ord a => ViaJSON a -> ViaJSON a -> ViaJSON a
min :: ViaJSON a -> ViaJSON a -> ViaJSON a
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
$cshowsPrec :: forall a. Show a => Int -> ViaJSON a -> ShowS
showsPrec :: Int -> ViaJSON a -> ShowS
$cshow :: forall a. Show a => ViaJSON a -> String
show :: ViaJSON a -> String
$cshowList :: forall a. Show a => [ViaJSON a] -> ShowS
showList :: [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
$creadsPrec :: forall a. Read a => Int -> ReadS (ViaJSON a)
readsPrec :: Int -> ReadS (ViaJSON a)
$creadList :: forall a. Read a => ReadS [ViaJSON a]
readList :: ReadS [ViaJSON a]
$creadPrec :: forall a. Read a => ReadPrec (ViaJSON a)
readPrec :: ReadPrec (ViaJSON a)
$creadListPrec :: forall a. Read a => ReadPrec [ViaJSON a]
readListPrec :: ReadPrec [ViaJSON a]
Read)

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 a. String -> Get a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
s
      Right a
r -> ViaJSON a -> Get (ViaJSON a)
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> ViaJSON a
forall a. a -> ViaJSON a
ViaJSON a
r)