Portability | non-portable |
---|---|
Stability | stable |
Maintainer | pxqr.sta@gmail.com |
Safe Haskell | Trustworthy |
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.
- data BEncode
- class BEncodable a where
- toBEncode :: a -> BEncode
- fromBEncode :: BEncode -> Result a
- dictAssoc :: [(ByteString, BEncode)] -> BEncode
- type Result = Either String
- (-->) :: BEncodable a => ByteString -> a -> Assoc
- (-->?) :: BEncodable a => ByteString -> Maybe a -> Assoc
- fromAssocs :: [Assoc] -> BEncode
- fromAscAssocs :: [Assoc] -> BEncode
- reqKey :: BEncodable a => Map ByteString BEncode -> ByteString -> Result a
- optKey :: BEncodable a => Map ByteString BEncode -> ByteString -> Result (Maybe a)
- (>--) :: BEncodable a => Map ByteString BEncode -> ByteString -> Result a
- (>--?) :: BEncodable a => Map ByteString BEncode -> ByteString -> Result (Maybe a)
- encode :: BEncode -> ByteString
- decode :: ByteString -> Result BEncode
- encoded :: BEncodable a => a -> ByteString
- decoded :: BEncodable a => ByteString -> Result a
- isInteger :: BEncode -> Bool
- isString :: BEncode -> Bool
- isList :: BEncode -> Bool
- isDict :: BEncode -> Bool
- builder :: BEncode -> Builder
- parser :: Parser BEncode
- decodingError :: String -> Result a
- printPretty :: BEncode -> IO ()
Datatype
Construction && Destructuring
class BEncodable a whereSource
BEncodable Bool | |
BEncodable Int | |
BEncodable Integer | |
BEncodable Word | |
BEncodable Word8 | |
BEncodable Word16 | |
BEncodable Word32 | |
BEncodable Word64 | |
BEncodable () | |
BEncodable ByteString | |
BEncodable Text | |
BEncodable Version | |
BEncodable BEncode | |
BEncodable a => BEncodable [a] | |
(Eq a, BEncodable a) => BEncodable (Set a) | |
(BEncodable a, BEncodable b) => BEncodable (a, b) | |
BEncodable a => BEncodable (Map ByteString a) | |
(BEncodable a, BEncodable b, BEncodable c) => BEncodable (a, b, c) | |
(BEncodable a, BEncodable b, BEncodable c, BEncodable d) => BEncodable (a, b, c, d) | |
(BEncodable a, BEncodable b, BEncodable c, BEncodable d, BEncodable e) => BEncodable (a, b, c, d, e) |
dictAssoc :: [(ByteString, BEncode)] -> BEncodeSource
Dictionaries
Building
(-->) :: BEncodable a => ByteString -> a -> AssocSource
(-->?) :: BEncodable a => ByteString -> Maybe a -> AssocSource
fromAssocs :: [Assoc] -> BEncodeSource
fromAscAssocs :: [Assoc] -> BEncodeSource
A faster version of fromAssocs
.
Should be used only when keys are sorted by ascending.
Extraction
reqKey :: BEncodable a => Map ByteString BEncode -> ByteString -> Result aSource
optKey :: BEncodable a => Map ByteString BEncode -> ByteString -> Result (Maybe a)Source
(>--) :: BEncodable a => Map ByteString BEncode -> ByteString -> Result aSource
(>--?) :: BEncodable a => Map ByteString BEncode -> ByteString -> Result (Maybe a)Source
Serialization
encode :: BEncode -> ByteStringSource
decode :: ByteString -> Result BEncodeSource
encoded :: BEncodable a => a -> ByteStringSource
decoded :: BEncodable a => ByteString -> Result aSource
Predicates
Extra
decodingError :: String -> Result aSource
printPretty :: BEncode -> IO ()Source