Copyright | (c) 2015 Matthew Leon <ml@matthewleon.com> |
---|---|
License | BSD3 |
Maintainer | creichert07@gmail.com |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Extensions | GeneralizedNewtypeDeriving |
Reader monad and combinators for BEncoded data.
This is intended to replace the older Data.BEncode.Parser module.
Usage example:
>>>
:set -XOverloadedStrings
>>>
let bd = (BDict $ Map.fromList [("baz", BInt 1), ("foo", BString "bar")])
>>>
:{
let bReader = do baz <- dict "baz" bint foo <- dict "foo" bstring shouldBeNothing <- optional $ dict "optionalKey" bint return (foo, baz, shouldBeNothing) in runBReader bReader bd :} Right ("bar",1,Nothing)
Reader Monad
Reader monad for extracting data from a BEncoded structure.
runBReader :: BReader a -> BEncode -> Either String a Source #
Run a BReader. See usage examples elsewhere in this file.
Combinators
bbytestring :: BReader ByteString Source #
Usage same as bstring, below. (sadly, doctests for this cause errors on GHC 7.4)
optional :: Alternative f => f a -> f (Maybe a) #
One or none.
list :: BReader a -> BReader [a] Source #
Read a list of BEncoded data
>>>
runBReader (list bint) (BList [BInt 1, BInt 2])
Right [1,2]
>>>
runBReader (list bint) (BList [])
Right []
>>>
let bs = (BList [BList [BString "foo", BString "bar"], BList []])
>>>
runBReader (list $ list bstring) bs
Right [["foo","bar"],[]]
dict :: String -> BReader a -> BReader a Source #
Read the values of a BDict corresponding to a string key
>>>
let bd = (BDict $ Map.fromList [("bar", BInt 2), ("foo", BInt 1)])
>>>
runBReader (dict "foo" bint) bd
Right 1
>>>
:{
let bs = (BList [BDict $ Map.fromList [("baz", BInt 2), ("foo", BString "bar")], BDict $ Map.singleton "foo" (BString "bam")]) in runBReader (list $ dict "foo" bstring) bs :} Right ["bar","bam"]
>>>
:{
let bd = (BDict $ Map.singleton "foo" (BList [ BString "foo", BString "bar" ])) in runBReader (dict "foo" $ list $ bstring) bd :} Right ["foo","bar"]