-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | A library for encoding and decoding of BEncode data. -- -- A library for fast and easy encoding and decoding of BEncode data. @package bencoding @version 0.4.4.0 -- | This module defines a simple key/value list ordered by keys which both -- faster and more suitable for bencode dictionaries than just [(k,v)]. module Data.BEncode.BDict type BKey = ByteString -- | BDictMap is an ascending list of key/value pairs sorted by keys. data BDictMap a Cons :: !BKey -> a -> !(BDictMap a) -> BDictMap a Nil :: BDictMap a -- | O(1). The empty dicionary. empty :: BDictMap a -- | O(1). Dictionary of one key-value pair. singleton :: BKey -> a -> BDictMap a -- | O(1). Is the dictionary empty? null :: BDictMap a -> Bool -- | O(n). Is the key a member of the dictionary? member :: BKey -> BDictMap a -> Bool -- | O(n). Lookup the value at a key in the dictionary. lookup :: BKey -> BDictMap a -> Maybe a -- | O(n + m). Merge two dictionaries by taking pair from both given -- dictionaries. Dublicated keys are not filtered. union :: BDictMap a -> BDictMap a -> BDictMap a -- | O(n). Map a function over all values in the dictionary. map :: (a -> b) -> BDictMap a -> BDictMap b -- | O(n). Map a function over all keys/value pairs in the -- dictionary. mapWithKey :: (BKey -> a -> b) -> BDictMap a -> BDictMap b -- | O(n). Map each key/value pair to a monoid and fold resulting -- sequnce using mappend. foldMapWithKey :: Monoid m => (BKey -> a -> m) -> BDictMap a -> m -- | O(n). Map each key/value pair to a monoid and fold resulting -- sequnce using mappend. -- | Deprecated: Use foldMapWithKey instead bifoldMap :: Monoid m => (BKey -> a -> m) -> BDictMap a -> m -- | O(n). Build a dictionary from a list of key/value pairs where -- the keys are in ascending order. fromAscList :: [(BKey, a)] -> BDictMap a -- | O(n). Convert the dictionary to a list of key/value pairs where -- the keys are in ascending order. toAscList :: BDictMap a -> [(BKey, a)] instance GHC.Generics.Generic (Data.BEncode.BDict.BDictMap a) instance GHC.Classes.Ord a => GHC.Classes.Ord (Data.BEncode.BDict.BDictMap a) instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.BEncode.BDict.BDictMap a) instance GHC.Read.Read a => GHC.Read.Read (Data.BEncode.BDict.BDictMap a) instance GHC.Show.Show a => GHC.Show.Show (Data.BEncode.BDict.BDictMap a) instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Data.BEncode.BDict.BDictMap a) instance GHC.Base.Functor Data.BEncode.BDict.BDictMap instance Data.Foldable.Foldable Data.BEncode.BDict.BDictMap instance GHC.Base.Semigroup (Data.BEncode.BDict.BDictMap a) instance GHC.Base.Monoid (Data.BEncode.BDict.BDictMap a) -- | Types for working with bencode data. module Data.BEncode.Types -- | A bencode "integer". type BInteger = Integer -- | A raw bencode string. type BString = ByteString -- | A plain bencode list. type BList = [BValue] -- | A bencode dictionary. type BDict = BDictMap BValue -- | BValue 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. data BValue -- | bencode integers; BInteger :: !BInteger -> BValue -- | bencode strings; BString :: !BString -> BValue -- | list of bencode values; BList :: BList -> BValue -- | bencode key-value dictionary. BDict :: BDict -> BValue -- | Test if bencoded value is an integer. isInteger :: BValue -> Bool -- | Test if bencoded value is a string, both raw and utf8 encoded. isString :: BValue -> Bool -- | Test if bencoded value is a list. isList :: BValue -> Bool -- | Test if bencoded value is a dictionary. isDict :: BValue -> Bool instance GHC.Generics.Generic Data.BEncode.Types.BValue instance GHC.Classes.Ord Data.BEncode.Types.BValue instance GHC.Classes.Eq Data.BEncode.Types.BValue instance GHC.Read.Read Data.BEncode.Types.BValue instance GHC.Show.Show Data.BEncode.Types.BValue instance Control.DeepSeq.NFData Data.BEncode.Types.BValue -- | This module provides bencode values serialization. Normally, you don't -- need to import this module, use BEncode instead. module Data.BEncode.Internal -- | BEncode format parser according to specification. parser :: Parser BValue -- | Try to convert raw bytestring to bencoded value according to -- specification. parse :: ByteString -> Either String BValue -- | BEncode format encoder according to specification. builder :: BValue -> Builder -- | Convert bencoded value to raw bytestring according to the -- specification. build :: BValue -> ByteString -- | Convert to easily readable JSON-like document. Typically used for -- debugging purposes. ppBEncode :: BValue -> Doc -- | 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: -- -- -- -- To serialize any other types we need to make conversion. To make -- conversion more convenient there is type class for it: BEncode. -- 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, for example: -- --
--   import Data.BEncode as BE
--   
module Data.BEncode -- | BValue 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. data BValue -- | bencode integers; BInteger :: !BInteger -> BValue -- | bencode strings; BString :: !BString -> BValue -- | list of bencode values; BList :: BList -> BValue -- | bencode key-value dictionary. BDict :: BDict -> BValue -- | This class is used to define new datatypes that could be easily -- serialized using bencode format. -- -- By default BEncode have a generic implementation; suppose the -- following datatype: -- --
--   data List a = C { _head  :: a
--                   , __tail :: List a }
--               | N
--                 deriving Generic
--   
-- -- If we don't need to obey any particular specification or standard, the -- default instance could be derived automatically from the -- Generic instance: -- --
--   instance BEncode a => BEncode (List a)
--   
-- -- Example of derived toBEncode result: -- --
--   > toBEncode (C 123 $ C 1 N)
--   BDict (fromList [("head",BInteger 123),("tail",BList [])])
--   
-- -- Note that prefixed underscore characters are omitted since they are -- usually used for lens. class BEncode a -- | See an example of implementation here Assoc toBEncode :: BEncode a => a -> BValue -- | See an example of implementation here Assoc toBEncode :: (BEncode a, Generic a) => GBEncodable (Rep a) BValue => a -> BValue -- | See an example of implementation here Get. fromBEncode :: BEncode a => BValue -> Result a -- | See an example of implementation here Get. fromBEncode :: (BEncode a, Generic a) => GBEncodable (Rep a) BValue => BValue -> Result a -- | Encode a value using bencode format to a lazy ByteString. encode :: BEncode a => a -> ByteString -- | Decode a value from a strict ByteString using bencode format. decode :: BEncode a => ByteString -> Result a -- | Assoc used to easily build dictionaries with required and -- optional keys. Suppose we have we following datatype we want to -- serialize: -- --
--   data FileInfo = FileInfo
--     { fileLength :: Integer
--     , fileMD5sum :: Maybe ByteString
--     , filePath   :: [ByteString]
--     , fileTags   :: Maybe [Text]
--     } deriving (Show, Read, Eq)
--   
--   
-- -- We need to make instance BEncode FileInfo, though we -- don't want to check the both Maybes manually. The more -- declarative and convenient way to define the toBEncode method -- is to use dictionary builders: -- --
--   instance BEncode FileInfo where
--     toBEncode FileInfo {..} = toDict $
--          "length" .=! fileLength
--       .: "md5sum" .=? fileMD5sum
--       .: "path"   .=! filePath
--       .: "tags"   .=? fileTags
--       .: endDict
--   
--   
-- -- NOTE: the list of pairs MUST be sorted lexicographically by keys, like -- so: -- -- "length" < "md5sum" < "path" < "tags" data Assoc -- | Make required key value pair. (.=!) :: BEncode a => BKey -> a -> Assoc infix 6 .=! -- | Like the (.=!) operator but if the value is not present then -- the key do not appear in resulting bencode dictionary. (.=?) :: BEncode a => BKey -> Maybe a -> Assoc infix 6 .=? -- | Cons a key/value pair. (.:) :: Assoc -> BDict -> BDict infixr 5 .: -- | Used to specify end of dictionary. See Assoc. endDict :: BDict -- | Make a bencode value from dictionary description. toDict :: BDict -> BValue -- | Dictionary extractor are similar to dictionary builders, but play the -- opposite role: they are used to define fromBEncode method in -- declarative style. Using the same FileInfo datatype the -- fromBEncode function instance looks like: -- --
--   instance BEncode FileInfo where
--     fromBEncode = fromDict $ do
--       FileInfo <$>! "length"
--                <*>? "md5sum"
--                <*>! "path"
--                <*>? "tags"
--   
--   
-- -- The reqKey is used to extract required key — if lookup is -- failed then whole destructuring fail. -- -- NOTE: the actions MUST be sorted lexicographically by keys, like so: -- -- "length" < "md5sum" < "path" < "tags" data Get a -- | Result used in decoding operations. type Result = Either String -- | Typically used to throw an decoding error in fromBEncode; when BEncode -- value to match expected value. decodingError :: String -> Result a -- | Run a Get monad. See Get for usage. fromDict :: forall a. Typeable a => Get a -> BValue -> Result a -- | Run action, but return without consuming and key/value pair. Fails if -- the action fails. lookAhead :: Get a -> Get a -- | Get lexicographical successor of the current key/value pair. next :: Get BValue -- | Extract required value from the given key. req :: BKey -> Get BValue -- | Extract optional value from the given key. opt :: BKey -> Get (Maybe BValue) -- | Reconstruct a bencodable value from bencode value. field :: BEncode a => Get BValue -> Get a -- | Match key with value. match :: BKey -> BValue -> Get () -- | Shorthand for: f <$> field (req -- k). (<$>!) :: BEncode a => (a -> b) -> BKey -> Get b infixl 4 <$>! -- | Shorthand for: f <$> optional (field -- (req k)). (<$>?) :: BEncode a => (Maybe a -> b) -> BKey -> Get b infixl 4 <$>? -- | Shorthand for: f <*> field (req -- k). (<*>!) :: BEncode a => Get (a -> b) -> BKey -> Get b infixl 4 <*>! -- | Shorthand for: f <*> optional (field -- (req k)). (<*>?) :: BEncode a => Get (Maybe a -> b) -> BKey -> Get b infixl 4 <*>? instance GHC.Base.Alternative Data.BEncode.Get instance GHC.Base.Applicative Data.BEncode.Get instance GHC.Base.Functor Data.BEncode.Get instance GHC.Base.Monad Data.BEncode.Get instance Data.BEncode.BEncode f => Data.BEncode.GBEncodable (GHC.Generics.K1 GHC.Generics.R f) Data.BEncode.Types.BValue instance Data.BEncode.BEncode Data.BEncode.Types.BValue instance Data.BEncode.BEncode Data.BEncode.Types.BInteger instance Data.BEncode.BEncode Data.BEncode.Types.BString instance Data.BEncode.BEncode Data.BEncode.Types.BDict instance Data.BEncode.BEncode GHC.Word.Word8 instance Data.BEncode.BEncode GHC.Word.Word16 instance Data.BEncode.BEncode GHC.Word.Word32 instance Data.BEncode.BEncode GHC.Word.Word64 instance Data.BEncode.BEncode GHC.Types.Word instance Data.BEncode.BEncode GHC.Int.Int8 instance Data.BEncode.BEncode GHC.Int.Int16 instance Data.BEncode.BEncode GHC.Int.Int32 instance Data.BEncode.BEncode GHC.Int.Int64 instance Data.BEncode.BEncode GHC.Types.Int instance Data.BEncode.BEncode GHC.Types.Bool instance Data.BEncode.BEncode Data.Text.Internal.Text instance Data.BEncode.BEncode a => Data.BEncode.BEncode [a] instance Data.BEncode.BEncode Data.Version.Version instance Data.BEncode.BEncode () instance (Data.BEncode.BEncode a, Data.BEncode.BEncode b) => Data.BEncode.BEncode (a, b) instance (Data.BEncode.BEncode a, Data.BEncode.BEncode b, Data.BEncode.BEncode c) => Data.BEncode.BEncode (a, b, c) instance (Data.BEncode.BEncode a, Data.BEncode.BEncode b, Data.BEncode.BEncode c, Data.BEncode.BEncode d) => Data.BEncode.BEncode (a, b, c, d) instance (Data.BEncode.BEncode a, Data.BEncode.BEncode b, Data.BEncode.BEncode c, Data.BEncode.BEncode d, Data.BEncode.BEncode e) => Data.BEncode.BEncode (a, b, c, d, e) instance (GHC.Classes.Eq e, GHC.Base.Monoid e) => Data.BEncode.GBEncodable GHC.Generics.U1 e instance (Data.BEncode.GBEncodable a Data.BEncode.Types.BList, Data.BEncode.GBEncodable b Data.BEncode.Types.BList) => Data.BEncode.GBEncodable (a GHC.Generics.:*: b) Data.BEncode.Types.BList instance (Data.BEncode.GBEncodable a Data.BEncode.Types.BDict, Data.BEncode.GBEncodable b Data.BEncode.Types.BDict) => Data.BEncode.GBEncodable (a GHC.Generics.:*: b) Data.BEncode.Types.BDict instance (Data.BEncode.GBEncodable a e, Data.BEncode.GBEncodable b e) => Data.BEncode.GBEncodable (a GHC.Generics.:+: b) e instance (GHC.Generics.Selector s, Data.BEncode.GBEncodable f Data.BEncode.Types.BValue) => Data.BEncode.GBEncodable (GHC.Generics.M1 GHC.Generics.S s f) Data.BEncode.Types.BDict instance Data.BEncode.GBEncodable f Data.BEncode.Types.BValue => Data.BEncode.GBEncodable (GHC.Generics.M1 GHC.Generics.S s f) Data.BEncode.Types.BList instance (GHC.Generics.Constructor c, Data.BEncode.GBEncodable f Data.BEncode.Types.BDict, Data.BEncode.GBEncodable f Data.BEncode.Types.BList) => Data.BEncode.GBEncodable (GHC.Generics.M1 GHC.Generics.C c f) Data.BEncode.Types.BValue instance Data.BEncode.GBEncodable f e => Data.BEncode.GBEncodable (GHC.Generics.M1 GHC.Generics.D d f) e