{-# LANGUAGE StrictData #-}
{-# LANGUAGE NoFieldSelectors #-}
module Wai.CryptoCookie.Encoding
( Encoding (..)
, aeson
, binary
) where
import Data.Aeson qualified as Ae
import Data.Binary qualified as Bin
import Data.ByteString.Lazy qualified as BL
import Data.Kind (Type)
data Encoding (a :: Type) = Encoding
{ forall a. Encoding a -> a -> ByteString
encode :: a -> BL.ByteString
, forall a. Encoding a -> ByteString -> Maybe a
decode :: BL.ByteString -> Maybe a
}
binary :: (Bin.Binary a) => Encoding a
binary :: forall a. Binary a => Encoding a
binary =
Encoding
{ encode :: a -> ByteString
encode = a -> ByteString
forall a. Binary a => a -> ByteString
Bin.encode
, decode :: ByteString -> Maybe a
decode = \ByteString
bl -> case ByteString
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
forall a.
Binary a =>
ByteString
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
Bin.decodeOrFail ByteString
bl of
Right (ByteString
_, ByteOffset
_, a
a) -> a -> Maybe a
forall a. a -> Maybe a
Just a
a
Left (ByteString, ByteOffset, String)
_ -> Maybe a
forall a. Maybe a
Nothing
}
aeson :: (Ae.FromJSON a, Ae.ToJSON a) => Encoding a
aeson :: forall a. (FromJSON a, ToJSON a) => Encoding a
aeson = Encoding{encode :: a -> ByteString
encode = a -> ByteString
forall a. ToJSON a => a -> ByteString
Ae.encode, decode :: ByteString -> Maybe a
decode = ByteString -> Maybe a
forall a. FromJSON a => ByteString -> Maybe a
Ae.decode}