summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam T <sta.cs.vsu@gmail.com>2013-04-08 07:01:16 +0400
committerSam T <sta.cs.vsu@gmail.com>2013-04-08 07:01:16 +0400
commitff88ce802b9d7f192c79db794592e772853bdc9f (patch)
treed490695fad036596a98f8fb1c86dfb0e173417b8 /src
parent52fb797f692ffc4867a9cfddb9cfdcb0d90bdb71 (diff)
+ instance for bencoding
Diffstat (limited to 'src')
-rw-r--r--src/Data/Torrent/InfoHash.hs18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs
index f120d46a..a5ba3786 100644
--- a/src/Data/Torrent/InfoHash.hs
+++ b/src/Data/Torrent/InfoHash.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE GeneralizedNewtypeDeriving, FlexibleInstances #-}
1module Data.Torrent.InfoHash 2module Data.Torrent.InfoHash
2 ( InfoHash (getInfoHash) 3 ( InfoHash (getInfoHash)
3 , addHashToURI 4 , addHashToURI
@@ -10,9 +11,12 @@ module Data.Torrent.InfoHash
10 ) where 11 ) where
11 12
12import Control.Applicative 13import Control.Applicative
13import Data.Foldable 14import Data.BEncode
14import Data.List as L
15import Data.Char 15import Data.Char
16import Data.List as L
17import Data.Foldable
18import Data.Map (Map)
19import qualified Data.Map as M
16import Data.ByteString (ByteString) 20import Data.ByteString (ByteString)
17import qualified Data.ByteString as B 21import qualified Data.ByteString as B
18import qualified Data.ByteString.Char8 as BC 22import qualified Data.ByteString.Char8 as BC
@@ -27,7 +31,7 @@ import Numeric
27 31
28-- | Exactly 20 bytes long SHA1 hash. 32-- | Exactly 20 bytes long SHA1 hash.
29newtype InfoHash = InfoHash { getInfoHash :: ByteString } 33newtype InfoHash = InfoHash { getInfoHash :: ByteString }
30 deriving (Eq, Ord) 34 deriving (Eq, Ord, BEncodable)
31 35
32instance Show InfoHash where 36instance Show InfoHash where
33 show = BC.unpack . ppHex 37 show = BC.unpack . ppHex
@@ -36,6 +40,14 @@ instance Serialize InfoHash where
36 put = putByteString . getInfoHash 40 put = putByteString . getInfoHash
37 get = InfoHash <$> getBytes 20 41 get = InfoHash <$> getBytes 20
38 42
43instance BEncodable a => BEncodable (Map InfoHash a) where
44 {-# SPECIALIZE instance BEncodable a => BEncodable (Map InfoHash a) #-}
45 fromBEncode b = M.mapKeys InfoHash <$> fromBEncode b
46 {-# INLINE fromBEncode #-}
47
48 toBEncode = toBEncode . M.mapKeys getInfoHash
49 {-# INLINE toBEncode #-}
50
39hash :: ByteString -> InfoHash 51hash :: ByteString -> InfoHash
40hash = InfoHash . C.hash 52hash = InfoHash . C.hash
41 53