module Charon.Class.Serial
(
Serial (..),
encodeThrowM,
decodeUnit,
decodeUnitThrowM,
)
where
import Charon.Prelude
class Serial a where
type a
encode :: a -> Either String ByteString
decode :: DecodeExtra a -> ByteString -> Either String a
encodeThrowM :: (HasCallStack, MonadThrow m, Serial a) => a -> m ByteString
encodeThrowM :: forall (m :: * -> *) a.
(HasCallStack, MonadThrow m, Serial a) =>
a -> m ByteString
encodeThrowM a
x = case a -> Either String ByteString
forall a. Serial a => a -> Either String ByteString
encode a
x of
Left String
s -> String -> m ByteString
forall (m :: * -> *) a.
(MonadThrow m, HasCallStack) =>
String -> m a
throwString String
s
Right ByteString
y -> ByteString -> m ByteString
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
y
decodeThrowM ::
( HasCallStack,
Serial a,
MonadThrow m
) =>
DecodeExtra a ->
ByteString ->
m a
decodeThrowM :: forall a (m :: * -> *).
(HasCallStack, Serial a, MonadThrow m) =>
DecodeExtra a -> ByteString -> m a
decodeThrowM DecodeExtra a
extra ByteString
bs = case DecodeExtra a -> ByteString -> Either String a
forall a.
Serial a =>
DecodeExtra a -> ByteString -> Either String a
decode DecodeExtra a
extra ByteString
bs of
Left String
s -> String -> m a
forall (m :: * -> *) a.
(MonadThrow m, HasCallStack) =>
String -> m a
throwString String
s
Right a
y -> a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
y
decodeUnit :: (DecodeExtra a ~ (), Serial a) => ByteString -> Either String a
decodeUnit :: forall a.
(DecodeExtra a ~ (), Serial a) =>
ByteString -> Either String a
decodeUnit = DecodeExtra a -> ByteString -> Either String a
forall a.
Serial a =>
DecodeExtra a -> ByteString -> Either String a
decode ()
decodeUnitThrowM ::
( DecodeExtra a ~ (),
HasCallStack,
MonadThrow m,
Serial a
) =>
ByteString ->
m a
decodeUnitThrowM :: forall a (m :: * -> *).
(DecodeExtra a ~ (), HasCallStack, MonadThrow m, Serial a) =>
ByteString -> m a
decodeUnitThrowM = DecodeExtra a -> ByteString -> m a
forall a (m :: * -> *).
(HasCallStack, Serial a, MonadThrow m) =>
DecodeExtra a -> ByteString -> m a
decodeThrowM ()