summaryrefslogtreecommitdiff
path: root/src/Data/Torrent/InfoHash.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-11-23 03:37:15 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-11-23 03:37:15 +0400
commited25a297094b483dce06e14d52ced2f93f6dca41 (patch)
tree3df60d8b30af29345de021f251903a175e78c70c /src/Data/Torrent/InfoHash.hs
parent708938679954a5d5f9def82e1fa873c19ed662df (diff)
Use pretty-class package
Diffstat (limited to 'src/Data/Torrent/InfoHash.hs')
-rw-r--r--src/Data/Torrent/InfoHash.hs13
1 files changed, 6 insertions, 7 deletions
diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs
index 2e3946eb..e15fdbb3 100644
--- a/src/Data/Torrent/InfoHash.hs
+++ b/src/Data/Torrent/InfoHash.hs
@@ -19,11 +19,9 @@ module Data.Torrent.InfoHash
19 -- * Rendering 19 -- * Rendering
20 , longHex 20 , longHex
21 , shortHex 21 , shortHex
22 , ppInfoHash
23 22
24 , addHashToURI 23 , addHashToURI
25 24
26
27 , Data.Torrent.InfoHash.hash 25 , Data.Torrent.InfoHash.hash
28 , Data.Torrent.InfoHash.hashlazy 26 , Data.Torrent.InfoHash.hashlazy
29 ) where 27 ) where
@@ -52,6 +50,7 @@ import Network.URI
52import Numeric 50import Numeric
53import Text.ParserCombinators.ReadP as P 51import Text.ParserCombinators.ReadP as P
54import Text.PrettyPrint 52import Text.PrettyPrint
53import Text.PrettyPrint.Class
55 54
56 55
57-- | Exactly 20 bytes long SHA1 hash of the info part of torrent file. 56-- | Exactly 20 bytes long SHA1 hash of the info part of torrent file.
@@ -60,7 +59,7 @@ newtype InfoHash = InfoHash { getInfoHash :: BS.ByteString }
60 59
61-- | for hex encoded strings 60-- | for hex encoded strings
62instance Show InfoHash where 61instance Show InfoHash where
63 show = render . ppInfoHash 62 show = render . pretty
64 63
65-- | for hex encoded strings 64-- | for hex encoded strings
66instance Read InfoHash where 65instance Read InfoHash where
@@ -106,6 +105,10 @@ instance FromJSON InfoHash where
106instance URLShow InfoHash where 105instance URLShow InfoHash where
107 urlShow = show 106 urlShow = show
108 107
108-- | base16 encoded.
109instance Pretty InfoHash where
110 pretty = text . BC.unpack . ppHex . getInfoHash
111
109-- | Tries both base16 and base32 while decoding info hash. 112-- | Tries both base16 and base32 while decoding info hash.
110textToInfoHash :: Text -> Maybe InfoHash 113textToInfoHash :: Text -> Maybe InfoHash
111textToInfoHash text 114textToInfoHash text
@@ -127,10 +130,6 @@ longHex = T.decodeUtf8 . Base16.encode . getInfoHash
127shortHex :: InfoHash -> Text 130shortHex :: InfoHash -> Text
128shortHex = T.take 7 . longHex 131shortHex = T.take 7 . longHex
129 132
130-- | Pretty print info hash in hexadecimal format.
131ppInfoHash :: InfoHash -> Doc
132ppInfoHash = text . BC.unpack . ppHex . getInfoHash
133
134ppHex :: BS.ByteString -> BS.ByteString 133ppHex :: BS.ByteString -> BS.ByteString
135ppHex = BL.toStrict . B.toLazyByteString . B.byteStringHexFixed 134ppHex = BL.toStrict . B.toLazyByteString . B.byteStringHexFixed
136 135