diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-07-15 22:57:02 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-07-15 22:57:02 +0400 |
commit | 8101fa8a4286e779c45e17ac2f2b86e91a9f3b0a (patch) | |
tree | cd4162c73b9a95a79730cae285bf666dbf317456 /src/Data/Torrent.hs | |
parent | 33da2f459c0819b694f2ef21dba6d09167a5af6a (diff) |
~ Add read instance for infohash.
Diffstat (limited to 'src/Data/Torrent.hs')
-rw-r--r-- | src/Data/Torrent.hs | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index 3f555a5b..2ebe9374 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs | |||
@@ -62,6 +62,7 @@ import Prelude hiding (sum) | |||
62 | import Control.Applicative | 62 | import Control.Applicative |
63 | import Control.Arrow | 63 | import Control.Arrow |
64 | import Control.Exception | 64 | import Control.Exception |
65 | import Control.Monad | ||
65 | 66 | ||
66 | import qualified Crypto.Hash.SHA1 as C | 67 | import qualified Crypto.Hash.SHA1 as C |
67 | 68 | ||
@@ -69,8 +70,6 @@ import Data.Aeson.TH | |||
69 | import Data.BEncode as BE | 70 | import Data.BEncode as BE |
70 | import Data.Char | 71 | import Data.Char |
71 | import Data.Foldable | 72 | import Data.Foldable |
72 | import Data.Map (Map) | ||
73 | import qualified Data.Map as M | ||
74 | import qualified Data.ByteString as B | 73 | import qualified Data.ByteString as B |
75 | import Data.ByteString.Internal | 74 | import Data.ByteString.Internal |
76 | import qualified Data.ByteString.Char8 as BC (pack, unpack) | 75 | import qualified Data.ByteString.Char8 as BC (pack, unpack) |
@@ -78,10 +77,15 @@ import qualified Data.ByteString.Lazy as Lazy | |||
78 | import qualified Data.ByteString.Lazy.Builder as B | 77 | import qualified Data.ByteString.Lazy.Builder as B |
79 | import qualified Data.ByteString.Lazy.Builder.ASCII as B | 78 | import qualified Data.ByteString.Lazy.Builder.ASCII as B |
80 | import qualified Data.List as L | 79 | import qualified Data.List as L |
80 | import Data.Maybe | ||
81 | import Data.Map (Map) | ||
82 | import qualified Data.Map as M | ||
81 | import Data.Hashable as Hashable | 83 | import Data.Hashable as Hashable |
82 | import Data.Text (Text) | 84 | import Data.Text (Text) |
83 | import Data.Serialize as S hiding (Result) | 85 | import Data.Serialize as S hiding (Result) |
84 | import Text.PrettyPrint | 86 | import Text.PrettyPrint |
87 | import Text.ParserCombinators.ReadP as P | ||
88 | import Text.Read | ||
85 | 89 | ||
86 | import Network.URI | 90 | import Network.URI |
87 | import System.FilePath | 91 | import System.FilePath |
@@ -98,6 +102,17 @@ newtype InfoHash = InfoHash { getInfoHash :: ByteString } | |||
98 | instance Show InfoHash where | 102 | instance Show InfoHash where |
99 | show = render . ppInfoHash | 103 | show = render . ppInfoHash |
100 | 104 | ||
105 | instance Read InfoHash where | ||
106 | readsPrec _ = readP_to_S $ do | ||
107 | str <- replicateM 40 (satisfy isHexDigit) | ||
108 | return $ InfoHash $ decodeIH str | ||
109 | where | ||
110 | decodeIH = B.pack . map fromHex . pair | ||
111 | fromHex (a, b) = read $ '0' : 'x' : a : b : [] | ||
112 | |||
113 | pair (a : b : xs) = (a, b) : pair xs | ||
114 | pair _ = [] | ||
115 | |||
101 | instance Hashable InfoHash where | 116 | instance Hashable InfoHash where |
102 | hash = Hashable.hash . getInfoHash | 117 | hash = Hashable.hash . getInfoHash |
103 | 118 | ||