bencoding-0.1.0.0: A library for encoding and decoding of BEncode data.

Portabilitynon-portable
Stabilitystable
Maintainerpxqr.sta@gmail.com
Safe HaskellTrustworthy

Data.BEncode

Contents

Description

This module provides convinient and fast way to serialize, deserealize and construct/destructure Bencoded values with optional fields.

It supports four different types of values:

  • byte strings — represented as ByteString;
  • integers — represented as Integer;
  • lists - represented as ordinary lists;
  • dictionaries — represented as Map;

To serialize any other types we need to make conversion. To make conversion more convenient there is type class for it: BEncodable. Any textual strings are considered as UTF8 encoded Text.

The complete Augmented BNF syntax for bencoding format is:

 <BE>    ::= <DICT> | <LIST> | <INT> | <STR>

 <DICT>  ::= "d" 1 * (<STR> <BE>) "e"
 <LIST>  ::= "l" 1 * <BE>         "e"
 <INT>   ::= "i"     <SNUM>       "e"
 <STR>   ::= <NUM> ":" n * <CHAR>; where n equals the <NUM>

 <SNUM>  ::= "-" <NUM> / <NUM>
 <NUM>   ::= 1 * <DIGIT>
 <CHAR>  ::= %
 <DIGIT> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

This module is considered to be imported qualified.

Synopsis

Datatype

data BEncode Source

BEncode is straightforward ADT for b-encoded values. Please note that since dictionaries are sorted, in most cases we can compare BEncoded values without serialization and vice versa. Lists is not required to be sorted through. Also note that BEncode have JSON-like instance for Pretty.

Constructors

BInteger !Int64 
BString !ByteString 
BList [BEncode] 
BDict Dict 

Construction && Destructuring

Dictionaries

Building

(-->) :: BEncodable a => ByteString -> a -> AssocSource

(-->?) :: BEncodable a => ByteString -> Maybe a -> AssocSource

fromAscAssocs :: [Assoc] -> BEncodeSource

A faster version of fromAssocs. Should be used only when keys are sorted by ascending.

Extraction

Serialization

Predicates

Extra

parser :: Parser BEncodeSource

TODO try to replace peekChar with something else