pipes-binary-0.2.0: Encode and decode binary streams using the pipes and binary libraries.

Safe HaskellNone

Pipes.Binary

Contents

Description

This module exports facilities that allow you to encode and decode Pipes streams of binary values. It builds on top of the binary, pipes and pipes-parse libraries, and assumes you know how to use those libraries.

Synopsis

Binary instances

encode :: (Monad m, Binary x) => x -> Producer' ByteString m ()Source

Encodes the given Binary instance and sends it downstream in ByteString chunks.

decodeSource

Arguments

:: (Monad m, Binary b) 
=> StateT (Producer ByteString m r) m (Either DecodingError (ByteOffset, b)) 

Try to decode leading output from the underlying Producer into a Binary instance, returning either a DecodingError on failure, or a pair with the decoded entity together with the number of bytes consumed in order to produce it.

Do not use this function if isEndOfBytes returns True, otherwise you may get unexpected decoding errors.

decodeManySource

Arguments

:: (Monad m, Binary b) 
=> Producer ByteString m r

Producer from which to draw input.

-> Producer' (ByteOffset, b) m (Either (DecodingError, Producer ByteString m r) r) 

Continuously decode output from the given Producer into a Binary instance, sending downstream pairs of each successfully decoded entity together with the number of bytes consumed in order to produce it.

This Producer runs until it either runs out of input or a decoding failure occurs, in which case it returns Left with a DecodingError and a Producer with any leftovers. You can use errorP to turn the Either return value into an ErrorT monad transformer.

Get monad

decodeGetSource

Arguments

:: Monad m 
=> Get b 
-> StateT (Producer ByteString m r) m (Either DecodingError (ByteOffset, b)) 

Like decode, except it takes an explicit Get monad.

decodeGetManySource

Arguments

:: Monad m 
=> Get b 
-> Producer ByteString m r

Producer from which to draw input.

-> Producer' (ByteOffset, b) m (Either (DecodingError, Producer ByteString m r) r) 

Like decodeMany, except it takes an explicit Get monad.

Put monad

encodePut :: Monad m => Put -> Producer' ByteString m ()Source

Like encode, except it takes an explicit Put monad.

Types

data DecodingError Source

A Get decoding error, as provided by Fail.

Constructors

DecodingError 

Fields

peConsumed :: ByteOffset

Number of bytes consumed before the error.

peMessage :: String

Error message.

Exports

The following types are re-exported on this module for your convenience:

From Data.Binary
Binary.
From Data.Binary.Get
Get, ByteOffset.
From Data.Binary.Put
Put.

isEndOfBytes :: Monad m => StateT (Producer ByteString m r) m BoolSource

Deprecated: Will be removed as soon as the `pipes-bytestring` library exports it

Checks if the underlying Producer has any bytes left. Leading empty chunks are discarded.