Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- data Annotated b a = Annotated {
- unAnnotated ∷ !b
- annotation ∷ !a
- data ByteSpan = ByteSpan !ByteOffset !ByteOffset
- class Decoded t where
- type BaseType t ∷ Type
- recoverBytes ∷ t → ByteString
- annotationBytes ∷ Functor f ⇒ ByteString → f ByteSpan → f 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))
Documentation
Annotated | |
|
Instances
A pair of offsets delimiting the beginning and end of a substring of a ByteString
Instances
ToJSON ByteSpan Source # | |
Generic ByteSpan Source # | |
Show ByteSpan Source # | |
type Rep ByteSpan Source # | |
Defined in Cardano.Ledger.Binary.Decoding.Annotated type Rep ByteSpan = D1 ('MetaData "ByteSpan" "Cardano.Ledger.Binary.Decoding.Annotated" "cardano-ledger-binary-1.3.2.0-inplace" 'False) (C1 ('MetaCons "ByteSpan" 'PrefixI 'False) (S1 ('MetaSel ('Nothing ∷ Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteOffset) :*: S1 ('MetaSel ('Nothing ∷ Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteOffset))) |
class Decoded t where Source #
recoverBytes ∷ t → ByteString Source #
Instances
Decoded (Annotated b ByteString) Source # | |
Defined in Cardano.Ledger.Binary.Decoding.Annotated type BaseType (Annotated b ByteString) Source # |
annotationBytes ∷ Functor f ⇒ ByteString → f ByteSpan → f ByteString Source #
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)))))
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.