diff options
-rw-r--r-- | bittorrent.cabal | 1 | ||||
-rw-r--r-- | src/Data/Torrent.hs | 4 | ||||
-rw-r--r-- | src/Data/Torrent/InfoHash.hs | 10 | ||||
-rw-r--r-- | src/Network/BitTorrent/Tracker/Message.hs | 3 | ||||
-rw-r--r-- | tests/Data/Torrent/InfoHashSpec.hs | 3 |
5 files changed, 10 insertions, 11 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal index f1a04329..d653c971 100644 --- a/bittorrent.cabal +++ b/bittorrent.cabal | |||
@@ -160,6 +160,7 @@ test-suite spec | |||
160 | , directory | 160 | , directory |
161 | , filepath | 161 | , filepath |
162 | , time | 162 | , time |
163 | , convertible | ||
163 | 164 | ||
164 | , aeson | 165 | , aeson |
165 | , cereal | 166 | , cereal |
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index d6227b6f..b3ac7586 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs | |||
@@ -78,10 +78,10 @@ import Data.ByteString as BS | |||
78 | import qualified Data.ByteString.Char8 as BC (pack, unpack) | 78 | import qualified Data.ByteString.Char8 as BC (pack, unpack) |
79 | import qualified Data.ByteString.Lazy as BL | 79 | import qualified Data.ByteString.Lazy as BL |
80 | import Data.Char as Char | 80 | import Data.Char as Char |
81 | import Data.Convertible | ||
81 | import Data.Default | 82 | import Data.Default |
82 | import Data.Hashable as Hashable | 83 | import Data.Hashable as Hashable |
83 | import qualified Data.List as L | 84 | import qualified Data.List as L |
84 | import Data.Maybe | ||
85 | import Data.Text as T | 85 | import Data.Text as T |
86 | import Data.Time | 86 | import Data.Time |
87 | import Data.Time.Clock.POSIX | 87 | import Data.Time.Clock.POSIX |
@@ -155,7 +155,7 @@ putPrivate True = \ cont -> "private" .=! True .: cont | |||
155 | 155 | ||
156 | -- | Hash lazy bytestring using SHA1 algorithm. | 156 | -- | Hash lazy bytestring using SHA1 algorithm. |
157 | hashLazyIH :: BL.ByteString -> InfoHash | 157 | hashLazyIH :: BL.ByteString -> InfoHash |
158 | hashLazyIH = fromMaybe (error msg) . byteStringToInfoHash . C.hashlazy | 158 | hashLazyIH = either (const (error msg)) id . safeConvert . C.hashlazy |
159 | where | 159 | where |
160 | msg = "Infohash.hash: impossible: SHA1 is always 20 bytes long" | 160 | msg = "Infohash.hash: impossible: SHA1 is always 20 bytes long" |
161 | 161 | ||
diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs index 7442c61c..08cee3c9 100644 --- a/src/Data/Torrent/InfoHash.hs +++ b/src/Data/Torrent/InfoHash.hs | |||
@@ -15,7 +15,6 @@ module Data.Torrent.InfoHash | |||
15 | ( InfoHash | 15 | ( InfoHash |
16 | 16 | ||
17 | -- * Parsing | 17 | -- * Parsing |
18 | , byteStringToInfoHash | ||
19 | , textToInfoHash | 18 | , textToInfoHash |
20 | 19 | ||
21 | -- * Rendering | 20 | -- * Rendering |
@@ -163,12 +162,9 @@ instance FromJSON InfoHash where | |||
163 | ignoreErrorMsg :: Either a b -> Maybe b | 162 | ignoreErrorMsg :: Either a b -> Maybe b |
164 | ignoreErrorMsg = either (const Nothing) Just | 163 | ignoreErrorMsg = either (const Nothing) Just |
165 | 164 | ||
166 | -- | TODO remove from API | 165 | -- | Tries both base16 and base32 while decoding info hash. |
167 | byteStringToInfoHash :: BS.ByteString -> Maybe InfoHash | 166 | -- |
168 | byteStringToInfoHash = ignoreErrorMsg . safeConvert | 167 | -- Use 'safeConvert' for detailed error messages. |
169 | |||
170 | -- | Tries both base16 and base32 while decoding info hash. To Use | ||
171 | -- 'safeConvert' to find out | ||
172 | -- | 168 | -- |
173 | textToInfoHash :: Text -> Maybe InfoHash | 169 | textToInfoHash :: Text -> Maybe InfoHash |
174 | textToInfoHash = ignoreErrorMsg . safeConvert | 170 | textToInfoHash = ignoreErrorMsg . safeConvert |
diff --git a/src/Network/BitTorrent/Tracker/Message.hs b/src/Network/BitTorrent/Tracker/Message.hs index e5d8b25a..3437dcf3 100644 --- a/src/Network/BitTorrent/Tracker/Message.hs +++ b/src/Network/BitTorrent/Tracker/Message.hs | |||
@@ -55,6 +55,7 @@ import Data.BEncode.BDict as BE | |||
55 | import Data.ByteString as BS | 55 | import Data.ByteString as BS |
56 | import Data.ByteString.Char8 as BC | 56 | import Data.ByteString.Char8 as BC |
57 | import Data.Char as Char | 57 | import Data.Char as Char |
58 | import Data.Convertible | ||
58 | import Data.List as L | 59 | import Data.List as L |
59 | import Data.Map as M | 60 | import Data.Map as M |
60 | import Data.Maybe | 61 | import Data.Maybe |
@@ -265,7 +266,7 @@ class FromParam a where | |||
265 | fromParam :: BS.ByteString -> Maybe a | 266 | fromParam :: BS.ByteString -> Maybe a |
266 | 267 | ||
267 | instance FromParam InfoHash where | 268 | instance FromParam InfoHash where |
268 | fromParam = byteStringToInfoHash | 269 | fromParam = either (const Nothing) pure . safeConvert |
269 | 270 | ||
270 | instance FromParam PeerId where | 271 | instance FromParam PeerId where |
271 | fromParam = byteStringToPeerId | 272 | fromParam = byteStringToPeerId |
diff --git a/tests/Data/Torrent/InfoHashSpec.hs b/tests/Data/Torrent/InfoHashSpec.hs index afa017ef..3cc110c1 100644 --- a/tests/Data/Torrent/InfoHashSpec.hs +++ b/tests/Data/Torrent/InfoHashSpec.hs | |||
@@ -3,6 +3,7 @@ module Data.Torrent.InfoHashSpec (spec) where | |||
3 | 3 | ||
4 | import Control.Applicative | 4 | import Control.Applicative |
5 | import Data.ByteString as BS | 5 | import Data.ByteString as BS |
6 | import Data.Convertible | ||
6 | import Data.Maybe | 7 | import Data.Maybe |
7 | import System.FilePath | 8 | import System.FilePath |
8 | import Test.Hspec | 9 | import Test.Hspec |
@@ -16,7 +17,7 @@ import Data.Torrent.InfoHash as IH | |||
16 | instance Arbitrary InfoHash where | 17 | instance Arbitrary InfoHash where |
17 | arbitrary = do | 18 | arbitrary = do |
18 | bs <- BS.pack <$> vectorOf 20 arbitrary | 19 | bs <- BS.pack <$> vectorOf 20 arbitrary |
19 | pure $ fromMaybe (error "arbitrary infohash") $ byteStringToInfoHash bs | 20 | pure $ either (const (error "arbitrary infohash")) id $ safeConvert bs |
20 | 21 | ||
21 | type TestPair = (FilePath, String) | 22 | type TestPair = (FilePath, String) |
22 | 23 | ||