Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- newtype Decoder s a = Decoder {
- matchChunk :: LocatedChunk -> ST s (LocatedChunk, DecodeResult s a)
- data DecodeResult s a where
- DecodeDone :: a -> DecodeResult s a
- DecodeFail :: String -> DecodeResult s a
- DecodeNeedsData :: Decoder s a -> DecodeResult s a
- DecodeLargeToken :: Word32 -> (ByteString -> Decoder s a) -> DecodeResult s a
- DecodeIncremental :: Word32 -> Decoder s a -> ([a] -> Decoder s b) -> DecodeResult s b
- DecodeIncremental_ :: Word32 -> Decoder s () -> Decoder s a -> DecodeResult s a
- liftDecoder :: ST s a -> Decoder s a
- data DeserialiseFailure = DeserialiseFailure ByteOffset String
- deserialiseByteString :: (forall s. Decoder s a) -> ByteString -> Either DeserialiseFailure (ByteString, ByteOffset, a)
- decodeLittleEndian :: forall s a. ByteSwap a => Decoder s a
- decodeLargeToken :: Word32 -> Decoder s ByteString
- decodeIncremental :: Word32 -> Decoder s a -> Decoder s [a]
- decodeIncremental_ :: Word32 -> Decoder s () -> Decoder s ()
- data Located a = L !a !ByteOffset
- type ByteOffset = Word32
- type LocatedChunk = Located ByteString
Constructing decoders
Decoder
A decoder describes how to match against a single chunk of the input.
Decoder | |
|
data DecodeResult s a where Source #
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 |
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. |
| |
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, 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. |
| |
DecodeIncremental_ | Variation on 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. |
|
liftDecoder :: ST s a -> Decoder s a Source #
Running decoders
data DeserialiseFailure Source #
Error type for deserialisation.
DeserialiseFailure | |
|
Instances
Exception DeserialiseFailure Source # | |
Show DeserialiseFailure Source # | |
Defined in Codec.Borsh.Incremental.Monad showsPrec :: Int -> DeserialiseFailure -> ShowS # show :: DeserialiseFailure -> String # showList :: [DeserialiseFailure] -> ShowS # | |
Eq DeserialiseFailure Source # | |
Defined in Codec.Borsh.Incremental.Monad (==) :: DeserialiseFailure -> DeserialiseFailure -> Bool # (/=) :: DeserialiseFailure -> DeserialiseFailure -> Bool # |
deserialiseByteString :: (forall s. Decoder s a) -> ByteString -> Either DeserialiseFailure (ByteString, ByteOffset, a) Source #
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 #
decodeLargeToken :: Word32 -> Decoder s ByteString Source #
Located values
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