summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-06-02 07:08:23 +0400
committerSam T <pxqr.sta@gmail.com>2013-06-02 07:08:23 +0400
commitcae6c9928e2007ff26cc75bdcdcf91c71747b070 (patch)
tree785f7162f8465b7c52d93957c248411c3044ce76 /src
parent8deb9ec7c7d500daa3e84a92de02ae205ada7433 (diff)
~ Prettify section headings.
Diffstat (limited to 'src')
-rw-r--r--src/Data/BEncode.hs35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/Data/BEncode.hs b/src/Data/BEncode.hs
index 953fda3..4b80d4a 100644
--- a/src/Data/BEncode.hs
+++ b/src/Data/BEncode.hs
@@ -284,7 +284,10 @@ dictAssoc :: [(ByteString, BEncode)] -> BEncode
284dictAssoc = BDict . M.fromList 284dictAssoc = BDict . M.fromList
285{-# INLINE dictAssoc #-} 285{-# INLINE dictAssoc #-}
286 286
287------------------------------------- Building dictionaries -------------------- 287{--------------------------------------------------------------------
288 Building dictionaries
289--------------------------------------------------------------------}
290
288data Assoc = Required ByteString BEncode 291data Assoc = Required ByteString BEncode
289 | Optional ByteString (Maybe BEncode) 292 | Optional ByteString (Maybe BEncode)
290 293
@@ -313,7 +316,10 @@ fromAscAssocs :: [Assoc] -> BEncode
313fromAscAssocs = BDict . M.fromList . mkAssocs 316fromAscAssocs = BDict . M.fromList . mkAssocs
314{-# INLINE fromAscAssocs #-} 317{-# INLINE fromAscAssocs #-}
315 318
316------------------------------------ Extraction -------------------------------- 319{--------------------------------------------------------------------
320 Extraction
321--------------------------------------------------------------------}
322
317reqKey :: BEncodable a => Map ByteString BEncode -> ByteString -> Result a 323reqKey :: BEncodable a => Map ByteString BEncode -> ByteString -> Result a
318reqKey d key 324reqKey d key
319 | Just b <- M.lookup key d = fromBEncode b 325 | Just b <- M.lookup key d = fromBEncode b
@@ -333,7 +339,10 @@ optKey d key
333(>--?) = optKey 339(>--?) = optKey
334{-# INLINE (>--?) #-} 340{-# INLINE (>--?) #-}
335 341
336------------------------------------- Predicates ------------------------------- 342{--------------------------------------------------------------------
343 Predicated
344--------------------------------------------------------------------}
345
337isInteger :: BEncode -> Bool 346isInteger :: BEncode -> Bool
338isInteger (BInteger _) = True 347isInteger (BInteger _) = True
339isInteger _ = False 348isInteger _ = False
@@ -354,7 +363,10 @@ isDict (BList _) = True
354isDict _ = False 363isDict _ = False
355{-# INLINE isDict #-} 364{-# INLINE isDict #-}
356 365
357--------------------------------------- Encoding ------------------------------- 366{--------------------------------------------------------------------
367 Encoding
368--------------------------------------------------------------------}
369
358encode :: BEncode -> Lazy.ByteString 370encode :: BEncode -> Lazy.ByteString
359encode = B.toLazyByteString . builder 371encode = B.toLazyByteString . builder
360 372
@@ -367,7 +379,10 @@ decoded = decode >=> fromBEncode
367encoded :: BEncodable a => a -> Lazy.ByteString 379encoded :: BEncodable a => a -> Lazy.ByteString
368encoded = encode . toBEncode 380encoded = encode . toBEncode
369 381
370-------------------------------------- internals ------------------------------- 382{--------------------------------------------------------------------
383 Internals
384--------------------------------------------------------------------}
385
371builder :: BEncode -> B.Builder 386builder :: BEncode -> B.Builder
372builder = go 387builder = go
373 where 388 where
@@ -432,7 +447,10 @@ parser = valueP
432 _ -> P.decimal 447 _ -> P.decimal
433 {-# INLINE integerP #-} 448 {-# INLINE integerP #-}
434 449
435-------------------------------- pretty printing ------------------------------- 450{--------------------------------------------------------------------
451 Pretty Printing
452--------------------------------------------------------------------}
453
436printPretty :: BEncode -> IO () 454printPretty :: BEncode -> IO ()
437printPretty = print . ppBEncode 455printPretty = print . ppBEncode
438 456
@@ -447,8 +465,9 @@ ppBEncode (BDict d) = braces $ vcat (punctuate comma (map ppKV (M.toAscList d
447 where 465 where
448 ppKV (k, v) = ppBS k <+> colon <+> ppBEncode v 466 ppKV (k, v) = ppBS k <+> colon <+> ppBEncode v
449 467
450 468{--------------------------------------------------------------------
451------------------------------- other instances ------------------------------ 469 Other instances
470--------------------------------------------------------------------}
452 471
453instance BEncodable Word8 where 472instance BEncodable Word8 where
454 {-# SPECIALIZE instance BEncodable Word8 #-} 473 {-# SPECIALIZE instance BEncodable Word8 #-}