summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bencoding.cabal3
-rw-r--r--src/Data/BEncode.hs31
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 #-}
45module Data.BEncode 46module Data.BEncode
46 ( -- * Datatype 47 ( -- * Datatype
47 BEncode(..) 48 BEncode(..)
@@ -92,10 +93,11 @@ import qualified Data.ByteString.Builder.Prim as BP (int64Dec, primBounded)
92import Data.Text (Text) 93import Data.Text (Text)
93import qualified Data.Text.Encoding as T 94import qualified Data.Text.Encoding as T
94import Data.Version 95import Data.Version
95import Text.PrettyPrint.ANSI.Leijen (Pretty, Doc, pretty, (<+>), (</>)) 96import Text.PrettyPrint hiding ((<>))
96import qualified Text.PrettyPrint.ANSI.Leijen as PP
97import qualified Text.ParserCombinators.ReadP as ReadP 97import qualified Text.ParserCombinators.ReadP as ReadP
98 98
99
100
99type Dict = Map ByteString BEncode 101type 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 -------------------------------
434printPretty :: BEncode -> IO () 436printPretty :: BEncode -> IO ()
435printPretty = print . pretty 437printPretty = print . ppBEncode
436 438
437ppBS :: ByteString -> Doc 439ppBS :: ByteString -> Doc
438ppBS = PP.string . map w2c . B.unpack 440ppBS = text . map w2c . B.unpack
439 441
440instance Pretty BEncode where 442ppBEncode :: BEncode -> Doc
441 pretty (BInteger i) = PP.integer (fromIntegral i) 443ppBEncode (BInteger i) = int (fromIntegral i)
442 pretty (BString s) = ppBS s 444ppBEncode (BString s) = ppBS s
443 pretty (BList l) = PP.lbracket <+> 445ppBEncode (BList l) = brackets $ hsep (punctuate comma (map ppBEncode l))
444 PP.hsep (PP.punctuate PP.comma (map PP.pretty l)) <+> 446ppBEncode (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 ------------------------------