{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Cardano.Ledger.Binary.Decoding (
decodeFull,
decodeFull',
decodeFullDecoder,
decodeFullDecoder',
decodeFullAnnotator,
decodeFullAnnotatedBytes,
decodeFullAnnotatorFromHexText,
module Cardano.Ledger.Binary.Version,
module Cardano.Ledger.Binary.Decoding.DecCBOR,
module Cardano.Ledger.Binary.Decoding.Sharing,
module Cardano.Ledger.Binary.Decoding.Decoder,
module Cardano.Ledger.Binary.Decoding.Sized,
module Cardano.Ledger.Binary.Decoding.Drop,
module Cardano.Ledger.Binary.Decoding.Annotated,
decodeNestedCbor,
decodeNestedCborBytes,
unsafeDeserialize,
unsafeDeserialize',
toStrictByteString,
decodeMemPack,
)
where
import Cardano.Ledger.Binary.Decoding.Annotated
import Cardano.Ledger.Binary.Decoding.DecCBOR
import Cardano.Ledger.Binary.Decoding.Decoder hiding (getOriginalBytes)
import Cardano.Ledger.Binary.Decoding.Drop
import Cardano.Ledger.Binary.Decoding.Sharing
import Cardano.Ledger.Binary.Decoding.Sized
import Cardano.Ledger.Binary.Plain (withHexText)
import Cardano.Ledger.Binary.Version
import Codec.CBOR.Read as Read (DeserialiseFailure, IDecode (..), deserialiseIncremental)
import Codec.CBOR.Write (toStrictByteString)
import Control.Exception (displayException)
import Control.Monad (when)
import Control.Monad.ST (ST, runST)
import Data.Bifunctor (bimap)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString.Lazy.Internal as BSL
import Data.MemPack
import Data.Proxy (Proxy (Proxy))
import Data.Text (Text)
unsafeDeserialize ::
DecCBOR a =>
Version ->
BSL.ByteString ->
a
unsafeDeserialize :: forall a. DecCBOR a => Version -> ByteString -> a
unsafeDeserialize Version
version =
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a. HasCallStack => [Char] -> a
error forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. Exception e => e -> [Char]
displayException) forall a. a -> a
id forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap forall a b. (a, b) -> a
fst forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a.
Version
-> (forall s. Decoder s a)
-> ByteString
-> Either (DeserialiseFailure, ByteString) (a, ByteString)
deserialiseDecoder Version
version forall a s. DecCBOR a => Decoder s a
decCBOR
unsafeDeserialize' :: DecCBOR a => Version -> BS.ByteString -> a
unsafeDeserialize' :: forall a. DecCBOR a => Version -> ByteString -> a
unsafeDeserialize' Version
version = forall a. DecCBOR a => Version -> ByteString -> a
unsafeDeserialize Version
version forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
BSL.fromStrict
decodeFull :: forall a. DecCBOR a => Version -> BSL.ByteString -> Either DecoderError a
decodeFull :: forall a.
DecCBOR a =>
Version -> ByteString -> Either DecoderError a
decodeFull Version
version = forall a.
Version
-> Text
-> (forall s. Decoder s a)
-> ByteString
-> Either DecoderError a
decodeFullDecoder Version
version (forall a. DecCBOR a => Proxy a -> Text
label forall a b. (a -> b) -> a -> b
$ forall {k} (t :: k). Proxy t
Proxy @a) forall a s. DecCBOR a => Decoder s a
decCBOR
{-# INLINE decodeFull #-}
decodeFull' :: forall a. DecCBOR a => Version -> BS.ByteString -> Either DecoderError a
decodeFull' :: forall a.
DecCBOR a =>
Version -> ByteString -> Either DecoderError a
decodeFull' Version
version = forall a.
DecCBOR a =>
Version -> ByteString -> Either DecoderError a
decodeFull Version
version forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
BSL.fromStrict
{-# INLINE decodeFull' #-}
decodeFullDecoder ::
Version ->
Text ->
(forall s. Decoder s a) ->
BSL.ByteString ->
Either DecoderError a
decodeFullDecoder :: forall a.
Version
-> Text
-> (forall s. Decoder s a)
-> ByteString
-> Either DecoderError a
decodeFullDecoder Version
version Text
lbl forall s. Decoder s a
decoder ByteString
bs =
case forall a.
Version
-> (forall s. Decoder s a)
-> ByteString
-> Either (DeserialiseFailure, ByteString) (a, ByteString)
deserialiseDecoder Version
version forall s. Decoder s a
decoder ByteString
bs of
Right (a
x, ByteString
leftover) ->
if ByteString -> Bool
BS.null ByteString
leftover
then forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
else forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text -> ByteString -> DecoderError
DecoderErrorLeftover Text
lbl ByteString
leftover
Left (DeserialiseFailure
e, ByteString
_) -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text -> DeserialiseFailure -> DecoderError
DecoderErrorDeserialiseFailure Text
lbl DeserialiseFailure
e
{-# INLINE decodeFullDecoder #-}
decodeFullDecoder' ::
Version ->
Text ->
(forall s. Decoder s a) ->
BS.ByteString ->
Either DecoderError a
decodeFullDecoder' :: forall a.
Version
-> Text
-> (forall s. Decoder s a)
-> ByteString
-> Either DecoderError a
decodeFullDecoder' Version
version Text
lbl forall s. Decoder s a
decoder = forall a.
Version
-> Text
-> (forall s. Decoder s a)
-> ByteString
-> Either DecoderError a
decodeFullDecoder Version
version Text
lbl forall s. Decoder s a
decoder forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
BSL.fromStrict
{-# INLINE decodeFullDecoder' #-}
deserialiseDecoder ::
Version ->
(forall s. Decoder s a) ->
BSL.ByteString ->
Either (Read.DeserialiseFailure, BS.ByteString) (a, BS.ByteString)
deserialiseDecoder :: forall a.
Version
-> (forall s. Decoder s a)
-> ByteString
-> Either (DeserialiseFailure, ByteString) (a, ByteString)
deserialiseDecoder Version
version forall s. Decoder s a
decoder ByteString
bsl =
forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$
forall s a.
ByteString
-> IDecode s a
-> ST s (Either (DeserialiseFailure, ByteString) (a, ByteString))
supplyAllInput ByteString
bsl forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall s a. Decoder s a -> ST s (IDecode s a)
Read.deserialiseIncremental (forall s a.
Maybe ByteString -> Version -> Decoder s a -> Decoder s a
toPlainDecoder (forall a. a -> Maybe a
Just ByteString
bsl) Version
version forall s. Decoder s a
decoder)
{-# INLINE deserialiseDecoder #-}
supplyAllInput ::
BSL.ByteString ->
Read.IDecode s a ->
ST s (Either (Read.DeserialiseFailure, BS.ByteString) (a, BS.ByteString))
supplyAllInput :: forall s a.
ByteString
-> IDecode s a
-> ST s (Either (DeserialiseFailure, ByteString) (a, ByteString))
supplyAllInput ByteString
bs' (Read.Done ByteString
bs ByteOffset
_ a
x) =
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right (a
x, ByteString
bs forall a. Semigroup a => a -> a -> a
<> ByteString -> ByteString
BSL.toStrict ByteString
bs'))
supplyAllInput ByteString
bs (Read.Partial Maybe ByteString -> ST s (IDecode s a)
k) = case ByteString
bs of
BSL.Chunk ByteString
chunk ByteString
bs' -> Maybe ByteString -> ST s (IDecode s a)
k (forall a. a -> Maybe a
Just ByteString
chunk) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall s a.
ByteString
-> IDecode s a
-> ST s (Either (DeserialiseFailure, ByteString) (a, ByteString))
supplyAllInput ByteString
bs'
ByteString
BSL.Empty -> Maybe ByteString -> ST s (IDecode s a)
k forall a. Maybe a
Nothing forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall s a.
ByteString
-> IDecode s a
-> ST s (Either (DeserialiseFailure, ByteString) (a, ByteString))
supplyAllInput ByteString
BSL.Empty
supplyAllInput ByteString
_ (Read.Fail ByteString
bs ByteOffset
_ DeserialiseFailure
exn) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left (DeserialiseFailure
exn, ByteString
bs))
{-# INLINE supplyAllInput #-}
decodeFullAnnotator ::
Version ->
Text ->
(forall s. Decoder s (Annotator a)) ->
BSL.ByteString ->
Either DecoderError a
decodeFullAnnotator :: forall a.
Version
-> Text
-> (forall s. Decoder s (Annotator a))
-> ByteString
-> Either DecoderError a
decodeFullAnnotator Version
v Text
lbl forall s. Decoder s (Annotator a)
decoder ByteString
bytes =
(\Annotator a
x -> forall a. Annotator a -> FullByteString -> a
runAnnotator Annotator a
x (ByteString -> FullByteString
Full ByteString
bytes)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a.
Version
-> Text
-> (forall s. Decoder s a)
-> ByteString
-> Either DecoderError a
decodeFullDecoder Version
v Text
lbl forall s. Decoder s (Annotator a)
decoder ByteString
bytes
{-# INLINE decodeFullAnnotator #-}
decodeFullAnnotatedBytes ::
Functor f =>
Version ->
Text ->
(forall s. Decoder s (f ByteSpan)) ->
BSL.ByteString ->
Either DecoderError (f BS.ByteString)
decodeFullAnnotatedBytes :: forall (f :: * -> *).
Functor f =>
Version
-> Text
-> (forall s. Decoder s (f ByteSpan))
-> ByteString
-> Either DecoderError (f ByteString)
decodeFullAnnotatedBytes Version
v Text
lbl forall s. Decoder s (f ByteSpan)
decoder ByteString
bytes =
forall (f :: * -> *).
Functor f =>
ByteString -> f ByteSpan -> f ByteString
annotationBytes ByteString
bytes forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a.
Version
-> Text
-> (forall s. Decoder s a)
-> ByteString
-> Either DecoderError a
decodeFullDecoder Version
v Text
lbl forall s. Decoder s (f ByteSpan)
decoder ByteString
bytes
{-# INLINE decodeFullAnnotatedBytes #-}
decodeFullAnnotatorFromHexText ::
Version ->
Text ->
(forall s. Decoder s (Annotator a)) ->
Text ->
Either DecoderError a
decodeFullAnnotatorFromHexText :: forall a.
Version
-> Text
-> (forall s. Decoder s (Annotator a))
-> Text
-> Either DecoderError a
decodeFullAnnotatorFromHexText Version
v Text
desc forall s. Decoder s (Annotator a)
dec =
forall b.
(ByteString -> Either DecoderError b)
-> Text -> Either DecoderError b
withHexText forall a b. (a -> b) -> a -> b
$ forall a.
Version
-> Text
-> (forall s. Decoder s (Annotator a))
-> ByteString
-> Either DecoderError a
decodeFullAnnotator Version
v Text
desc forall s. Decoder s (Annotator a)
dec forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
BSL.fromStrict
{-# INLINE decodeFullAnnotatorFromHexText #-}
decodeNestedCborTag :: Decoder s ()
decodeNestedCborTag :: forall s. Decoder s ()
decodeNestedCborTag = 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
24) 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
"decodeNestedCborTag"
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
t)
{-# INLINE decodeNestedCborTag #-}
decodeNestedCbor :: DecCBOR a => Decoder s a
decodeNestedCbor :: forall a s. DecCBOR a => Decoder s a
decodeNestedCbor = do
ByteString
bs <- forall s. Decoder s ByteString
decodeNestedCborBytes
Version
version <- forall s. Decoder s Version
getDecoderVersion
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a.
DecCBOR a =>
Version -> ByteString -> Either DecoderError a
decodeFull' Version
version ByteString
bs
{-# INLINE decodeNestedCbor #-}
decodeNestedCborBytes :: Decoder s BS.ByteString
decodeNestedCborBytes :: forall s. Decoder s ByteString
decodeNestedCborBytes = forall s. Decoder s ()
decodeNestedCborTag forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall s. Decoder s ByteString
decodeBytes
{-# INLINE decodeNestedCborBytes #-}
decodeMemPack :: MemPack a => Decoder s a
decodeMemPack :: forall a s. MemPack a => Decoder s a
decodeMemPack = forall a b (m :: * -> *).
(MemPack a, Buffer b, MonadFail m) =>
b -> m a
unpackMonadFail forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteArray -> ByteArray
unBA forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall s. Decoder s ByteArray
decodeByteArray