diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-06-02 07:08:23 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-06-02 07:08:23 +0400 |
commit | cae6c9928e2007ff26cc75bdcdcf91c71747b070 (patch) | |
tree | 785f7162f8465b7c52d93957c248411c3044ce76 /src | |
parent | 8deb9ec7c7d500daa3e84a92de02ae205ada7433 (diff) |
~ Prettify section headings.
Diffstat (limited to 'src')
-rw-r--r-- | src/Data/BEncode.hs | 35 |
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 | |||
284 | dictAssoc = BDict . M.fromList | 284 | dictAssoc = BDict . M.fromList |
285 | {-# INLINE dictAssoc #-} | 285 | {-# INLINE dictAssoc #-} |
286 | 286 | ||
287 | ------------------------------------- Building dictionaries -------------------- | 287 | {-------------------------------------------------------------------- |
288 | Building dictionaries | ||
289 | --------------------------------------------------------------------} | ||
290 | |||
288 | data Assoc = Required ByteString BEncode | 291 | data Assoc = Required ByteString BEncode |
289 | | Optional ByteString (Maybe BEncode) | 292 | | Optional ByteString (Maybe BEncode) |
290 | 293 | ||
@@ -313,7 +316,10 @@ fromAscAssocs :: [Assoc] -> BEncode | |||
313 | fromAscAssocs = BDict . M.fromList . mkAssocs | 316 | fromAscAssocs = BDict . M.fromList . mkAssocs |
314 | {-# INLINE fromAscAssocs #-} | 317 | {-# INLINE fromAscAssocs #-} |
315 | 318 | ||
316 | ------------------------------------ Extraction -------------------------------- | 319 | {-------------------------------------------------------------------- |
320 | Extraction | ||
321 | --------------------------------------------------------------------} | ||
322 | |||
317 | reqKey :: BEncodable a => Map ByteString BEncode -> ByteString -> Result a | 323 | reqKey :: BEncodable a => Map ByteString BEncode -> ByteString -> Result a |
318 | reqKey d key | 324 | reqKey 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 | |||
337 | isInteger :: BEncode -> Bool | 346 | isInteger :: BEncode -> Bool |
338 | isInteger (BInteger _) = True | 347 | isInteger (BInteger _) = True |
339 | isInteger _ = False | 348 | isInteger _ = False |
@@ -354,7 +363,10 @@ isDict (BList _) = True | |||
354 | isDict _ = False | 363 | isDict _ = False |
355 | {-# INLINE isDict #-} | 364 | {-# INLINE isDict #-} |
356 | 365 | ||
357 | --------------------------------------- Encoding ------------------------------- | 366 | {-------------------------------------------------------------------- |
367 | Encoding | ||
368 | --------------------------------------------------------------------} | ||
369 | |||
358 | encode :: BEncode -> Lazy.ByteString | 370 | encode :: BEncode -> Lazy.ByteString |
359 | encode = B.toLazyByteString . builder | 371 | encode = B.toLazyByteString . builder |
360 | 372 | ||
@@ -367,7 +379,10 @@ decoded = decode >=> fromBEncode | |||
367 | encoded :: BEncodable a => a -> Lazy.ByteString | 379 | encoded :: BEncodable a => a -> Lazy.ByteString |
368 | encoded = encode . toBEncode | 380 | encoded = encode . toBEncode |
369 | 381 | ||
370 | -------------------------------------- internals ------------------------------- | 382 | {-------------------------------------------------------------------- |
383 | Internals | ||
384 | --------------------------------------------------------------------} | ||
385 | |||
371 | builder :: BEncode -> B.Builder | 386 | builder :: BEncode -> B.Builder |
372 | builder = go | 387 | builder = 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 | |||
436 | printPretty :: BEncode -> IO () | 454 | printPretty :: BEncode -> IO () |
437 | printPretty = print . ppBEncode | 455 | printPretty = 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 | ||
453 | instance BEncodable Word8 where | 472 | instance BEncodable Word8 where |
454 | {-# SPECIALIZE instance BEncodable Word8 #-} | 473 | {-# SPECIALIZE instance BEncodable Word8 #-} |