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
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, 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
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)
ReadS [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)

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