{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module Cardano.Ledger.Binary.Encoding.Coders (
Encode (..),
(!>),
encode,
runE,
encodeDual,
encodeKeyedStrictMaybeWith,
encodeKeyedStrictMaybe,
)
where
import Cardano.Ledger.Binary.Decoding.Coders (Density (Dense, Sparse), Wrapped (..))
import Cardano.Ledger.Binary.Decoding.DecCBOR (DecCBOR (..))
import Cardano.Ledger.Binary.Decoding.Decoder (Decoder)
import Cardano.Ledger.Binary.Encoding.EncCBOR (EncCBOR (encCBOR))
import Cardano.Ledger.Binary.Encoding.Encoder (
Encoding,
encodeListLen,
encodeMapLen,
encodeTag,
encodeWord,
)
import Cardano.Ledger.Binary.Group (EncCBORGroup (..))
import Data.Maybe.Strict (StrictMaybe (SJust, SNothing))
data Encode (w :: Wrapped) t where
Rec :: t -> Encode ('Closed 'Dense) t
Sum :: t -> Word -> Encode 'Open t
Keyed :: t -> Encode ('Closed 'Sparse) t
To :: EncCBOR a => a -> Encode ('Closed 'Dense) a
ToGroup :: EncCBORGroup t => t -> Encode ('Closed 'Dense) t
E :: (t -> Encoding) -> t -> Encode ('Closed 'Dense) t
MapE :: (a -> b) -> Encode w a -> Encode w b
OmitC :: t -> Encode w t
Tag :: Word -> Encode ('Closed x) t -> Encode ('Closed x) t
Omit ::
(t -> Bool) ->
Encode ('Closed 'Sparse) t ->
Encode ('Closed 'Sparse) t
Key :: Word -> Encode ('Closed 'Dense) t -> Encode ('Closed 'Sparse) t
ApplyE :: Encode w (a -> t) -> Encode ('Closed r) a -> Encode w t
infixl 4 !>
(!>) :: Encode w (a -> t) -> Encode ('Closed r) a -> Encode w t
Encode w (a -> t)
x !> :: forall (w :: Wrapped) a t (r :: Density).
Encode w (a -> t) -> Encode ('Closed r) a -> Encode w t
!> Encode ('Closed r) a
y = forall (w :: Wrapped) a t (r :: Density).
Encode w (a -> t) -> Encode ('Closed r) a -> Encode w t
ApplyE Encode w (a -> t)
x Encode ('Closed r) a
y
runE :: Encode w t -> t
runE :: forall (w :: Wrapped) t. Encode w t -> t
runE (Sum t
cn Word
_) = t
cn
runE (Rec t
cn) = t
cn
runE (ApplyE Encode w (a -> t)
f Encode ('Closed r) a
x) = forall (w :: Wrapped) t. Encode w t -> t
runE Encode w (a -> t)
f (forall (w :: Wrapped) t. Encode w t -> t
runE Encode ('Closed r) a
x)
runE (To t
x) = t
x
runE (ToGroup t
x) = t
x
runE (E t -> Encoding
_ t
x) = t
x
runE (MapE a -> t
f Encode w a
x) = a -> t
f forall a b. (a -> b) -> a -> b
$ forall (w :: Wrapped) t. Encode w t -> t
runE Encode w a
x
runE (OmitC t
x) = t
x
runE (Omit t -> Bool
_ Encode ('Closed 'Sparse) t
x) = forall (w :: Wrapped) t. Encode w t -> t
runE Encode ('Closed 'Sparse) t
x
runE (Tag Word
_ Encode ('Closed x) t
x) = forall (w :: Wrapped) t. Encode w t -> t
runE Encode ('Closed x) t
x
runE (Key Word
_ Encode ('Closed 'Dense) t
x) = forall (w :: Wrapped) t. Encode w t -> t
runE Encode ('Closed 'Dense) t
x
runE (Keyed t
cn) = t
cn
gsize :: Encode w t -> Word
gsize :: forall (w :: Wrapped) t. Encode w t -> Word
gsize (Sum t
_ Word
_) = Word
0
gsize (Rec t
_) = Word
0
gsize (To t
_) = Word
1
gsize (ToGroup t
x) = forall a. EncCBORGroup a => a -> Word
listLen t
x
gsize (E t -> Encoding
_ t
_) = Word
1
gsize (MapE a -> t
_ Encode w a
x) = forall (w :: Wrapped) t. Encode w t -> Word
gsize Encode w a
x
gsize (ApplyE Encode w (a -> t)
f Encode ('Closed r) a
x) = forall (w :: Wrapped) t. Encode w t -> Word
gsize Encode w (a -> t)
f forall a. Num a => a -> a -> a
+ forall (w :: Wrapped) t. Encode w t -> Word
gsize Encode ('Closed r) a
x
gsize (OmitC t
_) = Word
0
gsize (Omit t -> Bool
p Encode ('Closed 'Sparse) t
x) = if t -> Bool
p (forall (w :: Wrapped) t. Encode w t -> t
runE Encode ('Closed 'Sparse) t
x) then Word
0 else forall (w :: Wrapped) t. Encode w t -> Word
gsize Encode ('Closed 'Sparse) t
x
gsize (Tag Word
_ Encode ('Closed x) t
_) = Word
1
gsize (Key Word
_ Encode ('Closed 'Dense) t
x) = forall (w :: Wrapped) t. Encode w t -> Word
gsize Encode ('Closed 'Dense) t
x
gsize (Keyed t
_) = Word
0
encode :: Encode w t -> Encoding
encode :: forall (w :: Wrapped) t. Encode w t -> Encoding
encode = forall (w :: Wrapped) t. Word -> Encode w t -> Encoding
encodeCountPrefix Word
0
where
encodeCountPrefix :: Word -> Encode w t -> Encoding
encodeCountPrefix :: forall (w :: Wrapped) t. Word -> Encode w t -> Encoding
encodeCountPrefix Word
n (Sum t
_ Word
tag) = Word -> Encoding
encodeListLen (Word
n forall a. Num a => a -> a -> a
+ Word
1) forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
tag
encodeCountPrefix Word
n (Keyed t
_) = Word -> Encoding
encodeMapLen Word
n
encodeCountPrefix Word
n (Rec t
_) = Word -> Encoding
encodeListLen Word
n
encodeCountPrefix Word
_ (To t
x) = forall a. EncCBOR a => a -> Encoding
encCBOR t
x
encodeCountPrefix Word
_ (ToGroup t
x) = forall a. EncCBORGroup a => a -> Encoding
encCBORGroup t
x
encodeCountPrefix Word
_ (E t -> Encoding
enc t
x) = t -> Encoding
enc t
x
encodeCountPrefix Word
n (MapE a -> t
_ Encode w a
x) = forall (w :: Wrapped) t. Word -> Encode w t -> Encoding
encodeCountPrefix Word
n Encode w a
x
encodeCountPrefix Word
_ (OmitC t
_) = forall a. Monoid a => a
mempty
encodeCountPrefix Word
n (Tag Word
tag Encode ('Closed x) t
x) = Word -> Encoding
encodeTag Word
tag forall a. Semigroup a => a -> a -> a
<> forall (w :: Wrapped) t. Word -> Encode w t -> Encoding
encodeCountPrefix Word
n Encode ('Closed x) t
x
encodeCountPrefix Word
n (Key Word
tag Encode ('Closed 'Dense) t
x) = Word -> Encoding
encodeWord Word
tag forall a. Semigroup a => a -> a -> a
<> forall (w :: Wrapped) t. Word -> Encode w t -> Encoding
encodeCountPrefix Word
n Encode ('Closed 'Dense) t
x
encodeCountPrefix Word
n (Omit t -> Bool
p Encode ('Closed 'Sparse) t
x) =
if t -> Bool
p (forall (w :: Wrapped) t. Encode w t -> t
runE Encode ('Closed 'Sparse) t
x) then forall a. Monoid a => a
mempty else forall (w :: Wrapped) t. Word -> Encode w t -> Encoding
encodeCountPrefix Word
n Encode ('Closed 'Sparse) t
x
encodeCountPrefix Word
n (ApplyE Encode w (a -> t)
ff Encode ('Closed r) a
xx) = forall (w :: Wrapped) t. Word -> Encode w t -> Encoding
encodeCountPrefix (Word
n forall a. Num a => a -> a -> a
+ forall (w :: Wrapped) t. Encode w t -> Word
gsize Encode ('Closed r) a
xx) Encode w (a -> t)
ff forall a. Semigroup a => a -> a -> a
<> forall (d :: Density) t. Encode ('Closed d) t -> Encoding
encodeClosed Encode ('Closed r) a
xx
where
encodeClosed :: Encode ('Closed d) t -> Encoding
encodeClosed :: forall (d :: Density) t. Encode ('Closed d) t -> Encoding
encodeClosed (Rec t
_) = forall a. Monoid a => a
mempty
encodeClosed (To t
x) = forall a. EncCBOR a => a -> Encoding
encCBOR t
x
encodeClosed (ToGroup t
x) = forall a. EncCBORGroup a => a -> Encoding
encCBORGroup t
x
encodeClosed (E t -> Encoding
enc t
x) = t -> Encoding
enc t
x
encodeClosed (MapE a -> t
_ Encode ('Closed d) a
x) = forall (d :: Density) t. Encode ('Closed d) t -> Encoding
encodeClosed Encode ('Closed d) a
x
encodeClosed (ApplyE Encode ('Closed d) (a -> t)
f Encode ('Closed r) a
x) = forall (d :: Density) t. Encode ('Closed d) t -> Encoding
encodeClosed Encode ('Closed d) (a -> t)
f forall a. Semigroup a => a -> a -> a
<> forall (d :: Density) t. Encode ('Closed d) t -> Encoding
encodeClosed Encode ('Closed r) a
x
encodeClosed (OmitC t
_) = forall a. Monoid a => a
mempty
encodeClosed (Omit t -> Bool
p Encode ('Closed 'Sparse) t
x) =
if t -> Bool
p (forall (w :: Wrapped) t. Encode w t -> t
runE Encode ('Closed 'Sparse) t
x) then forall a. Monoid a => a
mempty else forall (d :: Density) t. Encode ('Closed d) t -> Encoding
encodeClosed Encode ('Closed 'Sparse) t
x
encodeClosed (Tag Word
tag Encode ('Closed x) t
x) = Word -> Encoding
encodeTag Word
tag forall a. Semigroup a => a -> a -> a
<> forall (d :: Density) t. Encode ('Closed d) t -> Encoding
encodeClosed Encode ('Closed x) t
x
encodeClosed (Key Word
tag Encode ('Closed 'Dense) t
x) = Word -> Encoding
encodeWord Word
tag forall a. Semigroup a => a -> a -> a
<> forall (d :: Density) t. Encode ('Closed d) t -> Encoding
encodeClosed Encode ('Closed 'Dense) t
x
encodeClosed (Keyed t
_) = forall a. Monoid a => a
mempty
encodeKeyedStrictMaybeWith ::
Word ->
(a -> Encoding) ->
StrictMaybe a ->
Encode ('Closed 'Sparse) (StrictMaybe a)
encodeKeyedStrictMaybeWith :: forall a.
Word
-> (a -> Encoding)
-> StrictMaybe a
-> Encode ('Closed 'Sparse) (StrictMaybe a)
encodeKeyedStrictMaybeWith Word
_ a -> Encoding
_ StrictMaybe a
SNothing = forall t (w :: Wrapped). t -> Encode w t
OmitC forall a. StrictMaybe a
SNothing
encodeKeyedStrictMaybeWith Word
key a -> Encoding
enc (SJust a
x) = forall t.
Word -> Encode ('Closed 'Dense) t -> Encode ('Closed 'Sparse) t
Key Word
key (forall a b (w :: Wrapped). (a -> b) -> Encode w a -> Encode w b
MapE forall a. a -> StrictMaybe a
SJust forall a b. (a -> b) -> a -> b
$ forall t. (t -> Encoding) -> t -> Encode ('Closed 'Dense) t
E a -> Encoding
enc a
x)
encodeKeyedStrictMaybe ::
EncCBOR a =>
Word ->
StrictMaybe a ->
Encode ('Closed 'Sparse) (StrictMaybe a)
encodeKeyedStrictMaybe :: forall a.
EncCBOR a =>
Word -> StrictMaybe a -> Encode ('Closed 'Sparse) (StrictMaybe a)
encodeKeyedStrictMaybe Word
key = forall a.
Word
-> (a -> Encoding)
-> StrictMaybe a
-> Encode ('Closed 'Sparse) (StrictMaybe a)
encodeKeyedStrictMaybeWith Word
key forall a. EncCBOR a => a -> Encoding
encCBOR
encodeDual :: forall t. (EncCBOR t, DecCBOR t) => t -> Encode ('Closed 'Dense) t
encodeDual :: forall t. (EncCBOR t, DecCBOR t) => t -> Encode ('Closed 'Dense) t
encodeDual = forall t. (t -> Encoding) -> t -> Encode ('Closed 'Dense) t
E forall a. EncCBOR a => a -> Encoding
encCBOR
where
_decCBOR :: Decoder Any t
_decCBOR = forall a s. DecCBOR a => Decoder s a
decCBOR :: Decoder s t