cardano-ledger-binary-1.7.0.0: Binary serialization library used throughout ledger
Safe HaskellSafe-Inferred
LanguageHaskell2010

Test.Cardano.Ledger.Binary.Annotator

Synopsis

Documentation

decodeFullAnnotatorVersionText → (∀ 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.

decodeFullAnnotatedBytesFunctor f ⇒ VersionText → (∀ 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.

data Annotated b a Source #

Constructors

Annotated 

Fields

Instances

Instances details
Bifunctor Annotated 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Methods

bimap ∷ (a → b) → (c → d) → Annotated a c → Annotated b d #

first ∷ (a → b) → Annotated a c → Annotated b c #

second ∷ (b → c) → Annotated a b → Annotated a c #

Functor (Annotated b) 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Methods

fmap ∷ (a → b0) → Annotated b a → Annotated b b0 #

(<$) ∷ a → Annotated b b0 → Annotated b a #

FromJSON b ⇒ FromJSON (Annotated b ()) 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

ToJSON b ⇒ ToJSON (Annotated b a) 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Generic (Annotated b a) 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Associated Types

type Rep (Annotated b a) ∷ TypeType #

Methods

fromAnnotated b a → Rep (Annotated b a) x #

toRep (Annotated b a) x → Annotated b a #

(Show b, Show a) ⇒ Show (Annotated b a) 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Methods

showsPrecIntAnnotated b a → ShowS #

showAnnotated b a → String #

showList ∷ [Annotated b a] → ShowS #

Decoded (Annotated b ByteString) 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Associated Types

type BaseType (Annotated b ByteString) Source #

DecCBOR a ⇒ DecCBOR (Annotated a ByteString) 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Methods

decCBORDecoder s (Annotated a ByteString) Source #

dropCBORProxy (Annotated a ByteString) → Decoder s () Source #

labelProxy (Annotated a ByteString) → Text Source #

(NFData b, NFData a) ⇒ NFData (Annotated b a) 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Methods

rnfAnnotated b a → () #

(Eq b, Eq a) ⇒ Eq (Annotated b a) 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Methods

(==)Annotated b a → Annotated b a → Bool #

(/=)Annotated b a → Annotated b a → Bool #

(Eq a, Ord b) ⇒ Ord (Annotated b a) 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Methods

compareAnnotated b a → Annotated b a → Ordering #

(<)Annotated b a → Annotated b a → Bool #

(<=)Annotated b a → Annotated b a → Bool #

(>)Annotated b a → Annotated b a → Bool #

(>=)Annotated b a → Annotated b a → Bool #

maxAnnotated b a → Annotated b a → Annotated b a #

minAnnotated b a → Annotated b a → Annotated b a #

(NoThunks b, NoThunks a) ⇒ NoThunks (Annotated b a) 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

type Rep (Annotated b a) 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

type Rep (Annotated b a) = D1 ('MetaData "Annotated" "Cardano.Ledger.Binary.Decoding.Annotated" "cardano-ledger-binary-1.7.0.0-inplace" 'False) (C1 ('MetaCons "Annotated" 'PrefixI 'True) (S1 ('MetaSel ('Just "unAnnotated") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 b) :*: S1 ('MetaSel ('Just "annotation") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a)))
type BaseType (Annotated b ByteString) 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

decodeAnnotatedDecoder s a → Decoder s (Annotated a ByteString) Source #

data ByteSpan Source #

A pair of offsets delimiting the beginning and end of a substring of a ByteString

Instances

Instances details
ToJSON ByteSpan 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Generic ByteSpan 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Associated Types

type Rep ByteSpanTypeType #

Methods

fromByteSpanRep ByteSpan x #

toRep ByteSpan x → ByteSpan #

Show ByteSpan 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Methods

showsPrecIntByteSpanShowS #

showByteSpanString #

showList ∷ [ByteSpan] → ShowS #

type Rep ByteSpan 
Instance details

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 ('NothingMaybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedUnpack) (Rec0 ByteOffset) :*: S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedUnpack) (Rec0 ByteOffset)))

class Decoded t where Source #

Associated Types

type BaseType t Source #

Methods

recoverBytes ∷ t → ByteString Source #

Instances

Instances details
Decoded (Annotated b ByteString) 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Associated Types

type BaseType (Annotated b ByteString) Source #

annotatedDecoderDecoder 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.

decCBORAnnotatedDecCBOR 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.

reAnnotateEncCBOR a ⇒ VersionAnnotated a b → Annotated a ByteString Source #

Reconstruct an annotation by re-serialising the payload to a ByteString.

newtype Annotator a Source #

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

Instances

Instances details
Applicative Annotator 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Methods

pure ∷ a → Annotator a #

(<*>)Annotator (a → b) → Annotator a → Annotator b #

liftA2 ∷ (a → b → c) → Annotator a → Annotator b → Annotator c #

(*>)Annotator a → Annotator b → Annotator b #

(<*)Annotator a → Annotator b → Annotator a #

Functor Annotator 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Methods

fmap ∷ (a → b) → Annotator a → Annotator b #

(<$) ∷ a → Annotator b → Annotator a #

Monad Annotator 
Instance details

Defined in Cardano.Ledger.Binary.Decoding.Annotated

Methods

(>>=)Annotator a → (a → Annotator b) → Annotator b #

(>>)Annotator a → Annotator b → Annotator b #

return ∷ a → Annotator a #

DecCBOR (Annotator Data) Source # 
Instance details

Defined in Test.Cardano.Ledger.Binary.Annotator

annotatorSliceDecoder 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.

withSliceDecoder 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.

Orphan instances