summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-11-28 06:42:10 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-11-28 06:42:10 +0400
commit32027c2d4392fb6964bafe3aaf6d427e5d827d21 (patch)
tree3c58da15d8c7a4b84c9dbe19ac22eb5e6ffed4a8 /src
parentfc4ed85a313f93b4fb0c46b500ee12c38e94df68 (diff)
Hide InfoHash constructor
Diffstat (limited to 'src')
-rw-r--r--src/Data/Torrent.hs2
-rw-r--r--src/Data/Torrent/InfoHash.hs15
-rw-r--r--src/Data/Torrent/Layout.hs4
3 files changed, 9 insertions, 12 deletions
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs
index 59f11814..8411dc97 100644
--- a/src/Data/Torrent.hs
+++ b/src/Data/Torrent.hs
@@ -142,7 +142,7 @@ infoDictionary :: LayoutInfo -> PieceInfo -> Bool -> InfoDict
142infoDictionary li pinfo private = InfoDict ih li pinfo private 142infoDictionary li pinfo private = InfoDict ih li pinfo private
143 where 143 where
144 ih = IH.hashlazy $ encode $ InfoDict fake_ih li pinfo private 144 ih = IH.hashlazy $ encode $ InfoDict fake_ih li pinfo private
145 fake_ih = InfoHash "" 145 fake_ih = "0123456789012345678901234567890123456789"
146 146
147getPrivate :: Get Bool 147getPrivate :: Get Bool
148getPrivate = (Just True ==) <$>? "private" 148getPrivate = (Just True ==) <$>? "private"
diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs
index a4d6a02e..fd57700a 100644
--- a/src/Data/Torrent/InfoHash.hs
+++ b/src/Data/Torrent/InfoHash.hs
@@ -10,8 +10,7 @@
10{-# LANGUAGE FlexibleInstances #-} 10{-# LANGUAGE FlexibleInstances #-}
11{-# LANGUAGE GeneralizedNewtypeDeriving #-} 11{-# LANGUAGE GeneralizedNewtypeDeriving #-}
12module Data.Torrent.InfoHash 12module Data.Torrent.InfoHash
13 ( -- * Info hash 13 ( InfoHash
14 InfoHash(..)
15 14
16 -- * Parsing 15 -- * Parsing
17 , byteStringToInfoHash 16 , byteStringToInfoHash
@@ -42,6 +41,7 @@ import qualified Data.ByteString.Lazy.Builder as B
42import qualified Data.ByteString.Lazy.Builder.ASCII as B 41import qualified Data.ByteString.Lazy.Builder.ASCII as B
43import Data.Char 42import Data.Char
44import Data.List as L 43import Data.List as L
44import Data.Maybe
45import Data.Hashable as Hashable 45import Data.Hashable as Hashable
46import Data.URLEncoded as URL 46import Data.URLEncoded as URL
47import Data.Serialize 47import Data.Serialize
@@ -85,14 +85,11 @@ instance Read InfoHash where
85 pair (a : b : xs) = (a, b) : pair xs 85 pair (a : b : xs) = (a, b) : pair xs
86 pair _ = [] 86 pair _ = []
87 87
88-- | for base16 (hex) encoded strings 88-- | for base16/base32 encoded strings
89instance IsString InfoHash where 89instance IsString InfoHash where
90 fromString str 90 fromString str = fromMaybe err $ textToInfoHash $ T.pack str
91 | L.length str == 40 91 where
92 , (ihStr, inv) <- Base16.decode $ BC.pack str 92 err = error $ "fromString: invalid infohash string" ++ str
93 = if BS.length inv == 0 then InfoHash ihStr
94 else error "fromString: invalid infohash string"
95 | otherwise = error "fromString: invalid infohash string length"
96 93
97instance Hashable InfoHash where 94instance Hashable InfoHash where
98 hash = Hashable.hash . getInfoHash 95 hash = Hashable.hash . getInfoHash
diff --git a/src/Data/Torrent/Layout.hs b/src/Data/Torrent/Layout.hs
index 70908c4e..8f35772d 100644
--- a/src/Data/Torrent/Layout.hs
+++ b/src/Data/Torrent/Layout.hs
@@ -68,6 +68,7 @@ import Data.Aeson.Types (FromJSON, ToJSON)
68import Data.BEncode 68import Data.BEncode
69import Data.BEncode.Types 69import Data.BEncode.Types
70import Data.ByteString as BS 70import Data.ByteString as BS
71import Data.ByteString.Base16 as Base16
71import Data.ByteString.Char8 as BC 72import Data.ByteString.Char8 as BC
72import Data.Char as Char 73import Data.Char as Char
73import Data.Foldable as F 74import Data.Foldable as F
@@ -81,7 +82,6 @@ import System.FilePath
81import System.Posix.Types 82import System.Posix.Types
82 83
83import Data.Torrent.Block 84import Data.Torrent.Block
84import Data.Torrent.InfoHash
85 85
86 86
87{----------------------------------------------------------------------- 87{-----------------------------------------------------------------------
@@ -180,7 +180,7 @@ instance Pretty (FileInfo BS.ByteString) where
180 $$ "Size: " <> text (show fiLength) 180 $$ "Size: " <> text (show fiLength)
181 $$ maybe PP.empty ppMD5 fiMD5Sum 181 $$ maybe PP.empty ppMD5 fiMD5Sum
182 where 182 where
183 ppMD5 md5 = "MD5 : " <> text (show (InfoHash md5)) 183 ppMD5 md5 = "MD5 : " <> text (show (Base16.encode md5))
184 184
185-- | Join file path. 185-- | Join file path.
186joinFilePath :: FileInfo [ByteString] -> FileInfo ByteString 186joinFilePath :: FileInfo [ByteString] -> FileInfo ByteString