]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/FuncTorrent/Bencode.hs
Move Arbitrary instances into another module
[functorrent.git] / src / FuncTorrent / Bencode.hs
index 2b641a7f4b51f57b8ef4f831c6156a20f79c054c..3a982ec0336da0f8ca5a31bbfb73ed4a29b2d78e 100644 (file)
@@ -2,7 +2,7 @@
 module FuncTorrent.Bencode
     (BVal(..)
     , bValToBList
-    , bValToBstr
+    , bValToBytestr
     , bValToInfoDict
     , bValToInteger
     , bstrToString
@@ -16,7 +16,7 @@ import Control.Applicative ((<*))
 import Data.ByteString (ByteString, length, concat)
 import Data.ByteString.Char8 (unpack, pack)
 import Data.Functor ((<$>))
-import Data.Map.Strict (Map, fromList, keys, (!))
+import Data.Map.Strict (Map, fromList, toList)
 import Text.ParserCombinators.Parsec
 import qualified Text.Parsec.ByteString as ParsecBS
 
@@ -31,9 +31,9 @@ bValToInteger :: BVal -> Maybe Integer
 bValToInteger (Bint x) = Just x
 bValToInteger _        = Nothing
 
-bValToBstr :: BVal  -> Maybe ByteString
-bValToBstr (Bstr bs) = Just bs
-bValToBstr _         = Nothing
+bValToBytestr :: BVal  -> Maybe ByteString
+bValToBytestr (Bstr bs) = Just bs
+bValToBytestr _         = Nothing
 
 bValToBList :: BVal    -> Maybe [BVal]
 bValToBList (Blist lst) = Just lst
@@ -44,7 +44,7 @@ bValToInfoDict (Bdict dict) = Just dict
 bValToInfoDict _            = Nothing
 
 bstrToString :: BVal -> Maybe String
-bstrToString bval     = unpack <$> bValToBstr bval
+bstrToString bval     = unpack <$> bValToBytestr bval
 
 -- $setup
 -- >>> import Data.Either
@@ -59,8 +59,7 @@ bstrToString bval     = unpack <$> bValToBstr bval
 -- Right ""
 --
 bencStr :: ParsecBS.Parser ByteString
-bencStr = do _ <- spaces
-             ds <- many1 digit <* char ':'
+bencStr = do ds <- many1 digit <* char ':'
              s <- count (read ds) anyChar
              return (pack s)
 
@@ -83,8 +82,7 @@ bencStr = do _ <- spaces
 -- >>> isLeft $ parse bencInt "Bint" (pack "i002e")
 -- True
 bencInt :: ParsecBS.Parser Integer
-bencInt = do _ <- spaces
-             ds <- between (char 'i') (char 'e') numbers
+bencInt = do ds <- between (char 'i') (char 'e') numbers
              return (read ds)
                where numbers = do d' <- char '-' <|> digit
                                   ds' <- many digit
@@ -106,8 +104,7 @@ bencInt = do _ <- spaces
 -- >>> parse bencList "Blist" (pack "l4:spam4:eggsli42eee")
 -- Right [Bstr "spam",Bstr "eggs",Blist [Bint 42]]
 bencList :: ParsecBS.Parser [BVal]
-bencList = do _ <- spaces
-              between (char 'l') (char 'e') (many bencVal)
+bencList = between (char 'l') (char 'e') (many bencVal)
 
 -- | parse dict
 --
@@ -159,5 +156,9 @@ decode = parse bencVal "BVal"
 encode :: BVal -> ByteString
 encode (Bstr bs) = pack $ show (length bs) ++ ":" ++ unpack bs
 encode (Bint i) = pack $ "i" ++ show i ++ "e"
-encode (Blist xs) = pack $ "l" ++ unpack (concat $ map encode xs) ++ "e"
-encode (Bdict d) = concat [concat ["d", encode . Bstr . pack $ k , encode (d ! k) , "e"] | k <- keys d]
+encode (Blist xs) = concat ["l", concat $ map encode xs, "e"]
+encode (Bdict d) = concat ["d", concat kvlist, "e"]
+    where
+      kvlist :: [ByteString]
+      kvlist = [encPair kv | kv <- toList d]
+      encPair (k, v) = concat [encode . Bstr . pack $ k, encode v]