{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeData #-}
{-# LANGUAGE TypeFamilies #-}
module Cardano.Ledger.Binary.Decoding.Coders (
Decode (..),
(<!),
(<*!),
(<?),
decode,
decodeSparse,
mapCoder,
Density (..),
Wrapped (..),
Field (..),
ofield,
invalidField,
field,
fieldGuarded,
fieldA,
fieldAA,
ofieldA,
fieldAGuarded,
decodeDual,
listDecodeA,
mapDecodeA,
setDecodeA,
decodeRecordNamed,
decodeRecordNamedT,
decodeRecordSum,
invalidKey,
unusedRequiredKeys,
duplicateKey,
guardUntilAtLeast,
) where
import Cardano.Ledger.Binary.Decoding.Annotated (Annotator (..), decodeAnnSet)
import Cardano.Ledger.Binary.Decoding.DecCBOR (DecCBOR (decCBOR))
import Cardano.Ledger.Binary.Decoding.Decoder
import Cardano.Ledger.Binary.Encoding.EncCBOR (EncCBOR (encCBOR))
import Cardano.Ledger.Binary.Group (DecCBORGroup (..), EncCBORGroup (..))
import Cardano.Ledger.Binary.Version (Version)
#if ! MIN_VERSION_base(4,18,0)
import Control.Applicative (liftA2)
#endif
import Control.Monad (when)
import qualified Data.Map.Strict as Map
import Data.Maybe.Strict (StrictMaybe (..))
import Data.Set (Set, insert, member)
import qualified Data.Set as Set
import qualified Data.Text as Text
import Data.Typeable (Proxy (..), Typeable, typeOf)
import Data.Void (Void)
type data Density = Dense | Sparse
type data Wrapped where
Open :: Wrapped
Closed :: Density -> Wrapped
data Field t where
Field :: (x -> t -> t) -> (forall s. Decoder s x) -> Field t
{-# INLINE field #-}
field :: Typeable x => (x -> t -> t) -> Decode (Closed d) x -> Field t
field :: forall x t (d :: Density).
Typeable x =>
(x -> t -> t) -> Decode (Closed d) x -> Field t
field x -> t -> t
update Decode (Closed d) x
dec = (x -> t -> t) -> (forall s. Decoder s x) -> Field t
forall x t. (x -> t -> t) -> (forall s. Decoder s x) -> Field t
Field x -> t -> t
update (Decode (Closed d) x -> Decoder s x
forall t (w :: Wrapped) s. Typeable t => Decode w t -> Decoder s t
decode Decode (Closed d) x
dec)
{-# INLINE fieldGuarded #-}
fieldGuarded ::
Typeable x =>
String ->
(x -> Bool) ->
(x -> t -> t) ->
Decode (Closed d) x ->
Field t
fieldGuarded :: forall x t (d :: Density).
Typeable x =>
String
-> (x -> Bool) -> (x -> t -> t) -> Decode (Closed d) x -> Field t
fieldGuarded String
failMsg x -> Bool
check x -> t -> t
update Decode (Closed d) x
dec =
(x -> t -> t) -> (forall s. Decoder s x) -> Field t
forall x t. (x -> t -> t) -> (forall s. Decoder s x) -> Field t
Field
x -> t -> t
update
( Decode (Closed d) x -> Decoder s x
forall t (w :: Wrapped) s. Typeable t => Decode w t -> Decoder s t
decode Decode (Closed d) x
dec Decoder s x -> (x -> Decoder s x) -> Decoder s x
forall a b. Decoder s a -> (a -> Decoder s b) -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \x
x ->
x
x x -> Decoder s () -> Decoder s x
forall a b. a -> Decoder s b -> Decoder s a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (x -> Bool
check x
x) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
failMsg)
)
{-# INLINE ofield #-}
ofield :: Typeable x => (StrictMaybe x -> t -> t) -> Decode (Closed d) x -> Field t
ofield :: forall x t (d :: Density).
Typeable x =>
(StrictMaybe x -> t -> t) -> Decode (Closed d) x -> Field t
ofield StrictMaybe x -> t -> t
update Decode (Closed d) x
dec = (StrictMaybe x -> t -> t)
-> (forall s. Decoder s (StrictMaybe x)) -> Field t
forall x t. (x -> t -> t) -> (forall s. Decoder s x) -> Field t
Field StrictMaybe x -> t -> t
update (x -> StrictMaybe x
forall a. a -> StrictMaybe a
SJust (x -> StrictMaybe x) -> Decoder s x -> Decoder s (StrictMaybe x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decode (Closed d) x -> Decoder s x
forall t (w :: Wrapped) s. Typeable t => Decode w t -> Decoder s t
decode Decode (Closed d) x
dec)
{-# INLINE invalidField #-}
invalidField :: forall t. Word -> Field t
invalidField :: forall t. Word -> Field t
invalidField Word
n = (Void -> t -> t) -> Decode (Closed (ZonkAny 1)) Void -> Field t
forall x t (d :: Density).
Typeable x =>
(x -> t -> t) -> Decode (Closed d) x -> Field t
field ((t -> Void -> t) -> Void -> t -> t
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((t -> Void -> t) -> Void -> t -> t)
-> (t -> Void -> t) -> Void -> t -> t
forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const @t @Void) (Word -> Decode (Closed (ZonkAny 1)) Void
forall (w :: Wrapped) t. Word -> Decode w t
Invalid Word
n)
{-# INLINE fieldA #-}
fieldA :: (Typeable x, Applicative ann) => (x -> t -> t) -> Decode (Closed d) x -> Field (ann t)
fieldA :: forall x (ann :: * -> *) t (d :: Density).
(Typeable x, Applicative ann) =>
(x -> t -> t) -> Decode (Closed d) x -> Field (ann t)
fieldA x -> t -> t
update = Field t -> Field (ann t)
forall (ann :: * -> *) t.
Applicative ann =>
Field t -> Field (ann t)
liftFieldA (Field t -> Field (ann t))
-> (Decode (Closed d) x -> Field t)
-> Decode (Closed d) x
-> Field (ann t)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (x -> t -> t) -> Decode (Closed d) x -> Field t
forall x t (d :: Density).
Typeable x =>
(x -> t -> t) -> Decode (Closed d) x -> Field t
field x -> t -> t
update
{-# INLINE ofieldA #-}
ofieldA ::
(Typeable x, Applicative ann) =>
(StrictMaybe x -> t -> t) ->
Decode (Closed d) x ->
Field (ann t)
ofieldA :: forall x (ann :: * -> *) t (d :: Density).
(Typeable x, Applicative ann) =>
(StrictMaybe x -> t -> t) -> Decode (Closed d) x -> Field (ann t)
ofieldA StrictMaybe x -> t -> t
update = Field t -> Field (ann t)
forall (ann :: * -> *) t.
Applicative ann =>
Field t -> Field (ann t)
liftFieldA (Field t -> Field (ann t))
-> (Decode (Closed d) x -> Field t)
-> Decode (Closed d) x
-> Field (ann t)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (StrictMaybe x -> t -> t) -> Decode (Closed d) x -> Field t
forall x t (d :: Density).
Typeable x =>
(StrictMaybe x -> t -> t) -> Decode (Closed d) x -> Field t
ofield StrictMaybe x -> t -> t
update
{-# INLINE fieldAGuarded #-}
fieldAGuarded ::
(Typeable x, Applicative ann) =>
String ->
(x -> Bool) ->
(x -> t -> t) ->
Decode (Closed d) x ->
Field (ann t)
fieldAGuarded :: forall x (ann :: * -> *) t (d :: Density).
(Typeable x, Applicative ann) =>
String
-> (x -> Bool)
-> (x -> t -> t)
-> Decode (Closed d) x
-> Field (ann t)
fieldAGuarded String
failMsg x -> Bool
check x -> t -> t
update =
Field t -> Field (ann t)
forall (ann :: * -> *) t.
Applicative ann =>
Field t -> Field (ann t)
liftFieldA (Field t -> Field (ann t))
-> (Decode (Closed d) x -> Field t)
-> Decode (Closed d) x
-> Field (ann t)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String
-> (x -> Bool) -> (x -> t -> t) -> Decode (Closed d) x -> Field t
forall x t (d :: Density).
Typeable x =>
String
-> (x -> Bool) -> (x -> t -> t) -> Decode (Closed d) x -> Field t
fieldGuarded String
failMsg x -> Bool
check x -> t -> t
update
fieldAA ::
(Typeable x, Typeable ann, Applicative ann) =>
(x -> t -> t) ->
Decode (Closed d) (ann x) ->
Field (ann t)
fieldAA :: forall x (ann :: * -> *) t (d :: Density).
(Typeable x, Typeable ann, Applicative ann) =>
(x -> t -> t) -> Decode (Closed d) (ann x) -> Field (ann t)
fieldAA x -> t -> t
update Decode (Closed d) (ann x)
dec = (ann x -> ann t -> ann t)
-> (forall s. Decoder s (ann x)) -> Field (ann t)
forall x t. (x -> t -> t) -> (forall s. Decoder s x) -> Field t
Field ((x -> t -> t) -> ann x -> ann t -> ann t
forall a b c. (a -> b -> c) -> ann a -> ann b -> ann c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 x -> t -> t
update) (Decode (Closed d) (ann x) -> Decoder s (ann x)
forall t (w :: Wrapped) s. Typeable t => Decode w t -> Decoder s t
decode Decode (Closed d) (ann x)
dec)
{-# INLINE fieldAA #-}
liftFieldA ::
Applicative ann =>
Field t ->
Field (ann t)
liftFieldA :: forall (ann :: * -> *) t.
Applicative ann =>
Field t -> Field (ann t)
liftFieldA (Field x -> t -> t
update forall s. Decoder s x
dec) =
(ann x -> ann t -> ann t)
-> (forall s. Decoder s (ann x)) -> Field (ann t)
forall x t. (x -> t -> t) -> (forall s. Decoder s x) -> Field t
Field ((x -> t -> t) -> ann x -> ann t -> ann t
forall a b c. (a -> b -> c) -> ann a -> ann b -> ann c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 x -> t -> t
update) (x -> ann x
forall a. a -> ann a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (x -> ann x) -> Decoder s x -> Decoder s (ann x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s x
forall s. Decoder s x
dec)
data Decode (w :: Wrapped) t where
RecD :: t -> Decode (Closed Dense) t
SumD :: t -> Decode Open t
Summands :: Text.Text -> (Word -> Decode Open t) -> Decode (Closed Dense) t
SparseKeyed ::
Typeable t =>
String ->
t ->
(Word -> Field t) ->
[(Word, String)] ->
Decode (Closed Dense) t
KeyedD :: t -> Decode (Closed Sparse) t
From :: DecCBOR t => Decode w t
FromGroup :: (EncCBORGroup t, DecCBORGroup t) => Decode w t
D :: (forall s. Decoder s t) -> Decode (Closed Dense) t
ApplyD :: Typeable a => Decode w1 (a -> t) -> Decode (Closed d) a -> Decode w1 t
Invalid :: Word -> Decode w t
Map :: Typeable a => (a -> b) -> Decode w a -> Decode w b
TagD :: Word -> Decode (Closed x) t -> Decode (Closed x) t
Emit :: t -> Decode w t
Ann :: Typeable t => Decode w t -> Decode w (Annotator t)
ApplyAnn ::
(Typeable a, Typeable t) =>
Decode w1 (Annotator (a -> t)) ->
Decode (Closed d) (Annotator a) ->
Decode w1 (Annotator t)
ApplyErr :: Typeable a => Decode w1 (a -> Either String t) -> Decode (Closed d) a -> Decode w1 t
infixl 4 <!
infixl 4 <*!
infixl 4 <?
(<!) :: Typeable a => Decode w1 (a -> t) -> Decode (Closed w) a -> Decode w1 t
Decode w1 (a -> t)
x <! :: forall a (w1 :: Wrapped) t (w :: Density).
Typeable a =>
Decode w1 (a -> t) -> Decode (Closed w) a -> Decode w1 t
<! Decode (Closed w) a
y = Decode w1 (a -> t) -> Decode (Closed w) a -> Decode w1 t
forall a (w1 :: Wrapped) t (w :: Density).
Typeable a =>
Decode w1 (a -> t) -> Decode (Closed w) a -> Decode w1 t
ApplyD Decode w1 (a -> t)
x Decode (Closed w) a
y
{-# INLINE (<!) #-}
(<*!) ::
(Typeable a, Typeable t) =>
Decode w1 (Annotator (a -> t)) -> Decode (Closed d) (Annotator a) -> Decode w1 (Annotator t)
Decode w1 (Annotator (a -> t))
x <*! :: forall a t (w1 :: Wrapped) (d :: Density).
(Typeable a, Typeable t) =>
Decode w1 (Annotator (a -> t))
-> Decode (Closed d) (Annotator a) -> Decode w1 (Annotator t)
<*! Decode (Closed d) (Annotator a)
y = Decode w1 (Annotator (a -> t))
-> Decode (Closed d) (Annotator a) -> Decode w1 (Annotator t)
forall a t (w1 :: Wrapped) (d :: Density).
(Typeable a, Typeable t) =>
Decode w1 (Annotator (a -> t))
-> Decode (Closed d) (Annotator a) -> Decode w1 (Annotator t)
ApplyAnn Decode w1 (Annotator (a -> t))
x Decode (Closed d) (Annotator a)
y
{-# INLINE (<*!) #-}
(<?) :: Typeable a => Decode w1 (a -> Either String t) -> Decode (Closed d) a -> Decode w1 t
Decode w1 (a -> Either String t)
f <? :: forall a (w1 :: Wrapped) t (d :: Density).
Typeable a =>
Decode w1 (a -> Either String t)
-> Decode (Closed d) a -> Decode w1 t
<? Decode (Closed d) a
y = Decode w1 (a -> Either String t)
-> Decode (Closed d) a -> Decode w1 t
forall a (w1 :: Wrapped) t (d :: Density).
Typeable a =>
Decode w1 (a -> Either String t)
-> Decode (Closed d) a -> Decode w1 t
ApplyErr Decode w1 (a -> Either String t)
f Decode (Closed d) a
y
{-# INLINE (<?) #-}
hsize :: forall w t. Decode w t -> Int
hsize :: forall (w :: Wrapped) t. Decode w t -> Int
hsize (Summands Text
_ Word -> Decode Open t
_) = Int
1
hsize (SumD t
_) = Int
0
hsize (RecD t
_) = Int
0
hsize (KeyedD t
_) = Int
0
hsize Decode w t
From = Int
1
hsize Decode w t
FromGroup = Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word -> Int) -> Word -> Int
forall a b. (a -> b) -> a -> b
$ Proxy t -> Word
forall a. EncCBORGroup a => Proxy a -> Word
listLenBound (Proxy t -> Word) -> Proxy t -> Word
forall a b. (a -> b) -> a -> b
$ forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @t
hsize (D forall s. Decoder s t
_) = Int
1
hsize (ApplyD Decode w (a -> t)
f Decode (Closed d) a
x) = Decode w (a -> t) -> Int
forall (w :: Wrapped) t. Decode w t -> Int
hsize Decode w (a -> t)
f Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Decode (Closed d) a -> Int
forall (w :: Wrapped) t. Decode w t -> Int
hsize Decode (Closed d) a
x
hsize (Invalid Word
_) = Int
0
hsize (Map a -> t
_ Decode w a
x) = Decode w a -> Int
forall (w :: Wrapped) t. Decode w t -> Int
hsize Decode w a
x
hsize (Emit t
_) = Int
0
hsize SparseKeyed {} = Int
1
hsize (TagD Word
_ Decode (Closed x) t
_) = Int
1
hsize (Ann Decode w t
x) = Decode w t -> Int
forall (w :: Wrapped) t. Decode w t -> Int
hsize Decode w t
x
hsize (ApplyAnn Decode w (Annotator (a -> t))
f Decode (Closed d) (Annotator a)
x) = Decode w (Annotator (a -> t)) -> Int
forall (w :: Wrapped) t. Decode w t -> Int
hsize Decode w (Annotator (a -> t))
f Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Decode (Closed d) (Annotator a) -> Int
forall (w :: Wrapped) t. Decode w t -> Int
hsize Decode (Closed d) (Annotator a)
x
hsize (ApplyErr Decode w (a -> Either String t)
f Decode (Closed d) a
x) = Decode w (a -> Either String t) -> Int
forall (w :: Wrapped) t. Decode w t -> Int
hsize Decode w (a -> Either String t)
f Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Decode (Closed d) a -> Int
forall (w :: Wrapped) t. Decode w t -> Int
hsize Decode (Closed d) a
x
{-# INLINE hsize #-}
decode :: Typeable t => Decode w t -> Decoder s t
decode :: forall t (w :: Wrapped) s. Typeable t => Decode w t -> Decoder s t
decode Decode w t
x = ((Int, t) -> t) -> Decoder s (Int, t) -> Decoder s t
forall a b. (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int, t) -> t
forall a b. (a, b) -> b
snd (Decode w t -> Decoder s (Int, t)
forall t (w :: Wrapped) s.
Typeable t =>
Decode w t -> Decoder s (Int, t)
decodE Decode w t
x)
{-# INLINE decode #-}
decodE :: Typeable t => Decode w t -> Decoder s (Int, t)
decodE :: forall t (w :: Wrapped) s.
Typeable t =>
Decode w t -> Decoder s (Int, t)
decodE Decode w t
x = Decode w t -> Int -> Decoder s (Int, t)
forall (w :: Wrapped) s t.
Typeable t =>
Decode w t -> Int -> Decoder s (Int, t)
decodeCount Decode w t
x Int
0
{-# INLINE decodE #-}
decodeCount :: forall (w :: Wrapped) s t. Typeable t => Decode w t -> Int -> Decoder s (Int, t)
decodeCount :: forall (w :: Wrapped) s t.
Typeable t =>
Decode w t -> Int -> Decoder s (Int, t)
decodeCount (Summands Text
nm Word -> Decode Open t
f) Int
n = (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1,) (t -> (Int, t)) -> Decoder s t -> Decoder s (Int, t)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> (Word -> Decoder s (Int, t)) -> Decoder s t
forall s a. Text -> (Word -> Decoder s (Int, a)) -> Decoder s a
decodeRecordSum Text
nm (Decode Open t -> Decoder s (Int, t)
forall t (w :: Wrapped) s.
Typeable t =>
Decode w t -> Decoder s (Int, t)
decodE (Decode Open t -> Decoder s (Int, t))
-> (Word -> Decode Open t) -> Word -> Decoder s (Int, t)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Decode Open t
f)
decodeCount (SumD t
cn) Int
n = (Int, t) -> Decoder s (Int, t)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1, t
cn)
decodeCount (KeyedD t
cn) Int
n = (Int, t) -> Decoder s (Int, t)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1, t
cn)
decodeCount (RecD t
cn) Int
n = Text
-> ((Int, t) -> Int) -> Decoder s (Int, t) -> Decoder s (Int, t)
forall a s. Text -> (a -> Int) -> Decoder s a -> Decoder s a
decodeRecordNamed Text
"RecD" (Int -> (Int, t) -> Int
forall a b. a -> b -> a
const Int
n) ((Int, t) -> Decoder s (Int, t)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
n, t
cn))
decodeCount Decode w t
From Int
n = (Int
n,) (t -> (Int, t)) -> Decoder s t -> Decoder s (Int, t)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s t
forall s. Decoder s t
forall a s. DecCBOR a => Decoder s a
decCBOR
decodeCount Decode w t
FromGroup Int
n = (Int
n,) (t -> (Int, t)) -> Decoder s t -> Decoder s (Int, t)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s t
forall s. Decoder s t
forall a s. DecCBORGroup a => Decoder s a
decCBORGroup
decodeCount (D forall s. Decoder s t
dec) Int
n = (Int
n,) (t -> (Int, t)) -> Decoder s t -> Decoder s (Int, t)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s t
forall s. Decoder s t
dec
decodeCount (Invalid Word
k) Int
_ = Word -> Decoder s (Int, t)
forall a (m :: * -> *). (Typeable a, MonadFail m) => Word -> m a
invalidKey Word
k
decodeCount (Map a -> t
f Decode w a
x) Int
n = do (m, y) <- Decode w a -> Int -> Decoder s (Int, a)
forall (w :: Wrapped) s t.
Typeable t =>
Decode w t -> Int -> Decoder s (Int, t)
decodeCount Decode w a
x Int
n; pure (m, f y)
decodeCount (Emit t
x) Int
n = (Int, t) -> Decoder s (Int, t)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
n, t
x)
decodeCount (TagD Word
expectedTag Decode (Closed x) t
decoder) Int
n = do
Word -> Decoder s ()
forall s. Word -> Decoder s ()
assertTag Word
expectedTag
Decode (Closed x) t -> Int -> Decoder s (Int, t)
forall (w :: Wrapped) s t.
Typeable t =>
Decode w t -> Int -> Decoder s (Int, t)
decodeCount Decode (Closed x) t
decoder Int
n
decodeCount (SparseKeyed String
name t
initial Word -> Field t
pick [(Word, String)]
required) Int
n =
(Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1,) (t -> (Int, t)) -> Decoder s t -> Decoder s (Int, t)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> t -> (Word -> Field t) -> [(Word, String)] -> Decoder s t
forall a s.
Typeable a =>
String -> a -> (Word -> Field a) -> [(Word, String)] -> Decoder s a
decodeSparse String
name t
initial Word -> Field t
pick [(Word, String)]
required
decodeCount (Ann Decode w t
x) Int
n = do (m, y) <- Decode w t -> Int -> Decoder s (Int, t)
forall (w :: Wrapped) s t.
Typeable t =>
Decode w t -> Int -> Decoder s (Int, t)
decodeCount Decode w t
x Int
n; pure (m, pure y)
decodeCount (ApplyAnn Decode w (Annotator (a -> t))
g Decode (Closed d) (Annotator a)
x) Int
n = do
(i, f) <- Decode w (Annotator (a -> t))
-> Int -> Decoder s (Int, Annotator (a -> t))
forall (w :: Wrapped) s t.
Typeable t =>
Decode w t -> Int -> Decoder s (Int, t)
decodeCount Decode w (Annotator (a -> t))
g (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Decode (Closed d) (Annotator a) -> Int
forall (w :: Wrapped) t. Decode w t -> Int
hsize Decode (Closed d) (Annotator a)
x)
y <- decodeClosed x
pure (i, f <*> y)
decodeCount (ApplyD Decode w (a -> t)
cn Decode (Closed d) a
g) Int
n = do
(i, f) <- Decode w (a -> t) -> Int -> Decoder s (Int, a -> t)
forall (w :: Wrapped) s t.
Typeable t =>
Decode w t -> Int -> Decoder s (Int, t)
decodeCount Decode w (a -> t)
cn (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Decode (Closed d) a -> Int
forall (w :: Wrapped) t. Decode w t -> Int
hsize Decode (Closed d) a
g)
y <- decodeClosed g
pure (i, f y)
decodeCount (ApplyErr Decode w (a -> Either String t)
cn Decode (Closed d) a
g) Int
n = do
(i, f) <- Decode w (a -> Either String t)
-> Int -> Decoder s (Int, a -> Either String t)
forall (w :: Wrapped) s t.
Typeable t =>
Decode w t -> Int -> Decoder s (Int, t)
decodeCount Decode w (a -> Either String t)
cn (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Decode (Closed d) a -> Int
forall (w :: Wrapped) t. Decode w t -> Int
hsize Decode (Closed d) a
g)
y <- decodeClosed g
case f y of
Right t
z -> (Int, t) -> Decoder s (Int, t)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
i, t
z)
Left String
message -> DecoderError -> Decoder s (Int, t)
forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError (DecoderError -> Decoder s (Int, t))
-> DecoderError -> Decoder s (Int, t)
forall a b. (a -> b) -> a -> b
$ Text -> Text -> DecoderError
DecoderErrorCustom Text
"decoding error:" (String -> Text
Text.pack String
message)
{-# INLINE decodeCount #-}
decodeClosed :: Typeable t => Decode (Closed d) t -> Decoder s t
decodeClosed :: forall t (d :: Density) s.
Typeable t =>
Decode (Closed d) t -> Decoder s t
decodeClosed (Summands Text
nm Word -> Decode Open t
f) = Text -> (Word -> Decoder s (Int, t)) -> Decoder s t
forall s a. Text -> (Word -> Decoder s (Int, a)) -> Decoder s a
decodeRecordSum Text
nm (Decode Open t -> Decoder s (Int, t)
forall t (w :: Wrapped) s.
Typeable t =>
Decode w t -> Decoder s (Int, t)
decodE (Decode Open t -> Decoder s (Int, t))
-> (Word -> Decode Open t) -> Word -> Decoder s (Int, t)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Decode Open t
f)
decodeClosed (KeyedD t
cn) = t -> Decoder s t
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure t
cn
decodeClosed (RecD t
cn) = t -> Decoder s t
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure t
cn
decodeClosed Decode (Closed d) t
From = Decoder s t
forall s. Decoder s t
forall a s. DecCBOR a => Decoder s a
decCBOR
decodeClosed Decode (Closed d) t
FromGroup = Decoder s t
forall s. Decoder s t
forall a s. DecCBORGroup a => Decoder s a
decCBORGroup
decodeClosed (D forall s. Decoder s t
dec) = Decoder s t
forall s. Decoder s t
dec
decodeClosed (ApplyD Decode (Closed d) (a -> t)
cn Decode (Closed d) a
g) = do
f <- Decode (Closed d) (a -> t) -> Decoder s (a -> t)
forall t (d :: Density) s.
Typeable t =>
Decode (Closed d) t -> Decoder s t
decodeClosed Decode (Closed d) (a -> t)
cn
y <- decodeClosed g
pure (f y)
decodeClosed (Invalid Word
k) = Word -> Decoder s t
forall a (m :: * -> *). (Typeable a, MonadFail m) => Word -> m a
invalidKey Word
k
decodeClosed (Map a -> t
f Decode (Closed d) a
x) = a -> t
f (a -> t) -> Decoder s a -> Decoder s t
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decode (Closed d) a -> Decoder s a
forall t (d :: Density) s.
Typeable t =>
Decode (Closed d) t -> Decoder s t
decodeClosed Decode (Closed d) a
x
decodeClosed (Emit t
n) = t -> Decoder s t
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure t
n
decodeClosed (TagD Word
expectedTag Decode (Closed x) t
decoder) = do
Word -> Decoder s ()
forall s. Word -> Decoder s ()
assertTag Word
expectedTag
Decode (Closed x) t -> Decoder s t
forall t (d :: Density) s.
Typeable t =>
Decode (Closed d) t -> Decoder s t
decodeClosed Decode (Closed x) t
decoder
decodeClosed (SparseKeyed String
name t
initial Word -> Field t
pick [(Word, String)]
required) =
String -> t -> (Word -> Field t) -> [(Word, String)] -> Decoder s t
forall a s.
Typeable a =>
String -> a -> (Word -> Field a) -> [(Word, String)] -> Decoder s a
decodeSparse String
name t
initial Word -> Field t
pick [(Word, String)]
required
decodeClosed (Ann Decode (Closed d) t
x) = (t -> t) -> Decoder s t -> Decoder s t
forall a b. (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap t -> t
t -> Annotator t
forall a. a -> Annotator a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Decode (Closed d) t -> Decoder s t
forall t (d :: Density) s.
Typeable t =>
Decode (Closed d) t -> Decoder s t
decodeClosed Decode (Closed d) t
x)
decodeClosed (ApplyAnn Decode (Closed d) (Annotator (a -> t))
g Decode (Closed d) (Annotator a)
x) = do
f <- Decode (Closed d) (Annotator (a -> t))
-> Decoder s (Annotator (a -> t))
forall t (d :: Density) s.
Typeable t =>
Decode (Closed d) t -> Decoder s t
decodeClosed Decode (Closed d) (Annotator (a -> t))
g
y <- decodeClosed x
pure (f <*> y)
decodeClosed (ApplyErr Decode (Closed d) (a -> Either String t)
cn Decode (Closed d) a
g) = do
f <- Decode (Closed d) (a -> Either String t)
-> Decoder s (a -> Either String t)
forall t (d :: Density) s.
Typeable t =>
Decode (Closed d) t -> Decoder s t
decodeClosed Decode (Closed d) (a -> Either String t)
cn
y <- decodeClosed g
case f y of
Right t
z -> t -> Decoder s t
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure t
z
Left String
message -> DecoderError -> Decoder s t
forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError (DecoderError -> Decoder s t) -> DecoderError -> Decoder s t
forall a b. (a -> b) -> a -> b
$ Text -> Text -> DecoderError
DecoderErrorCustom Text
"decoding error:" (String -> Text
Text.pack String
message)
{-# INLINE decodeClosed #-}
decodeSparse ::
Typeable a =>
String ->
a ->
(Word -> Field a) ->
[(Word, String)] ->
Decoder s a
decodeSparse :: forall a s.
Typeable a =>
String -> a -> (Word -> Field a) -> [(Word, String)] -> Decoder s a
decodeSparse String
name a
initial Word -> Field a
pick [(Word, String)]
required = do
lenOrIndef <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
decodeMapLenOrIndef
(!v, used) <- case lenOrIndef of
Just Int
len -> Int
-> a
-> (Word -> Field a)
-> Set Word
-> String
-> Decoder s (a, Set Word)
forall t s.
Int
-> t
-> (Word -> Field t)
-> Set Word
-> String
-> Decoder s (t, Set Word)
getSparseBlock Int
len a
initial Word -> Field a
pick Set Word
forall a. Set a
Set.empty String
name
Maybe Int
Nothing -> a
-> (Word -> Field a)
-> Set Word
-> String
-> Decoder s (a, Set Word)
forall t s.
t
-> (Word -> Field t)
-> Set Word
-> String
-> Decoder s (t, Set Word)
getSparseBlockIndef a
initial Word -> Field a
pick Set Word
forall a. Set a
Set.empty String
name
if all (\(Word
key, String
_name) -> Word -> Set Word -> Bool
forall a. Ord a => a -> Set a -> Bool
member Word
key Set Word
used) required
then pure v
else unusedRequiredKeys used required (show (typeOf initial))
{-# INLINE decodeSparse #-}
applyField :: (Word -> Field t) -> Set Word -> String -> Decoder s (t -> t, Set Word)
applyField :: forall t s.
(Word -> Field t)
-> Set Word -> String -> Decoder s (t -> t, Set Word)
applyField Word -> Field t
f Set Word
seen String
name = do
tag <- Decoder s Word
forall s. Decoder s Word
decodeWord
if Set.member tag seen
then duplicateKey name tag
else case f tag of
Field x -> t -> t
update forall s. Decoder s x
d -> Decoder s x
forall s. Decoder s x
d Decoder s x
-> (x -> Decoder s (t -> t, Set Word))
-> Decoder s (t -> t, Set Word)
forall a b. Decoder s a -> (a -> Decoder s b) -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \x
v -> (t -> t, Set Word) -> Decoder s (t -> t, Set Word)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (x -> t -> t
update x
v, Word -> Set Word -> Set Word
forall a. Ord a => a -> Set a -> Set a
insert Word
tag Set Word
seen)
{-# INLINE applyField #-}
getSparseBlock :: Int -> t -> (Word -> Field t) -> Set Word -> String -> Decoder s (t, Set Word)
getSparseBlock :: forall t s.
Int
-> t
-> (Word -> Field t)
-> Set Word
-> String
-> Decoder s (t, Set Word)
getSparseBlock Int
0 t
initial Word -> Field t
_pick Set Word
seen String
_name = (t, Set Word) -> Decoder s (t, Set Word)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (t
initial, Set Word
seen)
getSparseBlock Int
n t
initial Word -> Field t
pick Set Word
seen String
name = do
(transform, seen2) <- (Word -> Field t)
-> Set Word -> String -> Decoder s (t -> t, Set Word)
forall t s.
(Word -> Field t)
-> Set Word -> String -> Decoder s (t -> t, Set Word)
applyField Word -> Field t
pick Set Word
seen String
name
getSparseBlock (n - 1) (transform initial) pick seen2 name
{-# INLINE getSparseBlock #-}
getSparseBlockIndef :: t -> (Word -> Field t) -> Set Word -> String -> Decoder s (t, Set Word)
getSparseBlockIndef :: forall t s.
t
-> (Word -> Field t)
-> Set Word
-> String
-> Decoder s (t, Set Word)
getSparseBlockIndef t
initial Word -> Field t
pick Set Word
seen String
name =
Decoder s Bool
forall s. Decoder s Bool
decodeBreakOr Decoder s Bool
-> (Bool -> Decoder s (t, Set Word)) -> Decoder s (t, Set Word)
forall a b. Decoder s a -> (a -> Decoder s b) -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Bool
True -> (t, Set Word) -> Decoder s (t, Set Word)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (t
initial, Set Word
seen)
Bool
False -> do
(transform, seen2) <- (Word -> Field t)
-> Set Word -> String -> Decoder s (t -> t, Set Word)
forall t s.
(Word -> Field t)
-> Set Word -> String -> Decoder s (t -> t, Set Word)
applyField Word -> Field t
pick Set Word
seen String
name
getSparseBlockIndef (transform initial) pick seen2 name
{-# INLINE getSparseBlockIndef #-}
mapCoder :: Typeable a => (a -> b) -> Decode w a -> Decode w b
mapCoder :: forall a b (w :: Wrapped).
Typeable a =>
(a -> b) -> Decode w a -> Decode w b
mapCoder a -> b
f (Map a -> a
g Decode w a
x) = (a -> b) -> Decode w a -> Decode w b
forall a b (w :: Wrapped).
Typeable a =>
(a -> b) -> Decode w a -> Decode w b
Map (a -> b
f (a -> b) -> (a -> a) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
g) Decode w a
x
mapCoder a -> b
f Decode w a
x = (a -> b) -> Decode w a -> Decode w b
forall a b (w :: Wrapped).
Typeable a =>
(a -> b) -> Decode w a -> Decode w b
Map a -> b
f Decode w a
x
{-# INLINE mapCoder #-}
decodeDual :: forall t. (EncCBOR t, DecCBOR t) => Decode (Closed Dense) t
decodeDual :: forall t. (EncCBOR t, DecCBOR t) => Decode (Closed Dense) t
decodeDual = (forall s. Decoder s t) -> Decode (Closed Dense) t
forall t. (forall s. Decoder s t) -> Decode (Closed Dense) t
D Decoder s t
forall s. Decoder s t
forall a s. DecCBOR a => Decoder s a
decCBOR
where
_encCBOR :: Encoding
_encCBOR = t -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR (t
forall a. HasCallStack => a
undefined :: t)
{-# INLINE decodeDual #-}
listDecodeA ::
Typeable x => Decode (Closed Dense) (Annotator x) -> Decode (Closed Dense) (Annotator [x])
listDecodeA :: forall x.
Typeable x =>
Decode (Closed Dense) (Annotator x)
-> Decode (Closed Dense) (Annotator [x])
listDecodeA Decode (Closed Dense) (Annotator x)
dx = (forall s. Decoder s (Annotator [x]))
-> Decode (Closed Dense) (Annotator [x])
forall t. (forall s. Decoder s t) -> Decode (Closed Dense) t
D ([Annotator x] -> Annotator [x]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence ([Annotator x] -> Annotator [x])
-> Decoder s [Annotator x] -> Decoder s (Annotator [x])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Annotator x) -> Decoder s [Annotator x]
forall s a. Decoder s a -> Decoder s [a]
decodeList (Decode (Closed Dense) (Annotator x) -> Decoder s (Annotator x)
forall t (w :: Wrapped) s. Typeable t => Decode w t -> Decoder s t
decode Decode (Closed Dense) (Annotator x)
dx))
{-# INLINE listDecodeA #-}
setDecodeA ::
(Ord x, Typeable x) =>
Decode (Closed Dense) (Annotator x) ->
Decode (Closed Dense) (Annotator (Set x))
setDecodeA :: forall x.
(Ord x, Typeable x) =>
Decode (Closed Dense) (Annotator x)
-> Decode (Closed Dense) (Annotator (Set x))
setDecodeA Decode (Closed Dense) (Annotator x)
dx = (forall s. Decoder s (Annotator (Set x)))
-> Decode (Closed Dense) (Annotator (Set x))
forall t. (forall s. Decoder s t) -> Decode (Closed Dense) t
D (Decoder s (Annotator x) -> Decoder s (Annotator (Set x))
forall t s.
Ord t =>
Decoder s (Annotator t) -> Decoder s (Annotator (Set t))
decodeAnnSet (Decode (Closed Dense) (Annotator x) -> Decoder s (Annotator x)
forall t (w :: Wrapped) s. Typeable t => Decode w t -> Decoder s t
decode Decode (Closed Dense) (Annotator x)
dx))
{-# INLINE setDecodeA #-}
mapDecodeA ::
(Ord k, Typeable k, Typeable v) =>
Decode (Closed Dense) (Annotator k) ->
Decode (Closed Dense) (Annotator v) ->
Decode (Closed Dense) (Annotator (Map.Map k v))
mapDecodeA :: forall k v.
(Ord k, Typeable k, Typeable v) =>
Decode (Closed Dense) (Annotator k)
-> Decode (Closed Dense) (Annotator v)
-> Decode (Closed Dense) (Annotator (Map k v))
mapDecodeA Decode (Closed Dense) (Annotator k)
k Decode (Closed Dense) (Annotator v)
v = (forall s. Decoder s (Annotator (Map k v)))
-> Decode (Closed Dense) (Annotator (Map k v))
forall t. (forall s. Decoder s t) -> Decode (Closed Dense) t
D (Decoder s (Annotator k)
-> Decoder s (Annotator v) -> Decoder s (Annotator (Map k v))
forall a (t :: * -> *) s b.
(Ord a, Applicative t) =>
Decoder s (t a) -> Decoder s (t b) -> Decoder s (t (Map a b))
decodeMapTraverse (Decode (Closed Dense) (Annotator k) -> Decoder s (Annotator k)
forall t (w :: Wrapped) s. Typeable t => Decode w t -> Decoder s t
decode Decode (Closed Dense) (Annotator k)
k) (Decode (Closed Dense) (Annotator v) -> Decoder s (Annotator v)
forall t (w :: Wrapped) s. Typeable t => Decode w t -> Decoder s t
decode Decode (Closed Dense) (Annotator v)
v))
{-# INLINE mapDecodeA #-}
duplicateKey :: String -> Word -> Decoder s a
duplicateKey :: forall s a. String -> Word -> Decoder s a
duplicateKey String
name Word
k =
DecoderError -> Decoder s a
forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError (DecoderError -> Decoder s a) -> DecoderError -> Decoder s a
forall a b. (a -> b) -> a -> b
$
Text -> Text -> DecoderError
DecoderErrorCustom
Text
"Duplicate key:"
(String -> Text
Text.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Word -> String
forall a. Show a => a -> String
show Word
k String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" while decoding type " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
name)
{-# NOINLINE duplicateKey #-}
unusedRequiredKeys :: Set Word -> [(Word, String)] -> String -> Decoder s a
unusedRequiredKeys :: forall s a. Set Word -> [(Word, String)] -> String -> Decoder s a
unusedRequiredKeys Set Word
used [(Word, String)]
required String
name =
DecoderError -> Decoder s a
forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError (DecoderError -> Decoder s a) -> DecoderError -> Decoder s a
forall a b. (a -> b) -> a -> b
$
Text -> Text -> DecoderError
DecoderErrorCustom
(String -> Text
Text.pack (String
"value of type " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
name))
(String -> Text
Text.pack ([(Word, String)] -> String
forall {a}. Show a => [(a, String)] -> String
message (((Word, String) -> Bool) -> [(Word, String)] -> [(Word, String)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Word, String) -> Bool
bad [(Word, String)]
required)))
where
bad :: (Word, String) -> Bool
bad (Word
k, String
_) = Bool -> Bool
not (Word -> Set Word -> Bool
forall a. Ord a => a -> Set a -> Bool
member Word
k Set Word
used)
message :: [(a, String)] -> String
message [] = String
", not decoded."
message [(a, String)
pair] = (a, String) -> String
forall {a}. Show a => (a, String) -> String
report (a, String)
pair String -> String -> String
forall a. [a] -> [a] -> [a]
++ [(a, String)] -> String
message []
message ((a, String)
pair : [(a, String)]
more) = (a, String) -> String
forall {a}. Show a => (a, String) -> String
report (a, String)
pair String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
", and " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [(a, String)] -> String
message [(a, String)]
more
report :: (a, String) -> String
report (a
k, String
f) = String
"field " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
f String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" with key " String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
k
{-# NOINLINE unusedRequiredKeys #-}
guardUntilAtLeast :: DecCBOR a => String -> Version -> Decode (Closed Dense) a
guardUntilAtLeast :: forall a. DecCBOR a => String -> Version -> Decode (Closed Dense) a
guardUntilAtLeast String
errMessage Version
v = (forall s. Decoder s a) -> Decode (Closed Dense) a
forall t. (forall s. Decoder s t) -> Decode (Closed Dense) t
D (Version -> Decoder s (ZonkAny 0) -> Decoder s ()
forall s a. Version -> Decoder s a -> Decoder s ()
unlessDecoderVersionAtLeast Version
v (String -> Decoder s (ZonkAny 0)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
errMessage) Decoder s () -> Decoder s a -> Decoder s a
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Decoder s a
forall s. Decoder s a
forall a s. DecCBOR a => Decoder s a
decCBOR)
{-# INLINE guardUntilAtLeast #-}