diff options
-rw-r--r-- | bencoding.cabal | 3 | ||||
-rw-r--r-- | src/Data/BEncode.hs | 31 |
2 files changed, 15 insertions, 19 deletions
diff --git a/bencoding.cabal b/bencoding.cabal index da58260..e3d6639 100644 --- a/bencoding.cabal +++ b/bencoding.cabal | |||
@@ -28,7 +28,7 @@ library | |||
28 | , bytestring >= 0.10.2.0 | 28 | , bytestring >= 0.10.2.0 |
29 | , attoparsec >= 0.10 | 29 | , attoparsec >= 0.10 |
30 | , text >= 0.11 | 30 | , text >= 0.11 |
31 | , ansi-wl-pprint | 31 | , pretty |
32 | 32 | ||
33 | hs-source-dirs: src | 33 | hs-source-dirs: src |
34 | 34 | ||
@@ -55,7 +55,6 @@ test-suite properties | |||
55 | , containers >= 0.4 | 55 | , containers >= 0.4 |
56 | , bytestring >= 0.10.2.0 | 56 | , bytestring >= 0.10.2.0 |
57 | , attoparsec >= 0.10 | 57 | , attoparsec >= 0.10 |
58 | , ansi-wl-pprint | ||
59 | 58 | ||
60 | , test-framework | 59 | , test-framework |
61 | , test-framework-quickcheck2 | 60 | , test-framework-quickcheck2 |
diff --git a/src/Data/BEncode.hs b/src/Data/BEncode.hs index 5097fe8..953fda3 100644 --- a/src/Data/BEncode.hs +++ b/src/Data/BEncode.hs | |||
@@ -42,6 +42,7 @@ | |||
42 | -- This module is considered to be imported qualified. | 42 | -- This module is considered to be imported qualified. |
43 | -- | 43 | -- |
44 | {-# LANGUAGE FlexibleInstances #-} | 44 | {-# LANGUAGE FlexibleInstances #-} |
45 | {-# LANGUAGE Trustworthy #-} | ||
45 | module Data.BEncode | 46 | module Data.BEncode |
46 | ( -- * Datatype | 47 | ( -- * Datatype |
47 | BEncode(..) | 48 | BEncode(..) |
@@ -92,10 +93,11 @@ import qualified Data.ByteString.Builder.Prim as BP (int64Dec, primBounded) | |||
92 | import Data.Text (Text) | 93 | import Data.Text (Text) |
93 | import qualified Data.Text.Encoding as T | 94 | import qualified Data.Text.Encoding as T |
94 | import Data.Version | 95 | import Data.Version |
95 | import Text.PrettyPrint.ANSI.Leijen (Pretty, Doc, pretty, (<+>), (</>)) | 96 | import Text.PrettyPrint hiding ((<>)) |
96 | import qualified Text.PrettyPrint.ANSI.Leijen as PP | ||
97 | import qualified Text.ParserCombinators.ReadP as ReadP | 97 | import qualified Text.ParserCombinators.ReadP as ReadP |
98 | 98 | ||
99 | |||
100 | |||
99 | type Dict = Map ByteString BEncode | 101 | type Dict = Map ByteString BEncode |
100 | 102 | ||
101 | -- | 'BEncode' is straightforward ADT for b-encoded values. | 103 | -- | 'BEncode' is straightforward ADT for b-encoded values. |
@@ -432,23 +434,18 @@ parser = valueP | |||
432 | 434 | ||
433 | -------------------------------- pretty printing ------------------------------- | 435 | -------------------------------- pretty printing ------------------------------- |
434 | printPretty :: BEncode -> IO () | 436 | printPretty :: BEncode -> IO () |
435 | printPretty = print . pretty | 437 | printPretty = print . ppBEncode |
436 | 438 | ||
437 | ppBS :: ByteString -> Doc | 439 | ppBS :: ByteString -> Doc |
438 | ppBS = PP.string . map w2c . B.unpack | 440 | ppBS = text . map w2c . B.unpack |
439 | 441 | ||
440 | instance Pretty BEncode where | 442 | ppBEncode :: BEncode -> Doc |
441 | pretty (BInteger i) = PP.integer (fromIntegral i) | 443 | ppBEncode (BInteger i) = int (fromIntegral i) |
442 | pretty (BString s) = ppBS s | 444 | ppBEncode (BString s) = ppBS s |
443 | pretty (BList l) = PP.lbracket <+> | 445 | ppBEncode (BList l) = brackets $ hsep (punctuate comma (map ppBEncode l)) |
444 | PP.hsep (PP.punctuate PP.comma (map PP.pretty l)) <+> | 446 | ppBEncode (BDict d) = braces $ vcat (punctuate comma (map ppKV (M.toAscList d))) |
445 | PP.rbracket | 447 | where |
446 | pretty (BDict d) = | 448 | ppKV (k, v) = ppBS k <+> colon <+> ppBEncode v |
447 | PP.align $ PP.lbrace <+> | ||
448 | PP.vsep (PP.punctuate PP.comma (map ppKV (M.toAscList d))) </> | ||
449 | PP.rbrace | ||
450 | where | ||
451 | ppKV (k, v) = ppBS k <+> PP.colon <+> PP.pretty v | ||
452 | 449 | ||
453 | 450 | ||
454 | ------------------------------- other instances ------------------------------ | 451 | ------------------------------- other instances ------------------------------ |