summaryrefslogtreecommitdiff
path: root/src/Data/BEncode.hs
diff options
context:
space:
mode:
authorSam T <sta.cs.vsu@gmail.com>2013-04-03 00:09:54 +0400
committerSam T <sta.cs.vsu@gmail.com>2013-04-03 00:09:54 +0400
commitc508066a6f4d2a0e267958aecf4ded609e1598b9 (patch)
tree23adfbbe60b775a986156413876ff3b6b772f6c0 /src/Data/BEncode.hs
parentf0a9f8dcb96bc0f012d291fc43a23c2704b79f1a (diff)
cosmetic changes
Diffstat (limited to 'src/Data/BEncode.hs')
-rw-r--r--src/Data/BEncode.hs41
1 files changed, 35 insertions, 6 deletions
diff --git a/src/Data/BEncode.hs b/src/Data/BEncode.hs
index 357f63f..c844858 100644
--- a/src/Data/BEncode.hs
+++ b/src/Data/BEncode.hs
@@ -16,6 +16,7 @@ module Data.BEncode
16 16
17 -- * Serialization 17 -- * Serialization
18 , encode, decode 18 , encode, decode
19 , encoded, decoded
19 20
20 -- * Extra 21 -- * Extra
21 , builder, parser, decodingError, printPretty 22 , builder, parser, decodingError, printPretty
@@ -26,6 +27,7 @@ module Data.BEncode
26 27
27 28
28import Control.Applicative 29import Control.Applicative
30import Control.Monad
29import Data.Int 31import Data.Int
30import Data.Maybe (mapMaybe) 32import Data.Maybe (mapMaybe)
31import Data.Monoid ((<>)) 33import Data.Monoid ((<>))
@@ -66,6 +68,8 @@ type Result = Either String
66class BEncodable a where 68class BEncodable a where
67 toBEncode :: a -> BEncode 69 toBEncode :: a -> BEncode
68 fromBEncode :: BEncode -> Result a 70 fromBEncode :: BEncode -> Result a
71
72
69-- isEncodable :: BEncode -> Bool 73-- isEncodable :: BEncode -> Bool
70-- bencoding :: Iso a 74-- bencoding :: Iso a
71-- bencoding = Iso (Right . toBencode) fromBEncode 75-- bencoding = Iso (Right . toBencode) fromBEncode
@@ -129,7 +133,7 @@ instance BEncodable Text where
129 133
130 134
131{- 135{-
132instance BEncodable String where 136instance BEncodable Stringwhere
133 toBEncode = BString . BC.pack 137 toBEncode = BString . BC.pack
134 {-# INLINE toBEncode #-} 138 {-# INLINE toBEncode #-}
135 139
@@ -174,17 +178,22 @@ key --> val = Required key (toBEncode val)
174key -->? mval = Optional key (toBEncode <$> mval) 178key -->? mval = Optional key (toBEncode <$> mval)
175{-# INLINE (-->?) #-} 179{-# INLINE (-->?) #-}
176 180
177fromAssocs :: [Assoc] -> BEncode 181mkAssocs :: [Assoc] -> [(ByteString, BEncode)]
178fromAssocs = BDict . M.fromList . mapMaybe unpackAssoc 182mkAssocs = mapMaybe unpackAssoc
179 where 183 where
180 unpackAssoc (Required n v) = Just (n, v) 184 unpackAssoc (Required n v) = Just (n, v)
181 unpackAssoc (Optional n (Just v)) = Just (n, v) 185 unpackAssoc (Optional n (Just v)) = Just (n, v)
182 unpackAssoc (Optional _ Nothing) = Nothing 186 unpackAssoc (Optional _ Nothing) = Nothing
183 187
188fromAssocs :: [Assoc] -> BEncode
189fromAssocs = BDict . M.fromList . mkAssocs
190{-# INLINE fromAssocs #-}
191
184-- | A faster version of 'fromAssocs'. 192-- | A faster version of 'fromAssocs'.
185-- Should be used only when keys are sorted by ascending. 193-- Should be used only when keys are sorted by ascending.
186fromAscAssocs :: [Assoc] -> BEncode 194fromAscAssocs :: [Assoc] -> BEncode
187fromAscAssocs = error "fromAscAssocs" 195fromAscAssocs = BDict . M.fromList . mkAssocs
196{-# INLINE fromAscAssocs #-}
188 197
189------------------------------------ Extraction -------------------------------- 198------------------------------------ Extraction --------------------------------
190reqKey :: BEncodable a => Map ByteString BEncode -> ByteString -> Result a 199reqKey :: BEncodable a => Map ByteString BEncode -> ByteString -> Result a
@@ -227,12 +236,19 @@ isDict (BList _) = True
227isDict _ = False 236isDict _ = False
228{-# INLINE isDict #-} 237{-# INLINE isDict #-}
229 238
239--------------------------------------- Encoding -------------------------------
230encode :: BEncode -> Lazy.ByteString 240encode :: BEncode -> Lazy.ByteString
231encode = B.toLazyByteString . builder 241encode = B.toLazyByteString . builder
232 242
233decode :: ByteString -> Either String BEncode 243decode :: ByteString -> Result BEncode
234decode = P.parseOnly parser 244decode = P.parseOnly parser
235 245
246decoded :: BEncodable a => ByteString -> Result a
247decoded = decode >=> fromBEncode
248
249encoded :: BEncodable a => a -> Lazy.ByteString
250encoded = encode . toBEncode
251
236 252
237builder :: BEncode -> B.Builder 253builder :: BEncode -> B.Builder
238builder = go 254builder = go
@@ -255,7 +271,7 @@ builder = go
255 B.byteString s 271 B.byteString s
256 {-# INLINE buildString #-} 272 {-# INLINE buildString #-}
257 273
258 274-- | todo zepto
259parser :: Parser BEncode 275parser :: Parser BEncode
260parser = valueP 276parser = valueP
261 where 277 where
@@ -288,6 +304,19 @@ parser = valueP
288 {-# INLINE integerP #-} 304 {-# INLINE integerP #-}
289 305
290 306
307-- | Extract raw field from the dict.
308-- Useful for info hash extraction.
309--rawLookup :: ByteString -> Result ByteString
310--rawLookup key = P.parseOnly (P.char 'd' >> go)
311-- where
312-- - go = do
313-- s <- stringP
314-- if s == key
315-- then (
316-- else parser >> go
317
318
319-------------------------------- pretty printing -------------------------------
291printPretty :: BEncode -> IO () 320printPretty :: BEncode -> IO ()
292printPretty = print . pretty 321printPretty = print . pretty
293 322