borsh-0.1.0: Implementation of BORSH serialisation
Safe HaskellSafe-Inferred
LanguageHaskell2010

Codec.Borsh.Incremental

Synopsis

Constructing decoders

newtype Decoder s a Source #

Decoder

A decoder describes how to match against a single chunk of the input.

Constructors

Decoder 

Instances

Instances details
MonadFail (Decoder s) Source # 
Instance details

Defined in Codec.Borsh.Incremental.Decoder

Methods

fail :: String -> Decoder s a #

Applicative (Decoder s) Source # 
Instance details

Defined in Codec.Borsh.Incremental.Decoder

Methods

pure :: a -> Decoder s a #

(<*>) :: Decoder s (a -> b) -> Decoder s a -> Decoder s b #

liftA2 :: (a -> b -> c) -> Decoder s a -> Decoder s b -> Decoder s c #

(*>) :: Decoder s a -> Decoder s b -> Decoder s b #

(<*) :: Decoder s a -> Decoder s b -> Decoder s a #

Functor (Decoder s) Source # 
Instance details

Defined in Codec.Borsh.Incremental.Decoder

Methods

fmap :: (a -> b) -> Decoder s a -> Decoder s b #

(<$) :: a -> Decoder s b -> Decoder s a #

Monad (Decoder s) Source # 
Instance details

Defined in Codec.Borsh.Incremental.Decoder

Methods

(>>=) :: Decoder s a -> (a -> Decoder s b) -> Decoder s b #

(>>) :: Decoder s a -> Decoder s b -> Decoder s b #

return :: a -> Decoder s a #

data DecodeResult s a where Source #

Constructors

DecodeDone :: a -> DecodeResult s a

The decoder terminated successfully: we can stop decoding

DecodeFail :: String -> DecodeResult s a

The decoder failed: we should abort

DecodeNeedsData :: Decoder s a -> DecodeResult s a

The decoder needs more data before it can continue

NOTE: The decoder that is waiting for more data may not be (and typically will not be) the decoder we started with in matchChunk: in the typical case, a bunch of values will have been decoded successfully before we get to a (continuation) decoder that requires data beyond the current chunk.

DecodeLargeToken

Large token of known length that spans multiple chunks

This is NOT incremental: all chunks will be read into memory before the function is applied. Primarily useful for large types that are not easily split into (valid) chunks, such as UTF8-encoded text (if were wanted to split that, we'd have to split it at UTF8 boundaries).

The continuation will be called with a lazy bytestring of precisely the requested length (provided enough input is available, of course), along with the remaining input token to be provided to the continuation decoder.

Fields

DecodeIncremental

Incremental interface

When decoding large objects such as lists, we do not want to bring all required chunks into memory before decoding the list. Instead, we want to decode the list elements as we go. In this case, DecodeIncremental can be used to repeatedly decode a value using decoder for the elements; when all elements have been processed, the continuation decoder is called.

NOTE: This interface is incremental in the sense that the input chunks are read one at a time. It is NOT incremental in the generated output.

Fields

DecodeIncremental_

Variation on DecodeIncremental, where we do not accumulate results

This is useful for example for datatypes that we can update imperatively, such as mutable arrays. It could also be used to skip over unused parts of the input.

Fields

  • :: Word32

    How often to repeat the smaller decoder

  • -> Decoder s ()

    Decoder to repeat (imperatively handling each element)

  • -> Decoder s a

    Continuation

  • -> DecodeResult s a
     

liftDecoder :: ST s a -> Decoder s a Source #

Running decoders

data DeserialiseFailure Source #

Error type for deserialisation.

Constructors

DeserialiseFailure 

Fields

  • ByteOffset

    The position of the decoder when the failure occurred

  • String

    Message explaining the failure

Specialised decoders

These functions comprise a low-level decoder interface which will not be necessary for most applications. Most applications should simply use deserialiseBorsh

decodeLittleEndian :: forall s a. ByteSwap a => Decoder s a Source #

Located values

data Located a Source #

Value at a particular point in the input

Constructors

L !a !ByteOffset 

type ByteOffset = Word32 Source #

Offset in bytes within the input

type LocatedChunk = Located ByteString Source #

The most common case: chunk of the input at a particular point