Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Test.Cardano.Ledger.Binary.Annotator
Contents
Synopsis
- decodeFullAnnotator ∷ Version → Text → (∀ s. Decoder s (Annotator a)) → ByteString → Either DecoderError a
- decodeFullAnnotatedBytes ∷ Functor f ⇒ Version → Text → (∀ s. Decoder s (f ByteSpan)) → ByteString → Either DecoderError (f ByteString)
- decodeFullAnnotatorFromHexText ∷ Version → Text → (∀ s. Decoder s (Annotator a)) → Text → Either DecoderError a
- data Annotated b a = Annotated {
- unAnnotated ∷ !b
- annotation ∷ !a
- decodeAnnotated ∷ Decoder s a → Decoder s (Annotated a ByteString)
- data ByteSpan = ByteSpan !ByteOffset !ByteOffset
- class Decoded t where
- type BaseType t
- recoverBytes ∷ t → ByteString
- annotatedDecoder ∷ Decoder s a → Decoder s (Annotated a ByteSpan)
- slice ∷ ByteString → ByteSpan → ByteString
- decCBORAnnotated ∷ DecCBOR a ⇒ Decoder s (Annotated a ByteSpan)
- reAnnotate ∷ EncCBOR a ⇒ Version → Annotated a b → Annotated a ByteString
- newtype Annotator a = Annotator {
- runAnnotator ∷ FullByteString → a
- annotatorSlice ∷ Decoder s (Annotator (ByteString → a)) → Decoder s (Annotator a)
- withSlice ∷ Decoder s a → Decoder s (a, Annotator ByteString)
- newtype FullByteString = Full ByteString
- decodeAnnSet ∷ Ord t ⇒ Decoder s (Annotator t) → Decoder s (Annotator (Set t))
- translateViaCBORAnnotator ∷ (ToCBOR a, DecCBOR (Annotator b)) ⇒ Version → Text → a → Except DecoderError b
Documentation
decodeFullAnnotator ∷ Version → Text → (∀ s. Decoder s (Annotator a)) → ByteString → Either DecoderError a Source #
Same as decodeFullDecoder
, except it provdes the means of passing portion or all
of the ByteString
input argument to the decoding Annotator
.
decodeFullAnnotatedBytes ∷ Functor f ⇒ Version → Text → (∀ s. Decoder s (f ByteSpan)) → ByteString → Either DecoderError (f ByteString) Source #
Same as decodeFullDecoder
, decodes a Haskell value from a lazy
ByteString
, requiring that the full ByteString is consumed, and
replaces ByteSpan
annotations with the corresponding slice of the input as
a strict ByteString
.
decodeFullAnnotatorFromHexText ∷ Version → Text → (∀ s. Decoder s (Annotator a)) → Text → Either DecoderError a Source #
Constructors
Annotated | |
Fields
|
Instances
A pair of offsets delimiting the beginning and end of a substring of a ByteString
Constructors
ByteSpan !ByteOffset !ByteOffset |
Instances
ToJSON ByteSpan | |
Generic ByteSpan | |
Show ByteSpan | |
type Rep ByteSpan | |
Defined in Cardano.Ledger.Binary.Decoding.Annotated type Rep ByteSpan = D1 ('MetaData "ByteSpan" "Cardano.Ledger.Binary.Decoding.Annotated" "cardano-ledger-binary-1.7.0.0-inplace" 'False) (C1 ('MetaCons "ByteSpan" 'PrefixI 'False) (S1 ('MetaSel ('Nothing ∷ Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedUnpack) (Rec0 ByteOffset) :*: S1 ('MetaSel ('Nothing ∷ Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedUnpack) (Rec0 ByteOffset))) |
class Decoded t where Source #
Methods
recoverBytes ∷ t → ByteString Source #
Instances
Decoded (Annotated b ByteString) | |
Defined in Cardano.Ledger.Binary.Decoding.Annotated Associated Types type BaseType (Annotated b ByteString) Source # Methods |
annotatedDecoder ∷ Decoder s a → Decoder s (Annotated a ByteSpan) Source #
A decoder for a value paired with an annotation specifying the start and end of the consumed bytes.
slice ∷ ByteString → ByteSpan → ByteString Source #
Extract a substring of a given ByteString corresponding to the offsets.
decCBORAnnotated ∷ DecCBOR a ⇒ Decoder s (Annotated a ByteSpan) Source #
A decoder for a value paired with an annotation specifying the start and end of the consumed bytes.
reAnnotate ∷ EncCBOR a ⇒ Version → Annotated a b → Annotated a ByteString Source #
Reconstruct an annotation by re-serialising the payload to a ByteString.
A value of type (Annotator a)
is one that needs access to the entire bytestring
used during decoding to finish construction of a vaue of type a
. A typical use is
some type that stores the bytes that were used to deserialize it. For example the
type Inner
below is constructed using the helper function makeInner
which
serializes and stores its bytes (using serialize
). Note how we build the
Annotator
by abstracting over the full bytes, and using those original bytes to
fill the bytes field of the constructor Inner
. The EncCBOR
instance just reuses
the stored bytes to produce an encoding (using encodePreEncoded
).
data Inner = Inner Int Bool LByteString makeInner :: Int -> Bool -> Inner makeInner i b = Inner i b (serialize (encCBOR i <> encCBOR b)) instance EncCBOR Inner where encCBOR (Inner _ _ bytes) = encodePreEncoded bytes instance DecCBOR (Annotator Inner) where decCBOR = do int <- decCBOR trueOrFalse <- decCBOR pure (Annotator ((Full bytes) -> Inner int trueOrFalse bytes))
if an Outer
type has a field of type Inner
, with a (EncCBOR (Annotator Inner))
instance, the Outer
type must also have a (EncCBOR (Annotator Outer))
instance. The
key to writing that instance is to use the operation withSlice
which returns a pair.
The first component is an Annotator
that can build Inner
, the second is an
Annotator
that given the full bytes, extracts just the bytes needed to decode
Inner
.
data Outer = Outer Text Inner instance EncCBOR Outer where encCBOR (Outer t i) = encCBOR t <> encCBOR i instance DecCBOR (Annotator Outer) where decCBOR = do t <- decCBOR (Annotator mkInner, Annotator extractInnerBytes) <- withSlice decCBOR pure (Annotator ( full -> Outer t (mkInner (Full (extractInnerBytes full)))))
Constructors
Annotator | |
Fields
|
annotatorSlice ∷ Decoder s (Annotator (ByteString → a)) → Decoder s (Annotator a) Source #
The argument is a decoder for a annotator that needs access to the bytes that | were decoded. This function constructs and supplies the relevant piece.
withSlice ∷ Decoder s a → Decoder s (a, Annotator ByteString) Source #
Pairs the decoder result with an annotator that can be used to construct the exact bytes used to decode the result.
newtype FullByteString Source #
This marks the entire bytestring used during decoding, rather than the piece we need to finish constructing our value.
Constructors
Full ByteString |
translateViaCBORAnnotator Source #
Arguments
∷ (ToCBOR a, DecCBOR (Annotator b)) | |
⇒ Version | Version that will be used for deserialization |
→ Text | |
→ a | |
→ Except DecoderError b |
Translation function between values through a related binary representation. This
function allows you to translate one type into another (or the same one) through their
common binary format. It is possible for the source type to be encoded with a different
version than the version that will be used for decoding. This is useful for types that
build upon one another and are "upgradeable" through their binary representation. It is
important to note that the deserialization will happen with Annotator
, since that is
usually the way we deserialize upgradeable types that live on chain. Moreover, encoding
does not require a version, because memoized types that were decoded with annotation
will have the bytes retained and thus will have the ToCBOR
instance.