diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-06-11 06:00:57 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-06-11 06:00:57 +0400 |
commit | 1b4fdf35dd6c21d600130c42a1bcb5c56b25afa6 (patch) | |
tree | 136bb1467f5d3b5929620ddb0ab2adffc656504d | |
parent | a6490c275b6c688df1f654bff92f96aaeb998805 (diff) |
~ Use exception in fromFile.
Again, the problem is that one function returns errors in two different
ways: Either and IO exceptions. It's better to just throw exception in
either case.
-rw-r--r-- | exsamples/Main.hs | 2 | ||||
-rw-r--r-- | src/Data/Torrent.hs | 6 | ||||
-rw-r--r-- | src/Network/BitTorrent.hs | 2 | ||||
-rw-r--r-- | tests/InfoHash.hs | 2 |
4 files changed, 8 insertions, 4 deletions
diff --git a/exsamples/Main.hs b/exsamples/Main.hs index 3a347ab6..b0224886 100644 --- a/exsamples/Main.hs +++ b/exsamples/Main.hs | |||
@@ -8,7 +8,7 @@ import System.Environment | |||
8 | main :: IO () | 8 | main :: IO () |
9 | main = do | 9 | main = do |
10 | [path] <- getArgs | 10 | [path] <- getArgs |
11 | Right torrent <- fromFile path | 11 | torrent <- fromFile path |
12 | 12 | ||
13 | client <- newClient [] | 13 | client <- newClient [] |
14 | swarm <- newLeacher client torrent | 14 | swarm <- newLeacher client torrent |
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index 3175e151..068e9cb6 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs | |||
@@ -55,6 +55,8 @@ import Prelude hiding (sum) | |||
55 | 55 | ||
56 | import Control.Applicative | 56 | import Control.Applicative |
57 | import Control.Arrow | 57 | import Control.Arrow |
58 | import Control.Exception | ||
59 | import Control.Monad | ||
58 | import Data.BEncode as BE | 60 | import Data.BEncode as BE |
59 | import Data.Char | 61 | import Data.Char |
60 | import Data.Foldable | 62 | import Data.Foldable |
@@ -353,8 +355,8 @@ isMultiFile MultiFile {} = True | |||
353 | isMultiFile _ = False | 355 | isMultiFile _ = False |
354 | 356 | ||
355 | -- | Read and decode a .torrent file. | 357 | -- | Read and decode a .torrent file. |
356 | fromFile :: FilePath -> IO (Result Torrent) | 358 | fromFile :: FilePath -> IO Torrent |
357 | fromFile filepath = decoded <$> B.readFile filepath | 359 | fromFile = B.readFile >=> either (throwIO . userError) return . decoded |
358 | 360 | ||
359 | {----------------------------------------------------------------------- | 361 | {----------------------------------------------------------------------- |
360 | Info hash | 362 | Info hash |
diff --git a/src/Network/BitTorrent.hs b/src/Network/BitTorrent.hs index 61185d08..c37129cb 100644 --- a/src/Network/BitTorrent.hs +++ b/src/Network/BitTorrent.hs | |||
@@ -28,6 +28,8 @@ import Network.BitTorrent.Peer as BT | |||
28 | import Network.BitTorrent.Exchange as BT | 28 | import Network.BitTorrent.Exchange as BT |
29 | import Network.BitTorrent.Tracker as BT | 29 | import Network.BitTorrent.Tracker as BT |
30 | 30 | ||
31 | -- discover should hide tracker and DHT communication under the hood | ||
32 | -- thus we can obtain unified interface | ||
31 | 33 | ||
32 | discover :: SwarmSession -> (TSession -> IO a) -> IO a | 34 | discover :: SwarmSession -> (TSession -> IO a) -> IO a |
33 | discover SwarmSession {..} action = do | 35 | discover SwarmSession {..} action = do |
diff --git a/tests/InfoHash.hs b/tests/InfoHash.hs index dbbcbf53..3f5a1432 100644 --- a/tests/InfoHash.hs +++ b/tests/InfoHash.hs | |||
@@ -19,7 +19,7 @@ main = do | |||
19 | args <- getArgs | 19 | args <- getArgs |
20 | let path = if length args == 0 then torrentFileName else head args | 20 | let path = if length args == 0 then torrentFileName else head args |
21 | 21 | ||
22 | Right t <- fromFile path | 22 | t <- fromFile path |
23 | 23 | ||
24 | BC.putStr "info hash: " | 24 | BC.putStr "info hash: " |
25 | print (ppInfoHash (tInfoHash t)) | 25 | print (ppInfoHash (tInfoHash t)) |