{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Cardano.Ledger.Binary.Decoding.Decoder (
Decoder,
toPlainDecoder,
fromPlainDecoder,
withPlainDecoder,
enforceDecoderVersion,
getOriginalBytes,
DecoderError (..),
C.ByteOffset,
C.DecodeAction (..),
C.TokenType (..),
getDecoderVersion,
ifDecoderVersionAtLeast,
whenDecoderVersionAtLeast,
unlessDecoderVersionAtLeast,
cborError,
toCborError,
showDecoderError,
invalidKey,
assertTag,
enforceSize,
matchSize,
binaryGetDecoder,
allowTag,
decodeVersion,
decodeRational,
decodeRationalWithTag,
decodeRecordNamed,
decodeRecordNamedT,
decodeRecordSum,
decodeListLike,
decodeListLikeT,
decodeEnumBounded,
decodeWithOrigin,
decodeMaybe,
decodeNullMaybe,
decodeStrictMaybe,
decodeNullStrictMaybe,
decodeEither,
decodeList,
decodeNonEmptyList,
decodeVector,
decodeSet,
setTag,
decodeMap,
decodeMapByKey,
decodeMapLikeEnforceNoDuplicates,
decodeVMap,
decodeSeq,
decodeStrictSeq,
decodeSetTag,
decodeListLikeWithCount,
decodeSetLikeEnforceNoDuplicates,
decodeListLikeEnforceNoDuplicates,
decodeMapContents,
decodeMapTraverse,
decodeMapContentsTraverse,
decodeUTCTime,
decodeIPv4,
decodeIPv6,
decodeBool,
decodeBreakOr,
decodeByteArray,
decodeByteArrayCanonical,
decodeBytes,
decodeBytesCanonical,
decodeBytesIndef,
decodeDouble,
decodeDoubleCanonical,
decodeFloat,
decodeFloat16Canonical,
decodeFloatCanonical,
decodeInt,
decodeInt16,
decodeInt16Canonical,
decodeInt32,
decodeInt32Canonical,
decodeInt64,
decodeInt64Canonical,
decodeInt8,
decodeInt8Canonical,
decodeIntCanonical,
decodeInteger,
decodeIntegerCanonical,
decodeNatural,
decodeListLen,
decodeListLenCanonical,
decodeListLenCanonicalOf,
decodeListLenIndef,
decodeListLenOf,
decodeListLenOrIndef,
decodeMapLen,
decodeMapLenCanonical,
decodeMapLenIndef,
decodeMapLenOrIndef,
decodeNegWord,
decodeNegWord64,
decodeNegWord64Canonical,
decodeNegWordCanonical,
decodeNull,
decodeSequenceLenIndef,
decodeSequenceLenN,
decodeSimple,
decodeSimpleCanonical,
decodeString,
decodeStringCanonical,
decodeStringIndef,
decodeTag,
decodeTag64,
decodeTag64Canonical,
decodeTagCanonical,
decodeUtf8ByteArray,
decodeUtf8ByteArrayCanonical,
decodeWithByteSpan,
decodeWord,
decodeWord16,
decodeWord16Canonical,
decodeWord32,
decodeWord32Canonical,
decodeWord64,
decodeWord64Canonical,
decodeWord8,
decodeWord8Canonical,
decodeWordCanonical,
decodeWordCanonicalOf,
decodeWordOf,
decodeTerm,
peekAvailable,
peekByteOffset,
peekTokenType,
)
where
import Cardano.Ledger.Binary.Plain (
DecoderError (..),
cborError,
invalidKey,
showDecoderError,
toCborError,
)
import Cardano.Ledger.Binary.Version (Version, mkVersion64, natVersion)
import Cardano.Slotting.Slot (WithOrigin, withOriginFromMaybe)
import Codec.CBOR.ByteArray (ByteArray)
import qualified Codec.CBOR.Decoding as C (
ByteOffset,
DecodeAction (..),
Decoder,
TokenType (..),
decodeBool,
decodeBreakOr,
decodeByteArray,
decodeByteArrayCanonical,
decodeBytes,
decodeBytesCanonical,
decodeBytesIndef,
decodeDouble,
decodeDoubleCanonical,
decodeFloat,
decodeFloat16Canonical,
decodeFloatCanonical,
decodeInt,
decodeInt16,
decodeInt16Canonical,
decodeInt32,
decodeInt32Canonical,
decodeInt64,
decodeInt64Canonical,
decodeInt8,
decodeInt8Canonical,
decodeIntCanonical,
decodeInteger,
decodeIntegerCanonical,
decodeListLen,
decodeListLenCanonical,
decodeListLenCanonicalOf,
decodeListLenIndef,
decodeListLenOf,
decodeListLenOrIndef,
decodeMapLen,
decodeMapLenCanonical,
decodeMapLenIndef,
decodeMapLenOrIndef,
decodeNegWord,
decodeNegWord64,
decodeNegWord64Canonical,
decodeNegWordCanonical,
decodeNull,
decodeSequenceLenIndef,
decodeSequenceLenN,
decodeSimple,
decodeSimpleCanonical,
decodeString,
decodeStringCanonical,
decodeStringIndef,
decodeTag,
decodeTag64,
decodeTag64Canonical,
decodeTagCanonical,
decodeUtf8ByteArray,
decodeUtf8ByteArrayCanonical,
decodeWithByteSpan,
decodeWord,
decodeWord16,
decodeWord16Canonical,
decodeWord32,
decodeWord32Canonical,
decodeWord64,
decodeWord64Canonical,
decodeWord8,
decodeWord8Canonical,
decodeWordCanonical,
decodeWordCanonicalOf,
decodeWordOf,
peekAvailable,
peekByteOffset,
peekTokenType,
)
import qualified Codec.CBOR.Term as C (Term (..), decodeTerm)
import Control.Monad
import Control.Monad.Trans (MonadTrans (..))
import Control.Monad.Trans.Identity (IdentityT (runIdentityT))
import Data.Binary.Get (Get, getWord32le, runGetOrFail)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import Data.Functor.Compose (Compose (..))
import Data.IP (IPv4, IPv6, fromHostAddress, fromHostAddress6)
import Data.Int (Int16, Int32, Int64, Int8)
import qualified Data.List.NonEmpty as NE (NonEmpty, nonEmpty)
import qualified Data.Map.Strict as Map
import Data.Maybe.Strict (StrictMaybe (..), maybeToStrictMaybe)
import Data.Ratio ((%))
import qualified Data.Sequence as Seq
import qualified Data.Sequence.Strict as SSeq
import qualified Data.Set as Set
import qualified Data.Text as Text
import Data.Time.Calendar.OrdinalDate (fromOrdinalDate)
import Data.Time.Clock (UTCTime (..), picosecondsToDiffTime)
import Data.Typeable (Proxy (Proxy), Typeable, typeRep)
import qualified Data.VMap as VMap
import qualified Data.Vector.Generic as VG
import Data.Word (Word16, Word32, Word64, Word8)
import Network.Socket (HostAddress6)
import Numeric.Natural (Natural)
import Prelude hiding (decodeFloat)
newtype Decoder s a = Decoder
{ forall s a.
Decoder s a -> Maybe ByteString -> Version -> Decoder s a
runDecoder :: Maybe BSL.ByteString -> Version -> C.Decoder s a
}
instance Functor (Decoder s) where
fmap :: forall a b. (a -> b) -> Decoder s a -> Decoder s b
fmap a -> b
f Decoder s a
d = forall s a.
(Maybe ByteString -> Version -> Decoder s a) -> Decoder s a
Decoder (\Maybe ByteString
bsl Version
v -> a -> b
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s a.
Decoder s a -> Maybe ByteString -> Version -> Decoder s a
runDecoder Decoder s a
d Maybe ByteString
bsl Version
v)
{-# INLINE fmap #-}
instance Applicative (Decoder s) where
pure :: forall a. a -> Decoder s a
pure a
x = forall s a.
(Maybe ByteString -> Version -> Decoder s a) -> Decoder s a
Decoder (\Maybe ByteString
_ Version
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
{-# INLINE pure #-}
Decoder Maybe ByteString -> Version -> Decoder s (a -> b)
f <*> :: forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
<*> Decoder Maybe ByteString -> Version -> Decoder s a
g = forall s a.
(Maybe ByteString -> Version -> Decoder s a) -> Decoder s a
Decoder forall a b. (a -> b) -> a -> b
$ \Maybe ByteString
bsl Version
v -> Maybe ByteString -> Version -> Decoder s (a -> b)
f Maybe ByteString
bsl Version
v forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Maybe ByteString -> Version -> Decoder s a
g Maybe ByteString
bsl Version
v
{-# INLINE (<*>) #-}
Decoder Maybe ByteString -> Version -> Decoder s a
f *> :: forall a b. Decoder s a -> Decoder s b -> Decoder s b
*> Decoder Maybe ByteString -> Version -> Decoder s b
g = forall s a.
(Maybe ByteString -> Version -> Decoder s a) -> Decoder s a
Decoder forall a b. (a -> b) -> a -> b
$ \Maybe ByteString
bsl Version
v -> Maybe ByteString -> Version -> Decoder s a
f Maybe ByteString
bsl Version
v forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Maybe ByteString -> Version -> Decoder s b
g Maybe ByteString
bsl Version
v
{-# INLINE (*>) #-}
instance Monad (Decoder s) where
Decoder Maybe ByteString -> Version -> Decoder s a
f >>= :: forall a b. Decoder s a -> (a -> Decoder s b) -> Decoder s b
>>= a -> Decoder s b
g = forall s a.
(Maybe ByteString -> Version -> Decoder s a) -> Decoder s a
Decoder forall a b. (a -> b) -> a -> b
$ \Maybe ByteString
bsl Version
v -> do
a
x <- Maybe ByteString -> Version -> Decoder s a
f Maybe ByteString
bsl Version
v
forall s a.
Decoder s a -> Maybe ByteString -> Version -> Decoder s a
runDecoder (a -> Decoder s b
g a
x) Maybe ByteString
bsl Version
v
{-# INLINE (>>=) #-}
instance MonadFail (Decoder s) where
fail :: forall a. String -> Decoder s a
fail String
msg = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
msg
{-# INLINE fail #-}
fromPlainDecoder :: C.Decoder s a -> Decoder s a
fromPlainDecoder :: forall s a. Decoder s a -> Decoder s a
fromPlainDecoder Decoder s a
d = forall s a.
(Maybe ByteString -> Version -> Decoder s a) -> Decoder s a
Decoder (\Maybe ByteString
_ Version
_ -> Decoder s a
d)
{-# INLINE fromPlainDecoder #-}
toPlainDecoder ::
Maybe BSL.ByteString ->
Version ->
Decoder s a ->
C.Decoder s a
toPlainDecoder :: forall s a.
Maybe ByteString -> Version -> Decoder s a -> Decoder s a
toPlainDecoder Maybe ByteString
bsl Version
v (Decoder Maybe ByteString -> Version -> Decoder s a
d) = Maybe ByteString -> Version -> Decoder s a
d Maybe ByteString
bsl Version
v
{-# INLINE toPlainDecoder #-}
withPlainDecoder :: Decoder s a -> (C.Decoder s a -> C.Decoder s b) -> Decoder s b
withPlainDecoder :: forall s a b.
Decoder s a -> (Decoder s a -> Decoder s b) -> Decoder s b
withPlainDecoder Decoder s a
vd Decoder s a -> Decoder s b
f = forall s a.
(Maybe ByteString -> Version -> Decoder s a) -> Decoder s a
Decoder forall a b. (a -> b) -> a -> b
$ \Maybe ByteString
bsl -> Decoder s a -> Decoder s b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a.
Decoder s a -> Maybe ByteString -> Version -> Decoder s a
runDecoder Decoder s a
vd Maybe ByteString
bsl
{-# INLINE withPlainDecoder #-}
enforceDecoderVersion :: Version -> Decoder s a -> Decoder s a
enforceDecoderVersion :: forall s a. Version -> Decoder s a -> Decoder s a
enforceDecoderVersion Version
version Decoder s a
d = forall s a.
(Maybe ByteString -> Version -> Decoder s a) -> Decoder s a
Decoder forall a b. (a -> b) -> a -> b
$ \Maybe ByteString
bsl Version
_ -> forall s a.
Decoder s a -> Maybe ByteString -> Version -> Decoder s a
runDecoder Decoder s a
d Maybe ByteString
bsl Version
version
{-# INLINE enforceDecoderVersion #-}
getOriginalBytes :: Decoder s BSL.ByteString
getOriginalBytes :: forall s. Decoder s ByteString
getOriginalBytes =
forall s a.
(Maybe ByteString -> Version -> Decoder s a) -> Decoder s a
Decoder forall a b. (a -> b) -> a -> b
$ \Maybe ByteString
maybeBytes Version
_ ->
case Maybe ByteString
maybeBytes of
Maybe ByteString
Nothing -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Decoder was expected to provide the original ByteString"
Just ByteString
bsl -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
bsl
getDecoderVersion :: Decoder s Version
getDecoderVersion :: forall s. Decoder s Version
getDecoderVersion = forall s a.
(Maybe ByteString -> Version -> Decoder s a) -> Decoder s a
Decoder forall a b. (a -> b) -> a -> b
$ \Maybe ByteString
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# INLINE getDecoderVersion #-}
ifDecoderVersionAtLeast ::
Version ->
Decoder s a ->
Decoder s a ->
Decoder s a
ifDecoderVersionAtLeast :: forall s a. Version -> Decoder s a -> Decoder s a -> Decoder s a
ifDecoderVersionAtLeast Version
atLeast Decoder s a
newerDecoder Decoder s a
olderDecoder = do
Version
cur <- forall s. Decoder s Version
getDecoderVersion
if Version
cur forall a. Ord a => a -> a -> Bool
>= Version
atLeast
then Decoder s a
newerDecoder
else Decoder s a
olderDecoder
{-# INLINE ifDecoderVersionAtLeast #-}
whenDecoderVersionAtLeast ::
Version ->
Decoder s a ->
Decoder s ()
whenDecoderVersionAtLeast :: forall s a. Version -> Decoder s a -> Decoder s ()
whenDecoderVersionAtLeast Version
atLeast Decoder s a
decoder = do
Version
cur <- forall s. Decoder s Version
getDecoderVersion
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Version
cur forall a. Ord a => a -> a -> Bool
>= Version
atLeast) (forall (f :: * -> *) a. Functor f => f a -> f ()
void Decoder s a
decoder)
{-# INLINE whenDecoderVersionAtLeast #-}
unlessDecoderVersionAtLeast ::
Version ->
Decoder s a ->
Decoder s ()
unlessDecoderVersionAtLeast :: forall s a. Version -> Decoder s a -> Decoder s ()
unlessDecoderVersionAtLeast Version
atLeast Decoder s a
decoder = do
Version
cur <- forall s. Decoder s Version
getDecoderVersion
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Version
cur forall a. Ord a => a -> a -> Bool
>= Version
atLeast) (forall (f :: * -> *) a. Functor f => f a -> f ()
void Decoder s a
decoder)
decodeVersion :: Decoder s Version
decodeVersion :: forall s. Decoder s Version
decodeVersion = forall s. Decoder s Word64
decodeWord64 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *). MonadFail m => Word64 -> m Version
mkVersion64
{-# INLINE decodeVersion #-}
decodeRational :: Decoder s Rational
decodeRational :: forall s. Decoder s Rational
decodeRational =
forall s a. Version -> Decoder s a -> Decoder s a -> Decoder s a
ifDecoderVersionAtLeast
(forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @9)
(forall s. Word -> Decoder s ()
allowTag Word
30 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall s. Decoder s Rational
decodeRationalWithoutTag)
( forall s a. Version -> Decoder s a -> Decoder s a -> Decoder s a
ifDecoderVersionAtLeast
(forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @2)
forall s. Decoder s Rational
decodeRationalWithoutTag
forall s. Decoder s Rational
decodeRationalFixedSizeTuple
)
where
decodeRationalFixedSizeTuple :: Decoder s Rational
decodeRationalFixedSizeTuple = do
forall s. Text -> Int -> Decoder s ()
enforceSize Text
"Rational" Int
2
Integer
n <- forall s. Decoder s Integer
decodeInteger
Integer
d <- forall s. Decoder s Integer
decodeInteger
if Integer
d forall a. Ord a => a -> a -> Bool
<= Integer
0
then forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError forall a b. (a -> b) -> a -> b
$ Text -> Text -> DecoderError
DecoderErrorCustom Text
"Rational" Text
"invalid denominator"
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! Integer
n forall a. Integral a => a -> a -> Ratio a
% Integer
d
{-# INLINE decodeRationalFixedSizeTuple #-}
{-# INLINE decodeRational #-}
_decodeRationalFuture :: Decoder s Rational
_decodeRationalFuture :: forall s. Decoder s Rational
_decodeRationalFuture = do
Version
v9 <- forall (m :: * -> *). MonadFail m => Word64 -> m Version
mkVersion64 Word64
9
Version
v10 <- forall (m :: * -> *). MonadFail m => Word64 -> m Version
mkVersion64 Word64
10
forall s a. Version -> Decoder s a -> Decoder s a -> Decoder s a
ifDecoderVersionAtLeast
Version
v10
forall s. Decoder s Rational
decodeRationalWithTag
( forall s a. Version -> Decoder s a -> Decoder s a -> Decoder s a
ifDecoderVersionAtLeast
Version
v9
(forall s. Word -> Decoder s ()
allowTag Word
30 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall s. Decoder s Rational
decodeRational)
forall s. Decoder s Rational
decodeRational
)
decodeRationalWithTag :: Decoder s Rational
decodeRationalWithTag :: forall s. Decoder s Rational
decodeRationalWithTag = forall s. Word -> Decoder s ()
assertTag Word
30 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall s. Decoder s Rational
decodeRationalWithoutTag
{-# INLINE decodeRationalWithTag #-}
decodeRationalWithoutTag :: Decoder s Rational
decodeRationalWithoutTag :: forall s. Decoder s Rational
decodeRationalWithoutTag = do
(Int
numValues, [Integer]
values) <- forall s v.
Decoder s (Maybe Int) -> Decoder s v -> Decoder s (Int, [v])
decodeCollectionWithLen forall s. Decoder s (Maybe Int)
decodeListLenOrIndef forall s. Decoder s Integer
decodeInteger
case [Integer]
values of
[Integer
n, Integer
d] -> do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Integer
d forall a. Eq a => a -> a -> Bool
== Integer
0) (forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Denominator cannot be zero")
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$! Integer
n forall a. Integral a => a -> a -> Ratio a
% Integer
d
[Integer]
_ -> forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError forall a b. (a -> b) -> a -> b
$ Text -> Int -> Int -> DecoderError
DecoderErrorSizeMismatch Text
"Rational" Int
2 Int
numValues
{-# INLINE decodeRationalWithoutTag #-}
decodeList :: Decoder s a -> Decoder s [a]
decodeList :: forall s a. Decoder s a -> Decoder s [a]
decodeList Decoder s a
decodeValue =
forall s a. Version -> Decoder s a -> Decoder s a -> Decoder s a
ifDecoderVersionAtLeast
(forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @2)
(forall s a. Decoder s (Maybe Int) -> Decoder s a -> Decoder s [a]
decodeCollection forall s. Decoder s (Maybe Int)
decodeListLenOrIndef Decoder s a
decodeValue)
(forall s a. Decoder s a -> Decoder s [a]
decodeListWith Decoder s a
decodeValue)
{-# INLINE decodeList #-}
decodeNonEmptyList :: Decoder s a -> Decoder s (NE.NonEmpty a)
decodeNonEmptyList :: forall s a. Decoder s a -> Decoder s (NonEmpty a)
decodeNonEmptyList Decoder s a
decodeValue = do
[a]
xs <- forall s a. Decoder s a -> Decoder s [a]
decodeList Decoder s a
decodeValue
case forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty [a]
xs of
Maybe (NonEmpty a)
Nothing -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Empty list found, expected non-empty"
Just NonEmpty a
ne -> forall (f :: * -> *) a. Applicative f => a -> f a
pure NonEmpty a
ne
{-# INLINE decodeNonEmptyList #-}
decodeListWith :: Decoder s a -> Decoder s [a]
decodeListWith :: forall s a. Decoder s a -> Decoder s [a]
decodeListWith Decoder s a
decodeValue = do
forall s. Decoder s ()
decodeListLenIndef
forall r a b s.
(r -> a -> r) -> r -> (r -> b) -> Decoder s a -> Decoder s b
decodeSequenceLenIndef (forall a b c. (a -> b -> c) -> b -> a -> c
flip (:)) [] forall a. [a] -> [a]
reverse Decoder s a
decodeValue
{-# INLINE decodeListWith #-}
decodeMaybe :: Decoder s a -> Decoder s (Maybe a)
decodeMaybe :: forall s a. Decoder s a -> Decoder s (Maybe a)
decodeMaybe Decoder s a
decodeValue = do
forall s a. Version -> Decoder s a -> Decoder s a -> Decoder s a
ifDecoderVersionAtLeast
(forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @2)
(forall s a. Decoder s a -> Decoder s (Maybe a)
decodeMaybeVarLen Decoder s a
decodeValue)
(forall s a. Decoder s a -> Decoder s (Maybe a)
decodeMaybeExactLen Decoder s a
decodeValue)
{-# INLINE decodeMaybe #-}
decodeMaybeExactLen :: Decoder s a -> Decoder s (Maybe a)
decodeMaybeExactLen :: forall s a. Decoder s a -> Decoder s (Maybe a)
decodeMaybeExactLen Decoder s a
decodeValue = do
Int
n <- forall s. Decoder s Int
decodeListLen
case Int
n of
Int
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
Int
1 -> do
!a
x <- Decoder s a
decodeValue
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just a
x)
Int
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"too many elements while decoding Maybe."
{-# INLINE decodeMaybeExactLen #-}
decodeMaybeVarLen :: Decoder s a -> Decoder s (Maybe a)
decodeMaybeVarLen :: forall s a. Decoder s a -> Decoder s (Maybe a)
decodeMaybeVarLen Decoder s a
decodeValue = do
Maybe Int
maybeLength <- forall s. Decoder s (Maybe Int)
decodeListLenOrIndef
case Maybe Int
maybeLength of
Just Int
0 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
Just Int
1 -> forall a. a -> Maybe a
Just forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
<$!> Decoder s a
decodeValue
Just Int
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"too many elements in length-style decoding of Maybe."
Maybe Int
Nothing -> do
Bool
isBreak <- forall s. Decoder s Bool
decodeBreakOr
if Bool
isBreak
then forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
else do
!a
x <- Decoder s a
decodeValue
Bool
isBreak2 <- forall s. Decoder s Bool
decodeBreakOr
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
isBreak2 forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"too many elements in break-style decoding of Maybe."
forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. a -> Maybe a
Just a
x)
{-# INLINE decodeMaybeVarLen #-}
decodeNullMaybe :: Decoder s a -> Decoder s (Maybe a)
decodeNullMaybe :: forall s a. Decoder s a -> Decoder s (Maybe a)
decodeNullMaybe Decoder s a
decoder = do
forall s. Decoder s TokenType
peekTokenType forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
TokenType
C.TypeNull -> do
forall s. Decoder s ()
decodeNull
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
TokenType
_ -> forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s a
decoder
{-# INLINE decodeNullMaybe #-}
decodeStrictMaybe :: Decoder s a -> Decoder s (StrictMaybe a)
decodeStrictMaybe :: forall s a. Decoder s a -> Decoder s (StrictMaybe a)
decodeStrictMaybe = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Maybe a -> StrictMaybe a
maybeToStrictMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. Decoder s a -> Decoder s (Maybe a)
decodeMaybeVarLen
{-# INLINE decodeStrictMaybe #-}
decodeNullStrictMaybe :: Decoder s a -> Decoder s (StrictMaybe a)
decodeNullStrictMaybe :: forall s a. Decoder s a -> Decoder s (StrictMaybe a)
decodeNullStrictMaybe Decoder s a
decoder = do
forall s. Decoder s TokenType
peekTokenType forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
TokenType
C.TypeNull -> do
forall s. Decoder s ()
decodeNull
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. StrictMaybe a
SNothing
TokenType
_ -> forall a. a -> StrictMaybe a
SJust forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s a
decoder
{-# INLINE decodeNullStrictMaybe #-}
decodeEither :: Decoder s a -> Decoder s b -> Decoder s (Either a b)
decodeEither :: forall s a b. Decoder s a -> Decoder s b -> Decoder s (Either a b)
decodeEither Decoder s a
decodeLeft Decoder s b
decodeRight = do
forall s. Int -> Decoder s ()
decodeListLenOf Int
2
Word
t <- forall s. Decoder s Word
decodeWord
case Word
t of
Word
0 -> forall a b. a -> Either a b
Left forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s a
decodeLeft
Word
1 -> forall a b. b -> Either a b
Right forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s b
decodeRight
Word
_ -> forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError forall a b. (a -> b) -> a -> b
$ Text -> Word8 -> DecoderError
DecoderErrorUnknownTag Text
"Either" (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
t)
{-# INLINE decodeEither #-}
decodeRecordNamed :: Text.Text -> (a -> Int) -> Decoder s a -> Decoder s a
decodeRecordNamed :: forall a s. Text -> (a -> Int) -> Decoder s a -> Decoder s a
decodeRecordNamed Text
name a -> Int
getRecordSize Decoder s a
decoder = do
forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT forall a b. (a -> b) -> a -> b
$ forall (m :: (* -> *) -> * -> *) s a.
(MonadTrans m, Monad (m (Decoder s))) =>
Text -> (a -> Int) -> m (Decoder s) a -> m (Decoder s) a
decodeRecordNamedT Text
name a -> Int
getRecordSize (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Decoder s a
decoder)
{-# INLINE decodeRecordNamed #-}
decodeRecordNamedT ::
(MonadTrans m, Monad (m (Decoder s))) =>
Text.Text ->
(a -> Int) ->
m (Decoder s) a ->
m (Decoder s) a
decodeRecordNamedT :: forall (m :: (* -> *) -> * -> *) s a.
(MonadTrans m, Monad (m (Decoder s))) =>
Text -> (a -> Int) -> m (Decoder s) a -> m (Decoder s) a
decodeRecordNamedT Text
name a -> Int
getRecordSize m (Decoder s) a
decoder =
forall (m :: (* -> *) -> * -> *) s a.
(MonadTrans m, Monad (m (Decoder s))) =>
Text
-> m (Decoder s) a
-> (a -> Int -> m (Decoder s) ())
-> m (Decoder s) a
decodeListLikeT Text
name m (Decoder s) a
decoder forall a b. (a -> b) -> a -> b
$ \a
result Int
n ->
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall s. Text -> Int -> Int -> Decoder s ()
matchSize (Text
"Record " forall a. Semigroup a => a -> a -> a
<> Text
name) Int
n (a -> Int
getRecordSize a
result)
{-# INLINE decodeRecordNamedT #-}
decodeRecordSum :: Text.Text -> (Word -> Decoder s (Int, a)) -> Decoder s a
decodeRecordSum :: forall s a. Text -> (Word -> Decoder s (Int, a)) -> Decoder s a
decodeRecordSum Text
name Word -> Decoder s (Int, a)
decoder =
forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> do
forall s a.
Text -> Decoder s a -> (a -> Int -> Decoder s ()) -> Decoder s a
decodeListLike Text
name (forall s. Decoder s Word
decodeWord forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Word -> Decoder s (Int, a)
decoder) forall a b. (a -> b) -> a -> b
$ \(Int
size, a
_) Int
n ->
forall s. Text -> Int -> Int -> Decoder s ()
matchSize (String -> Text
Text.pack String
"Sum " forall a. Semigroup a => a -> a -> a
<> Text
name) Int
size Int
n
{-# INLINE decodeRecordSum #-}
decodeListLike ::
Text.Text ->
Decoder s a ->
(a -> Int -> Decoder s ()) ->
Decoder s a
decodeListLike :: forall s a.
Text -> Decoder s a -> (a -> Int -> Decoder s ()) -> Decoder s a
decodeListLike Text
name Decoder s a
decoder a -> Int -> Decoder s ()
actOnLength =
forall {k} (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT forall a b. (a -> b) -> a -> b
$ forall (m :: (* -> *) -> * -> *) s a.
(MonadTrans m, Monad (m (Decoder s))) =>
Text
-> m (Decoder s) a
-> (a -> Int -> m (Decoder s) ())
-> m (Decoder s) a
decodeListLikeT Text
name (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Decoder s a
decoder) (\a
r Int
i -> forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (a -> Int -> Decoder s ()
actOnLength a
r Int
i))
{-# INLINE decodeListLike #-}
decodeListLikeT ::
(MonadTrans m, Monad (m (Decoder s))) =>
Text.Text ->
m (Decoder s) a ->
(a -> Int -> m (Decoder s) ()) ->
m (Decoder s) a
decodeListLikeT :: forall (m :: (* -> *) -> * -> *) s a.
(MonadTrans m, Monad (m (Decoder s))) =>
Text
-> m (Decoder s) a
-> (a -> Int -> m (Decoder s) ())
-> m (Decoder s) a
decodeListLikeT Text
name m (Decoder s) a
decoder a -> Int -> m (Decoder s) ()
actOnLength = do
Maybe Int
lenOrIndef <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall s. Decoder s (Maybe Int)
decodeListLenOrIndef
a
result <- m (Decoder s) a
decoder
case Maybe Int
lenOrIndef of
Just Int
n -> a -> Int -> m (Decoder s) ()
actOnLength a
result Int
n
Maybe Int
Nothing -> forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ do
Bool
isBreak <- forall s. Decoder s Bool
decodeBreakOr
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
isBreak forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError forall a b. (a -> b) -> a -> b
$ Text -> Text -> DecoderError
DecoderErrorCustom Text
name Text
"Excess terms in array"
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
result
{-# INLINE decodeListLikeT #-}
decodeEnumBounded :: forall a s. (Enum a, Bounded a, Typeable a) => Decoder s a
decodeEnumBounded :: forall a s. (Enum a, Bounded a, Typeable a) => Decoder s a
decodeEnumBounded = do
Int
n <- forall s. Decoder s Int
decodeInt
if forall a. Enum a => a -> Int
fromEnum (forall a. Bounded a => a
minBound :: a) forall a. Ord a => a -> a -> Bool
<= Int
n Bool -> Bool -> Bool
&& Int
n forall a. Ord a => a -> a -> Bool
<= forall a. Enum a => a -> Int
fromEnum (forall a. Bounded a => a
maxBound :: a)
then forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Enum a => Int -> a
toEnum Int
n
else
forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$
String
"Failed to decode an Enum: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Int
n forall a. Semigroup a => a -> a -> a
<> String
" for TypeRep: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show (forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall {k} (t :: k). Proxy t
Proxy @a))
{-# INLINE decodeEnumBounded #-}
decodeWithOrigin :: Decoder s a -> Decoder s (WithOrigin a)
decodeWithOrigin :: forall s a. Decoder s a -> Decoder s (WithOrigin a)
decodeWithOrigin Decoder s a
f = forall t. Maybe t -> WithOrigin t
withOriginFromMaybe forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s a. Decoder s a -> Decoder s (Maybe a)
decodeMaybe Decoder s a
f
{-# INLINE decodeWithOrigin #-}
decodeMapSkel ::
forall k m v s.
Ord k =>
([(k, v)] -> m) ->
Decoder s (k, v) ->
Decoder s m
decodeMapSkel :: forall k m v s.
Ord k =>
([(k, v)] -> m) -> Decoder s (k, v) -> Decoder s m
decodeMapSkel [(k, v)] -> m
fromDistinctDescList Decoder s (k, v)
decodeKeyValue = do
Int
n <- forall s. Decoder s Int
decodeMapLen
[(k, v)] -> m
fromDistinctDescList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case Int
n of
Int
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return []
Int
_ -> do
(k
firstKey, v
firstValue) <- Decoder s (k, v)
decodeKeyValue
Int -> k -> [(k, v)] -> Decoder s [(k, v)]
decodeEntries (Int
n forall a. Num a => a -> a -> a
- Int
1) k
firstKey [(k
firstKey, v
firstValue)]
where
decodeEntries :: Int -> k -> [(k, v)] -> Decoder s [(k, v)]
decodeEntries :: Int -> k -> [(k, v)] -> Decoder s [(k, v)]
decodeEntries Int
0 k
_ [(k, v)]
acc = forall (f :: * -> *) a. Applicative f => a -> f a
pure [(k, v)]
acc
decodeEntries !Int
remainingPairs k
previousKey ![(k, v)]
acc = do
p :: (k, v)
p@(k
newKey, v
_) <- Decoder s (k, v)
decodeKeyValue
if k
newKey forall a. Ord a => a -> a -> Bool
> k
previousKey
then Int -> k -> [(k, v)] -> Decoder s [(k, v)]
decodeEntries (Int
remainingPairs forall a. Num a => a -> a -> a
- Int
1) k
newKey ((k, v)
p forall a. a -> [a] -> [a]
: [(k, v)]
acc)
else forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError forall a b. (a -> b) -> a -> b
$ Text -> DecoderError
DecoderErrorCanonicityViolation Text
"Map"
{-# INLINE decodeMapSkel #-}
decodeCollection :: Decoder s (Maybe Int) -> Decoder s a -> Decoder s [a]
decodeCollection :: forall s a. Decoder s (Maybe Int) -> Decoder s a -> Decoder s [a]
decodeCollection Decoder s (Maybe Int)
lenOrIndef Decoder s a
el = forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s v.
Decoder s (Maybe Int) -> Decoder s v -> Decoder s (Int, [v])
decodeCollectionWithLen Decoder s (Maybe Int)
lenOrIndef Decoder s a
el
{-# INLINE decodeCollection #-}
decodeCollectionWithLen ::
Decoder s (Maybe Int) ->
Decoder s v ->
Decoder s (Int, [v])
decodeCollectionWithLen :: forall s v.
Decoder s (Maybe Int) -> Decoder s v -> Decoder s (Int, [v])
decodeCollectionWithLen Decoder s (Maybe Int)
lenOrIndef Decoder s v
decodeElement =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. [a] -> [a]
reverse forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s a b.
Monoid b =>
Decoder s (Maybe Int)
-> (a -> b -> b) -> (b -> Decoder s a) -> Decoder s (Int, b)
decodeListLikeWithCount Decoder s (Maybe Int)
lenOrIndef (:) (forall a b. a -> b -> a
const Decoder s v
decodeElement)
{-# INLINE decodeCollectionWithLen #-}
decodeMap ::
Ord k =>
Decoder s k ->
Decoder s v ->
Decoder s (Map.Map k v)
decodeMap :: forall k s v.
Ord k =>
Decoder s k -> Decoder s v -> Decoder s (Map k v)
decodeMap Decoder s k
decodeKey Decoder s v
decodeValue = forall k s v.
Ord k =>
Decoder s k -> (k -> Decoder s v) -> Decoder s (Map k v)
decodeMapByKey Decoder s k
decodeKey (forall a b. a -> b -> a
const Decoder s v
decodeValue)
{-# INLINE decodeMap #-}
decodeMapByKey ::
Ord k =>
Decoder s k ->
(k -> Decoder s v) ->
Decoder s (Map.Map k v)
decodeMapByKey :: forall k s v.
Ord k =>
Decoder s k -> (k -> Decoder s v) -> Decoder s (Map k v)
decodeMapByKey Decoder s k
decodeKey k -> Decoder s v
decodeValue =
forall s a. Version -> Decoder s a -> Decoder s a -> Decoder s a
ifDecoderVersionAtLeast
(forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @2)
( forall s a. Version -> Decoder s a -> Decoder s a -> Decoder s a
ifDecoderVersionAtLeast
(forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @9)
(forall k s v.
Ord k =>
Decoder s (Maybe Int) -> Decoder s (k, v) -> Decoder s (Map k v)
decodeMapLikeEnforceNoDuplicates forall s. Decoder s (Maybe Int)
decodeMapLenOrIndef Decoder s (k, v)
decodeKeyValue)
(forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s a. Decoder s (Maybe Int) -> Decoder s a -> Decoder s [a]
decodeCollection forall s. Decoder s (Maybe Int)
decodeMapLenOrIndef Decoder s (k, v)
decodeKeyValue)
)
(forall k m v s.
Ord k =>
([(k, v)] -> m) -> Decoder s (k, v) -> Decoder s m
decodeMapSkel forall k a. [(k, a)] -> Map k a
Map.fromDistinctDescList Decoder s (k, v)
decodeKeyValue)
where
decodeKeyValue :: Decoder s (k, v)
decodeKeyValue = do
!k
key <- Decoder s k
decodeKey
!v
value <- k -> Decoder s v
decodeValue k
key
forall (f :: * -> *) a. Applicative f => a -> f a
pure (k
key, v
value)
{-# INLINE decodeKeyValue #-}
{-# INLINE decodeMapByKey #-}
decodeMapLikeEnforceNoDuplicates ::
Ord k =>
Decoder s (Maybe Int) ->
Decoder s (k, v) ->
Decoder s (Map.Map k v)
decodeMapLikeEnforceNoDuplicates :: forall k s v.
Ord k =>
Decoder s (Maybe Int) -> Decoder s (k, v) -> Decoder s (Map k v)
decodeMapLikeEnforceNoDuplicates Decoder s (Maybe Int)
decodeLenOrIndef =
forall s a b c.
Monoid b =>
Decoder s (Maybe Int)
-> (a -> b -> b) -> (b -> (Int, c)) -> Decoder s a -> Decoder s c
decodeListLikeEnforceNoDuplicates Decoder s (Maybe Int)
decodeLenOrIndef (:) forall a b. (a -> b) -> a -> b
$ \[(k, v)]
xs ->
let result :: Map k v
result = forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList (forall a. [a] -> [a]
reverse [(k, v)]
xs)
in (forall k a. Map k a -> Int
Map.size Map k v
result, Map k v
result)
{-# INLINE decodeMapLikeEnforceNoDuplicates #-}
decodeVMap ::
(VMap.Vector kv k, VMap.Vector vv v, Ord k) =>
Decoder s k ->
Decoder s v ->
Decoder s (VMap.VMap kv vv k v)
decodeVMap :: forall (kv :: * -> *) k (vv :: * -> *) v s.
(Vector kv k, Vector vv v, Ord k) =>
Decoder s k -> Decoder s v -> Decoder s (VMap kv vv k v)
decodeVMap Decoder s k
decodeKey Decoder s v
decodeValue =
forall (kv :: * -> *) k (vv :: * -> *) v.
(Vector kv k, Vector vv v) =>
Map k v -> VMap kv vv k v
VMap.fromMap
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall k s v.
Ord k =>
Decoder s (Maybe Int) -> Decoder s (k, v) -> Decoder s (Map k v)
decodeMapLikeEnforceNoDuplicates
forall s. Decoder s (Maybe Int)
decodeMapLenOrIndef
((,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s k
decodeKey forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s v
decodeValue)
{-# INLINE decodeVMap #-}
setTag :: Word
setTag :: Word
setTag = Word
258
decodeSetTag :: Decoder s ()
decodeSetTag :: forall s. Decoder s ()
decodeSetTag = do
Word
t <- forall s. Decoder s Word
decodeTag
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Word
t forall a. Eq a => a -> a -> Bool
/= Word
setTag) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError forall a b. (a -> b) -> a -> b
$ Text -> Word8 -> DecoderError
DecoderErrorUnknownTag Text
"Set" (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
t)
{-# INLINE decodeSetTag #-}
decodeSetSkel ::
forall a s c.
Ord a =>
([a] -> c) ->
Decoder s a ->
Decoder s c
decodeSetSkel :: forall a s c. Ord a => ([a] -> c) -> Decoder s a -> Decoder s c
decodeSetSkel [a] -> c
fromDistinctDescList Decoder s a
decodeValue = do
forall s. Decoder s ()
decodeSetTag
Int
n <- forall s. Decoder s Int
decodeListLen
[a] -> c
fromDistinctDescList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case Int
n of
Int
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return []
Int
_ -> do
a
firstValue <- Decoder s a
decodeValue
Int -> a -> [a] -> Decoder s [a]
decodeEntries (Int
n forall a. Num a => a -> a -> a
- Int
1) a
firstValue [a
firstValue]
where
decodeEntries :: Int -> a -> [a] -> Decoder s [a]
decodeEntries :: Int -> a -> [a] -> Decoder s [a]
decodeEntries Int
0 a
_ [a]
acc = forall (f :: * -> *) a. Applicative f => a -> f a
pure [a]
acc
decodeEntries !Int
remainingEntries a
previousValue ![a]
acc = do
a
newValue <- Decoder s a
decodeValue
if a
newValue forall a. Ord a => a -> a -> Bool
> a
previousValue
then Int -> a -> [a] -> Decoder s [a]
decodeEntries (Int
remainingEntries forall a. Num a => a -> a -> a
- Int
1) a
newValue (a
newValue forall a. a -> [a] -> [a]
: [a]
acc)
else forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError forall a b. (a -> b) -> a -> b
$ Text -> DecoderError
DecoderErrorCanonicityViolation Text
"Set"
{-# INLINE decodeSetSkel #-}
decodeSet :: Ord a => Decoder s a -> Decoder s (Set.Set a)
decodeSet :: forall a s. Ord a => Decoder s a -> Decoder s (Set a)
decodeSet Decoder s a
valueDecoder =
forall s a. Version -> Decoder s a -> Decoder s a -> Decoder s a
ifDecoderVersionAtLeast
(forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @2)
( forall s a. Version -> Decoder s a -> Decoder s a -> Decoder s a
ifDecoderVersionAtLeast
(forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @9)
(forall s a. Ord a => Decoder s a -> Decoder s (Set a)
decodeSetEnforceNoDuplicates Decoder s a
valueDecoder)
(forall a. Ord a => [a] -> Set a
Set.fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s a. Decoder s (Maybe Int) -> Decoder s a -> Decoder s [a]
decodeCollection forall s. Decoder s (Maybe Int)
decodeListLenOrIndef Decoder s a
valueDecoder)
)
(forall a s c. Ord a => ([a] -> c) -> Decoder s a -> Decoder s c
decodeSetSkel forall a. [a] -> Set a
Set.fromDistinctDescList Decoder s a
valueDecoder)
{-# INLINE decodeSet #-}
decodeSetEnforceNoDuplicates ::
forall s a.
Ord a =>
Decoder s a ->
Decoder s (Set.Set a)
decodeSetEnforceNoDuplicates :: forall s a. Ord a => Decoder s a -> Decoder s (Set a)
decodeSetEnforceNoDuplicates = forall s a b c.
Monoid b =>
(a -> b -> b) -> (b -> (Int, c)) -> Decoder s a -> Decoder s c
decodeSetLikeEnforceNoDuplicates (:) forall a b. (a -> b) -> a -> b
$ \[a]
xs ->
let result :: Set a
result = forall a. Ord a => [a] -> Set a
Set.fromList (forall a. [a] -> [a]
reverse [a]
xs)
in (forall a. Set a -> Int
Set.size Set a
result, Set a
result)
{-# INLINE decodeSetEnforceNoDuplicates #-}
decodeListLikeWithCount ::
forall s a b.
Monoid b =>
Decoder s (Maybe Int) ->
(a -> b -> b) ->
(b -> Decoder s a) ->
Decoder s (Int, b)
decodeListLikeWithCount :: forall s a b.
Monoid b =>
Decoder s (Maybe Int)
-> (a -> b -> b) -> (b -> Decoder s a) -> Decoder s (Int, b)
decodeListLikeWithCount Decoder s (Maybe Int)
decodeLenOrIndef a -> b -> b
insert b -> Decoder s a
decodeElement = do
Decoder s (Maybe Int)
decodeLenOrIndef forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Just Int
len -> (Int -> Decoder s Bool) -> Int -> b -> Decoder s (Int, b)
loop (\Int
x -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
x forall a. Ord a => a -> a -> Bool
>= Int
len)) Int
0 forall a. Monoid a => a
mempty
Maybe Int
Nothing -> (Int -> Decoder s Bool) -> Int -> b -> Decoder s (Int, b)
loop (\Int
_ -> forall s. Decoder s Bool
decodeBreakOr) Int
0 forall a. Monoid a => a
mempty
where
loop :: (Int -> Decoder s Bool) -> Int -> b -> Decoder s (Int, b)
loop :: (Int -> Decoder s Bool) -> Int -> b -> Decoder s (Int, b)
loop Int -> Decoder s Bool
condition = Int -> b -> Decoder s (Int, b)
go
where
go :: Int -> b -> Decoder s (Int, b)
go !Int
count !b
acc = do
Bool
shouldStop <- Int -> Decoder s Bool
condition Int
count
if Bool
shouldStop
then forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
count, b
acc)
else do
a
element <- b -> Decoder s a
decodeElement b
acc
Int -> b -> Decoder s (Int, b)
go (Int
count forall a. Num a => a -> a -> a
+ Int
1) (a -> b -> b
insert a
element b
acc)
{-# INLINE loop #-}
{-# INLINE decodeListLikeWithCount #-}
decodeListLikeEnforceNoDuplicates ::
forall s a b c.
Monoid b =>
Decoder s (Maybe Int) ->
(a -> b -> b) ->
(b -> (Int, c)) ->
Decoder s a ->
Decoder s c
decodeListLikeEnforceNoDuplicates :: forall s a b c.
Monoid b =>
Decoder s (Maybe Int)
-> (a -> b -> b) -> (b -> (Int, c)) -> Decoder s a -> Decoder s c
decodeListLikeEnforceNoDuplicates Decoder s (Maybe Int)
decodeLenOrIndef a -> b -> b
insert b -> (Int, c)
getFinalWithCount Decoder s a
decodeElement = do
(Int
count, b
result) <- forall s a b.
Monoid b =>
Decoder s (Maybe Int)
-> (a -> b -> b) -> (b -> Decoder s a) -> Decoder s (Int, b)
decodeListLikeWithCount Decoder s (Maybe Int)
decodeLenOrIndef a -> b -> b
insert (forall a b. a -> b -> a
const Decoder s a
decodeElement)
let (Int
len, c
finalResult) = b -> (Int, c)
getFinalWithCount b
result
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
len forall a. Eq a => a -> a -> Bool
/= Int
count) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$
String
"Final number of elements: "
forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Int
len
forall a. Semigroup a => a -> a -> a
<> String
" does not match the total count that was decoded: "
forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Int
count
forall (f :: * -> *) a. Applicative f => a -> f a
pure c
finalResult
{-# INLINE decodeListLikeEnforceNoDuplicates #-}
decodeSetLikeEnforceNoDuplicates ::
forall s a b c.
Monoid b =>
(a -> b -> b) ->
(b -> (Int, c)) ->
Decoder s a ->
Decoder s c
decodeSetLikeEnforceNoDuplicates :: forall s a b c.
Monoid b =>
(a -> b -> b) -> (b -> (Int, c)) -> Decoder s a -> Decoder s c
decodeSetLikeEnforceNoDuplicates a -> b -> b
insert b -> (Int, c)
getFinalWithLen Decoder s a
decodeElement = do
forall s. Word -> Decoder s ()
allowTag Word
setTag
forall s a b c.
Monoid b =>
Decoder s (Maybe Int)
-> (a -> b -> b) -> (b -> (Int, c)) -> Decoder s a -> Decoder s c
decodeListLikeEnforceNoDuplicates forall s. Decoder s (Maybe Int)
decodeListLenOrIndef a -> b -> b
insert b -> (Int, c)
getFinalWithLen Decoder s a
decodeElement
{-# INLINE decodeSetLikeEnforceNoDuplicates #-}
decodeContainerSkelWithReplicate ::
Decoder s Int ->
(Int -> Decoder s c) ->
([c] -> c) ->
Decoder s c
decodeContainerSkelWithReplicate :: forall s c.
Decoder s Int -> (Int -> Decoder s c) -> ([c] -> c) -> Decoder s c
decodeContainerSkelWithReplicate Decoder s Int
decodeLen Int -> Decoder s c
replicateFun [c] -> c
concatList = do
Int
sz <- Decoder s Int
decodeLen
Int
limit <- forall s. Decoder s Int
peekAvailable
if Int
sz forall a. Ord a => a -> a -> Bool
<= Int
limit
then Int -> Decoder s c
replicateFun Int
sz
else do
let chunkSize :: Int
chunkSize = forall a. Ord a => a -> a -> a
max Int
limit Int
128
(Int
d, Int
m) = Int
sz forall a. Integral a => a -> a -> (a, a)
`divMod` Int
chunkSize
[c]
containers <- forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence forall a b. (a -> b) -> a -> b
$ Int -> Decoder s c
replicateFun Int
m forall a. a -> [a] -> [a]
: forall a. Int -> a -> [a]
replicate Int
d (Int -> Decoder s c
replicateFun Int
chunkSize)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! [c] -> c
concatList [c]
containers
{-# INLINE decodeContainerSkelWithReplicate #-}
decodeVector :: VG.Vector vec a => Decoder s a -> Decoder s (vec a)
decodeVector :: forall (vec :: * -> *) a s.
Vector vec a =>
Decoder s a -> Decoder s (vec a)
decodeVector Decoder s a
decodeValue =
forall s c.
Decoder s Int -> (Int -> Decoder s c) -> ([c] -> c) -> Decoder s c
decodeContainerSkelWithReplicate
forall s. Decoder s Int
decodeListLen
(forall (m :: * -> *) (v :: * -> *) a.
(Monad m, Vector v a) =>
Int -> m a -> m (v a)
`VG.replicateM` Decoder s a
decodeValue)
forall (v :: * -> *) a. Vector v a => [v a] -> v a
VG.concat
{-# INLINE decodeVector #-}
decodeSeq :: Decoder s a -> Decoder s (Seq.Seq a)
decodeSeq :: forall s a. Decoder s a -> Decoder s (Seq a)
decodeSeq Decoder s a
decoder = forall a. [a] -> Seq a
Seq.fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s a. Decoder s (Maybe Int) -> Decoder s a -> Decoder s [a]
decodeCollection forall s. Decoder s (Maybe Int)
decodeListLenOrIndef Decoder s a
decoder
{-# INLINE decodeSeq #-}
decodeStrictSeq :: Decoder s a -> Decoder s (SSeq.StrictSeq a)
decodeStrictSeq :: forall s a. Decoder s a -> Decoder s (StrictSeq a)
decodeStrictSeq Decoder s a
decoder = forall a. [a] -> StrictSeq a
SSeq.fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s a. Decoder s (Maybe Int) -> Decoder s a -> Decoder s [a]
decodeCollection forall s. Decoder s (Maybe Int)
decodeListLenOrIndef Decoder s a
decoder
{-# INLINE decodeStrictSeq #-}
decodeMapContents :: Decoder s a -> Decoder s [a]
decodeMapContents :: forall s a. Decoder s a -> Decoder s [a]
decodeMapContents = forall s a. Decoder s (Maybe Int) -> Decoder s a -> Decoder s [a]
decodeCollection forall s. Decoder s (Maybe Int)
decodeMapLenOrIndef
{-# INLINE decodeMapContents #-}
decodeMapTraverse ::
(Ord a, Applicative t) =>
Decoder s (t a) ->
Decoder s (t b) ->
Decoder s (t (Map.Map a b))
decodeMapTraverse :: forall a (t :: * -> *) s b.
(Ord a, Applicative t) =>
Decoder s (t a) -> Decoder s (t b) -> Decoder s (t (Map a b))
decodeMapTraverse Decoder s (t a)
decodeKey Decoder s (t b)
decodeValue =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) s a b.
Applicative t =>
Decoder s (t a) -> Decoder s (t b) -> Decoder s (t [(a, b)])
decodeMapContentsTraverse Decoder s (t a)
decodeKey Decoder s (t b)
decodeValue
{-# INLINE decodeMapTraverse #-}
decodeMapContentsTraverse ::
Applicative t =>
Decoder s (t a) ->
Decoder s (t b) ->
Decoder s (t [(a, b)])
decodeMapContentsTraverse :: forall (t :: * -> *) s a b.
Applicative t =>
Decoder s (t a) -> Decoder s (t b) -> Decoder s (t [(a, b)])
decodeMapContentsTraverse Decoder s (t a)
decodeKey Decoder s (t b)
decodeValue =
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s a. Decoder s a -> Decoder s [a]
decodeMapContents Decoder s (t (a, b))
decodeInlinedPair
where
decodeInlinedPair :: Decoder s (t (a, b))
decodeInlinedPair = forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose forall a b. (a -> b) -> a -> b
$ (,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose Decoder s (t a)
decodeKey forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose Decoder s (t b)
decodeValue
{-# INLINE decodeInlinedPair #-}
{-# INLINE decodeMapContentsTraverse #-}
decodeUTCTime :: Decoder s UTCTime
decodeUTCTime :: forall s. Decoder s UTCTime
decodeUTCTime =
forall s a. Version -> Decoder s a -> Decoder s a -> Decoder s a
ifDecoderVersionAtLeast
(forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @2)
(forall s. Text -> Int -> Decoder s ()
enforceSize Text
"UTCTime" Int
3 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall s. Decoder s UTCTime
timeDecoder)
(forall a s. Text -> (a -> Int) -> Decoder s a -> Decoder s a
decodeRecordNamed Text
"UTCTime" (forall a b. a -> b -> a
const Int
3) forall s. Decoder s UTCTime
timeDecoder)
where
timeDecoder :: Decoder s UTCTime
timeDecoder = do
!Integer
year <- forall s. Decoder s Integer
decodeInteger
!Int
dayOfYear <- forall s. Decoder s Int
decodeInt
!Integer
timeOfDayPico <- forall s. Decoder s Integer
decodeInteger
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$!
Day -> DiffTime -> UTCTime
UTCTime
(Integer -> Int -> Day
fromOrdinalDate Integer
year Int
dayOfYear)
(Integer -> DiffTime
picosecondsToDiffTime Integer
timeOfDayPico)
{-# INLINE timeDecoder #-}
{-# INLINE decodeUTCTime #-}
binaryGetDecoder ::
Bool ->
Text.Text ->
Get a ->
Decoder s a
binaryGetDecoder :: forall a s. Bool -> Text -> Get a -> Decoder s a
binaryGetDecoder Bool
allowLeftOver Text
name Get a
getter = do
ByteString
bs <- forall s. Decoder s ByteString
decodeBytes
case forall a.
Get a
-> ByteString
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
runGetOrFail Get a
getter (ByteString -> ByteString
BSL.fromStrict ByteString
bs) of
Left (ByteString
_, ByteOffset
_, String
err) -> forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError forall a b. (a -> b) -> a -> b
$ Text -> Text -> DecoderError
DecoderErrorCustom Text
name (String -> Text
Text.pack String
err)
Right (ByteString
leftOver, ByteOffset
_, a
ha)
| Bool
allowLeftOver Bool -> Bool -> Bool
|| ByteString -> Bool
BSL.null ByteString
leftOver -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
ha
| Bool
otherwise ->
forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError forall a b. (a -> b) -> a -> b
$ Text -> ByteString -> DecoderError
DecoderErrorLeftover Text
name (ByteString -> ByteString
BSL.toStrict ByteString
leftOver)
{-# INLINE binaryGetDecoder #-}
decodeIPv4 :: Decoder s IPv4
decodeIPv4 :: forall s. Decoder s IPv4
decodeIPv4 =
HostAddress -> IPv4
fromHostAddress
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s a. Version -> Decoder s a -> Decoder s a -> Decoder s a
ifDecoderVersionAtLeast
(forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @9)
(forall a s. Bool -> Text -> Get a -> Decoder s a
binaryGetDecoder Bool
False Text
"decodeIPv4" Get HostAddress
getWord32le)
(forall a s. Bool -> Text -> Get a -> Decoder s a
binaryGetDecoder Bool
True Text
"decodeIPv4" Get HostAddress
getWord32le)
{-# INLINE decodeIPv4 #-}
getHostAddress6 :: Get HostAddress6
getHostAddress6 :: Get HostAddress6
getHostAddress6 = do
!HostAddress
w1 <- Get HostAddress
getWord32le
!HostAddress
w2 <- Get HostAddress
getWord32le
!HostAddress
w3 <- Get HostAddress
getWord32le
!HostAddress
w4 <- Get HostAddress
getWord32le
forall (m :: * -> *) a. Monad m => a -> m a
return (HostAddress
w1, HostAddress
w2, HostAddress
w3, HostAddress
w4)
{-# INLINE getHostAddress6 #-}
decodeIPv6 :: Decoder s IPv6
decodeIPv6 :: forall s. Decoder s IPv6
decodeIPv6 =
HostAddress6 -> IPv6
fromHostAddress6
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s a. Version -> Decoder s a -> Decoder s a -> Decoder s a
ifDecoderVersionAtLeast
(forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @9)
(forall a s. Bool -> Text -> Get a -> Decoder s a
binaryGetDecoder Bool
False Text
"decodeIPv6" Get HostAddress6
getHostAddress6)
(forall a s. Bool -> Text -> Get a -> Decoder s a
binaryGetDecoder Bool
True Text
"decodeIPv6" Get HostAddress6
getHostAddress6)
{-# INLINE decodeIPv6 #-}
decodeTagMaybe :: Decoder s (Maybe Word64)
decodeTagMaybe :: forall s. Decoder s (Maybe Word64)
decodeTagMaybe =
forall s. Decoder s TokenType
peekTokenType forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
TokenType
C.TypeTag -> forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Decoder s Word
decodeTag
TokenType
C.TypeTag64 -> forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Decoder s Word64
decodeTag64
TokenType
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
{-# INLINE decodeTagMaybe #-}
allowTag :: Word -> Decoder s ()
allowTag :: forall s. Word -> Decoder s ()
allowTag Word
tagExpected = do
Maybe Word64
mTagReceived <- forall s. Decoder s (Maybe Word64)
decodeTagMaybe
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe Word64
mTagReceived forall a b. (a -> b) -> a -> b
$ \Word64
tagReceived ->
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Word64
tagReceived forall a. Eq a => a -> a -> Bool
== (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
tagExpected :: Word64)) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$
String
"Expecteg tag " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Word
tagExpected forall a. Semigroup a => a -> a -> a
<> String
" but got tag " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Word64
tagReceived
{-# INLINE allowTag #-}
assertTag :: Word -> Decoder s ()
assertTag :: forall s. Word -> Decoder s ()
assertTag Word
tagExpected = do
Word64
tagReceived <-
forall s. Decoder s (Maybe Word64)
decodeTagMaybe forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Just Word64
tag -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Word64
tag
Maybe Word64
Nothing -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expected tag"
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Word64
tagReceived forall a. Eq a => a -> a -> Bool
== (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
tagExpected :: Word64)) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$
String
"Expecteg tag " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Word
tagExpected forall a. Semigroup a => a -> a -> a
<> String
" but got tag " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Word64
tagReceived
{-# INLINE assertTag #-}
enforceSize :: Text.Text -> Int -> Decoder s ()
enforceSize :: forall s. Text -> Int -> Decoder s ()
enforceSize Text
lbl Int
requestedSize = forall s. Decoder s Int
decodeListLen forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall s. Text -> Int -> Int -> Decoder s ()
matchSize Text
lbl Int
requestedSize
{-# INLINE enforceSize #-}
matchSize :: Text.Text -> Int -> Int -> Decoder s ()
matchSize :: forall s. Text -> Int -> Int -> Decoder s ()
matchSize Text
lbl Int
requestedSize Int
actualSize =
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
actualSize forall a. Eq a => a -> a -> Bool
/= Int
requestedSize) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError forall a b. (a -> b) -> a -> b
$
Text -> Int -> Int -> DecoderError
DecoderErrorSizeMismatch
Text
lbl
Int
requestedSize
Int
actualSize
{-# INLINE matchSize #-}
decodeBool :: Decoder s Bool
decodeBool :: forall s. Decoder s Bool
decodeBool = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Bool
C.decodeBool
{-# INLINE decodeBool #-}
decodeBreakOr :: Decoder s Bool
decodeBreakOr :: forall s. Decoder s Bool
decodeBreakOr = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Bool
C.decodeBreakOr
{-# INLINE decodeBreakOr #-}
decodeByteArray :: Decoder s ByteArray
decodeByteArray :: forall s. Decoder s ByteArray
decodeByteArray = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s ByteArray
C.decodeByteArray
{-# INLINE decodeByteArray #-}
decodeByteArrayCanonical :: Decoder s ByteArray
decodeByteArrayCanonical :: forall s. Decoder s ByteArray
decodeByteArrayCanonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s ByteArray
C.decodeByteArrayCanonical
{-# INLINE decodeByteArrayCanonical #-}
decodeBytes :: Decoder s BS.ByteString
decodeBytes :: forall s. Decoder s ByteString
decodeBytes = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s ByteString
C.decodeBytes
{-# INLINE decodeBytes #-}
decodeBytesCanonical :: Decoder s BS.ByteString
decodeBytesCanonical :: forall s. Decoder s ByteString
decodeBytesCanonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s ByteString
C.decodeBytesCanonical
{-# INLINE decodeBytesCanonical #-}
decodeBytesIndef :: Decoder s ()
decodeBytesIndef :: forall s. Decoder s ()
decodeBytesIndef = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s ()
C.decodeBytesIndef
{-# INLINE decodeBytesIndef #-}
decodeDouble :: Decoder s Double
decodeDouble :: forall s. Decoder s Double
decodeDouble = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Double
C.decodeDouble
{-# INLINE decodeDouble #-}
decodeDoubleCanonical :: Decoder s Double
decodeDoubleCanonical :: forall s. Decoder s Double
decodeDoubleCanonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Double
C.decodeDoubleCanonical
{-# INLINE decodeDoubleCanonical #-}
decodeFloat :: Decoder s Float
decodeFloat :: forall s. Decoder s Float
decodeFloat = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Float
C.decodeFloat
{-# INLINE decodeFloat #-}
decodeFloat16Canonical :: Decoder s Float
decodeFloat16Canonical :: forall s. Decoder s Float
decodeFloat16Canonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Float
C.decodeFloat16Canonical
{-# INLINE decodeFloat16Canonical #-}
decodeFloatCanonical :: Decoder s Float
decodeFloatCanonical :: forall s. Decoder s Float
decodeFloatCanonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Float
C.decodeFloatCanonical
{-# INLINE decodeFloatCanonical #-}
decodeInt :: Decoder s Int
decodeInt :: forall s. Decoder s Int
decodeInt = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Int
C.decodeInt
{-# INLINE decodeInt #-}
decodeInt16 :: Decoder s Int16
decodeInt16 :: forall s. Decoder s Int16
decodeInt16 = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Int16
C.decodeInt16
{-# INLINE decodeInt16 #-}
decodeInt16Canonical :: Decoder s Int16
decodeInt16Canonical :: forall s. Decoder s Int16
decodeInt16Canonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Int16
C.decodeInt16Canonical
{-# INLINE decodeInt16Canonical #-}
decodeInt32 :: Decoder s Int32
decodeInt32 :: forall s. Decoder s Int32
decodeInt32 = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Int32
C.decodeInt32
{-# INLINE decodeInt32 #-}
decodeInt32Canonical :: Decoder s Int32
decodeInt32Canonical :: forall s. Decoder s Int32
decodeInt32Canonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Int32
C.decodeInt32Canonical
{-# INLINE decodeInt32Canonical #-}
decodeInt64 :: Decoder s Int64
decodeInt64 :: forall s. Decoder s ByteOffset
decodeInt64 = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s ByteOffset
C.decodeInt64
{-# INLINE decodeInt64 #-}
decodeInt64Canonical :: Decoder s Int64
decodeInt64Canonical :: forall s. Decoder s ByteOffset
decodeInt64Canonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s ByteOffset
C.decodeInt64Canonical
{-# INLINE decodeInt64Canonical #-}
decodeInt8 :: Decoder s Int8
decodeInt8 :: forall s. Decoder s Int8
decodeInt8 = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Int8
C.decodeInt8
{-# INLINE decodeInt8 #-}
decodeInt8Canonical :: Decoder s Int8
decodeInt8Canonical :: forall s. Decoder s Int8
decodeInt8Canonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Int8
C.decodeInt8Canonical
{-# INLINE decodeInt8Canonical #-}
decodeIntCanonical :: Decoder s Int
decodeIntCanonical :: forall s. Decoder s Int
decodeIntCanonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Int
C.decodeIntCanonical
{-# INLINE decodeIntCanonical #-}
decodeInteger :: Decoder s Integer
decodeInteger :: forall s. Decoder s Integer
decodeInteger = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Integer
C.decodeInteger
{-# INLINE decodeInteger #-}
decodeNatural :: Decoder s Natural
decodeNatural :: forall s. Decoder s Natural
decodeNatural = do
!Integer
n <- forall s. Decoder s Integer
decodeInteger
if Integer
n forall a. Ord a => a -> a -> Bool
>= Integer
0
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall a. Num a => Integer -> a
fromInteger Integer
n
else forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError forall a b. (a -> b) -> a -> b
$ Text -> Text -> DecoderError
DecoderErrorCustom Text
"Natural" Text
"got a negative number"
{-# INLINE decodeNatural #-}
decodeIntegerCanonical :: Decoder s Integer
decodeIntegerCanonical :: forall s. Decoder s Integer
decodeIntegerCanonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Integer
C.decodeIntegerCanonical
{-# INLINE decodeIntegerCanonical #-}
decodeListLen :: Decoder s Int
decodeListLen :: forall s. Decoder s Int
decodeListLen = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Int
C.decodeListLen
{-# INLINE decodeListLen #-}
decodeListLenCanonical :: Decoder s Int
decodeListLenCanonical :: forall s. Decoder s Int
decodeListLenCanonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Int
C.decodeListLenCanonical
{-# INLINE decodeListLenCanonical #-}
decodeListLenCanonicalOf :: Int -> Decoder s ()
decodeListLenCanonicalOf :: forall s. Int -> Decoder s ()
decodeListLenCanonicalOf = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Int -> Decoder s ()
C.decodeListLenCanonicalOf
{-# INLINE decodeListLenCanonicalOf #-}
decodeListLenIndef :: Decoder s ()
decodeListLenIndef :: forall s. Decoder s ()
decodeListLenIndef = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s ()
C.decodeListLenIndef
{-# INLINE decodeListLenIndef #-}
decodeListLenOf :: Int -> Decoder s ()
decodeListLenOf :: forall s. Int -> Decoder s ()
decodeListLenOf = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Int -> Decoder s ()
C.decodeListLenOf
{-# INLINE decodeListLenOf #-}
decodeListLenOrIndef :: Decoder s (Maybe Int)
decodeListLenOrIndef :: forall s. Decoder s (Maybe Int)
decodeListLenOrIndef = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s (Maybe Int)
C.decodeListLenOrIndef
{-# INLINE decodeListLenOrIndef #-}
decodeMapLen :: Decoder s Int
decodeMapLen :: forall s. Decoder s Int
decodeMapLen = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Int
C.decodeMapLen
{-# INLINE decodeMapLen #-}
decodeMapLenCanonical :: Decoder s Int
decodeMapLenCanonical :: forall s. Decoder s Int
decodeMapLenCanonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Int
C.decodeMapLenCanonical
{-# INLINE decodeMapLenCanonical #-}
decodeMapLenIndef :: Decoder s ()
decodeMapLenIndef :: forall s. Decoder s ()
decodeMapLenIndef = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s ()
C.decodeMapLenIndef
{-# INLINE decodeMapLenIndef #-}
decodeMapLenOrIndef :: Decoder s (Maybe Int)
decodeMapLenOrIndef :: forall s. Decoder s (Maybe Int)
decodeMapLenOrIndef = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s (Maybe Int)
C.decodeMapLenOrIndef
{-# INLINE decodeMapLenOrIndef #-}
decodeNegWord :: Decoder s Word
decodeNegWord :: forall s. Decoder s Word
decodeNegWord = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word
C.decodeNegWord
{-# INLINE decodeNegWord #-}
decodeNegWord64 :: Decoder s Word64
decodeNegWord64 :: forall s. Decoder s Word64
decodeNegWord64 = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word64
C.decodeNegWord64
{-# INLINE decodeNegWord64 #-}
decodeNegWord64Canonical :: Decoder s Word64
decodeNegWord64Canonical :: forall s. Decoder s Word64
decodeNegWord64Canonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word64
C.decodeNegWord64Canonical
{-# INLINE decodeNegWord64Canonical #-}
decodeNegWordCanonical :: Decoder s Word
decodeNegWordCanonical :: forall s. Decoder s Word
decodeNegWordCanonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word
C.decodeNegWordCanonical
{-# INLINE decodeNegWordCanonical #-}
decodeNull :: Decoder s ()
decodeNull :: forall s. Decoder s ()
decodeNull = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s ()
C.decodeNull
{-# INLINE decodeNull #-}
decodeSequenceLenIndef :: (r -> a -> r) -> r -> (r -> b) -> Decoder s a -> Decoder s b
decodeSequenceLenIndef :: forall r a b s.
(r -> a -> r) -> r -> (r -> b) -> Decoder s a -> Decoder s b
decodeSequenceLenIndef r -> a -> r
a r
b r -> b
c Decoder s a
dec = forall s a b.
Decoder s a -> (Decoder s a -> Decoder s b) -> Decoder s b
withPlainDecoder Decoder s a
dec forall a b. (a -> b) -> a -> b
$ forall r a r' s.
(r -> a -> r) -> r -> (r -> r') -> Decoder s a -> Decoder s r'
C.decodeSequenceLenIndef r -> a -> r
a r
b r -> b
c
{-# INLINE decodeSequenceLenIndef #-}
decodeSequenceLenN :: (r -> a -> r) -> r -> (r -> b) -> Int -> Decoder s a -> Decoder s b
decodeSequenceLenN :: forall r a b s.
(r -> a -> r) -> r -> (r -> b) -> Int -> Decoder s a -> Decoder s b
decodeSequenceLenN r -> a -> r
a r
b r -> b
c Int
n Decoder s a
dec = forall s a b.
Decoder s a -> (Decoder s a -> Decoder s b) -> Decoder s b
withPlainDecoder Decoder s a
dec forall a b. (a -> b) -> a -> b
$ forall r a r' s.
(r -> a -> r)
-> r -> (r -> r') -> Int -> Decoder s a -> Decoder s r'
C.decodeSequenceLenN r -> a -> r
a r
b r -> b
c Int
n
{-# INLINE decodeSequenceLenN #-}
decodeSimple :: Decoder s Word8
decodeSimple :: forall s. Decoder s Word8
decodeSimple = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word8
C.decodeSimple
{-# INLINE decodeSimple #-}
decodeSimpleCanonical :: Decoder s Word8
decodeSimpleCanonical :: forall s. Decoder s Word8
decodeSimpleCanonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word8
C.decodeSimpleCanonical
{-# INLINE decodeSimpleCanonical #-}
decodeString :: Decoder s Text.Text
decodeString :: forall s. Decoder s Text
decodeString = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Text
C.decodeString
{-# INLINE decodeString #-}
decodeStringCanonical :: Decoder s Text.Text
decodeStringCanonical :: forall s. Decoder s Text
decodeStringCanonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Text
C.decodeStringCanonical
{-# INLINE decodeStringCanonical #-}
decodeStringIndef :: Decoder s ()
decodeStringIndef :: forall s. Decoder s ()
decodeStringIndef = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s ()
C.decodeStringIndef
{-# INLINE decodeStringIndef #-}
decodeTag :: Decoder s Word
decodeTag :: forall s. Decoder s Word
decodeTag = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word
C.decodeTag
{-# INLINE decodeTag #-}
decodeTag64 :: Decoder s Word64
decodeTag64 :: forall s. Decoder s Word64
decodeTag64 = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word64
C.decodeTag64
{-# INLINE decodeTag64 #-}
decodeTag64Canonical :: Decoder s Word64
decodeTag64Canonical :: forall s. Decoder s Word64
decodeTag64Canonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word64
C.decodeTag64Canonical
{-# INLINE decodeTag64Canonical #-}
decodeTagCanonical :: Decoder s Word
decodeTagCanonical :: forall s. Decoder s Word
decodeTagCanonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word
C.decodeTagCanonical
{-# INLINE decodeTagCanonical #-}
decodeUtf8ByteArray :: Decoder s ByteArray
decodeUtf8ByteArray :: forall s. Decoder s ByteArray
decodeUtf8ByteArray = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s ByteArray
C.decodeUtf8ByteArray
{-# INLINE decodeUtf8ByteArray #-}
decodeUtf8ByteArrayCanonical :: Decoder s ByteArray
decodeUtf8ByteArrayCanonical :: forall s. Decoder s ByteArray
decodeUtf8ByteArrayCanonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s ByteArray
C.decodeUtf8ByteArrayCanonical
{-# INLINE decodeUtf8ByteArrayCanonical #-}
decodeWithByteSpan :: Decoder s a -> Decoder s (a, C.ByteOffset, C.ByteOffset)
decodeWithByteSpan :: forall s a. Decoder s a -> Decoder s (a, ByteOffset, ByteOffset)
decodeWithByteSpan Decoder s a
d = forall s a b.
Decoder s a -> (Decoder s a -> Decoder s b) -> Decoder s b
withPlainDecoder Decoder s a
d forall s a. Decoder s a -> Decoder s (a, ByteOffset, ByteOffset)
C.decodeWithByteSpan
{-# INLINE decodeWithByteSpan #-}
decodeWord :: Decoder s Word
decodeWord :: forall s. Decoder s Word
decodeWord = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word
C.decodeWord
{-# INLINE decodeWord #-}
decodeWord16 :: Decoder s Word16
decodeWord16 :: forall s. Decoder s Word16
decodeWord16 = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word16
C.decodeWord16
{-# INLINE decodeWord16 #-}
decodeWord16Canonical :: Decoder s Word16
decodeWord16Canonical :: forall s. Decoder s Word16
decodeWord16Canonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word16
C.decodeWord16Canonical
{-# INLINE decodeWord16Canonical #-}
decodeWord32 :: Decoder s Word32
decodeWord32 :: forall s. Decoder s HostAddress
decodeWord32 = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s HostAddress
C.decodeWord32
{-# INLINE decodeWord32 #-}
decodeWord32Canonical :: Decoder s Word32
decodeWord32Canonical :: forall s. Decoder s HostAddress
decodeWord32Canonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s HostAddress
C.decodeWord32Canonical
{-# INLINE decodeWord32Canonical #-}
decodeWord64 :: Decoder s Word64
decodeWord64 :: forall s. Decoder s Word64
decodeWord64 = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word64
C.decodeWord64
{-# INLINE decodeWord64 #-}
decodeWord64Canonical :: Decoder s Word64
decodeWord64Canonical :: forall s. Decoder s Word64
decodeWord64Canonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word64
C.decodeWord64Canonical
{-# INLINE decodeWord64Canonical #-}
decodeWord8 :: Decoder s Word8
decodeWord8 :: forall s. Decoder s Word8
decodeWord8 = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word8
C.decodeWord8
{-# INLINE decodeWord8 #-}
decodeWord8Canonical :: Decoder s Word8
decodeWord8Canonical :: forall s. Decoder s Word8
decodeWord8Canonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word8
C.decodeWord8Canonical
{-# INLINE decodeWord8Canonical #-}
decodeWordCanonical :: Decoder s Word
decodeWordCanonical :: forall s. Decoder s Word
decodeWordCanonical = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Word
C.decodeWordCanonical
{-# INLINE decodeWordCanonical #-}
decodeWordCanonicalOf :: Word -> Decoder s ()
decodeWordCanonicalOf :: forall s. Word -> Decoder s ()
decodeWordCanonicalOf = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Word -> Decoder s ()
C.decodeWordCanonicalOf
{-# INLINE decodeWordCanonicalOf #-}
decodeWordOf :: Word -> Decoder s ()
decodeWordOf :: forall s. Word -> Decoder s ()
decodeWordOf = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Word -> Decoder s ()
C.decodeWordOf
{-# INLINE decodeWordOf #-}
decodeTerm :: Decoder s C.Term
decodeTerm :: forall s. Decoder s Term
decodeTerm = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Term
C.decodeTerm
{-# INLINE decodeTerm #-}
peekAvailable :: Decoder s Int
peekAvailable :: forall s. Decoder s Int
peekAvailable = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s Int
C.peekAvailable
{-# INLINE peekAvailable #-}
peekByteOffset :: Decoder s C.ByteOffset
peekByteOffset :: forall s. Decoder s ByteOffset
peekByteOffset = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s ByteOffset
C.peekByteOffset
{-# INLINE peekByteOffset #-}
peekTokenType :: Decoder s C.TokenType
peekTokenType :: forall s. Decoder s TokenType
peekTokenType = forall s a. Decoder s a -> Decoder s a
fromPlainDecoder forall s. Decoder s TokenType
C.peekTokenType
{-# INLINE peekTokenType #-}