diff options
author | Sam T <sta.cs.vsu@gmail.com> | 2013-04-08 07:01:16 +0400 |
---|---|---|
committer | Sam T <sta.cs.vsu@gmail.com> | 2013-04-08 07:01:16 +0400 |
commit | ff88ce802b9d7f192c79db794592e772853bdc9f (patch) | |
tree | d490695fad036596a98f8fb1c86dfb0e173417b8 | |
parent | 52fb797f692ffc4867a9cfddb9cfdcb0d90bdb71 (diff) |
+ instance for bencoding
-rw-r--r-- | src/Data/Torrent/InfoHash.hs | 18 |
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 #-} | ||
1 | module Data.Torrent.InfoHash | 2 | module 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 | ||
12 | import Control.Applicative | 13 | import Control.Applicative |
13 | import Data.Foldable | 14 | import Data.BEncode |
14 | import Data.List as L | ||
15 | import Data.Char | 15 | import Data.Char |
16 | import Data.List as L | ||
17 | import Data.Foldable | ||
18 | import Data.Map (Map) | ||
19 | import qualified Data.Map as M | ||
16 | import Data.ByteString (ByteString) | 20 | import Data.ByteString (ByteString) |
17 | import qualified Data.ByteString as B | 21 | import qualified Data.ByteString as B |
18 | import qualified Data.ByteString.Char8 as BC | 22 | import 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. |
29 | newtype InfoHash = InfoHash { getInfoHash :: ByteString } | 33 | newtype InfoHash = InfoHash { getInfoHash :: ByteString } |
30 | deriving (Eq, Ord) | 34 | deriving (Eq, Ord, BEncodable) |
31 | 35 | ||
32 | instance Show InfoHash where | 36 | instance 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 | ||
43 | instance 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 | |||
39 | hash :: ByteString -> InfoHash | 51 | hash :: ByteString -> InfoHash |
40 | hash = InfoHash . C.hash | 52 | hash = InfoHash . C.hash |
41 | 53 | ||