From 1b4fdf35dd6c21d600130c42a1bcb5c56b25afa6 Mon Sep 17 00:00:00 2001 From: Sam T Date: Tue, 11 Jun 2013 06:00:57 +0400 Subject: ~ 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. --- exsamples/Main.hs | 2 +- src/Data/Torrent.hs | 6 ++++-- src/Network/BitTorrent.hs | 2 ++ 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 main :: IO () main = do [path] <- getArgs - Right torrent <- fromFile path + torrent <- fromFile path client <- newClient [] 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) import Control.Applicative import Control.Arrow +import Control.Exception +import Control.Monad import Data.BEncode as BE import Data.Char import Data.Foldable @@ -353,8 +355,8 @@ isMultiFile MultiFile {} = True isMultiFile _ = False -- | Read and decode a .torrent file. -fromFile :: FilePath -> IO (Result Torrent) -fromFile filepath = decoded <$> B.readFile filepath +fromFile :: FilePath -> IO Torrent +fromFile = B.readFile >=> either (throwIO . userError) return . decoded {----------------------------------------------------------------------- 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 import Network.BitTorrent.Exchange as BT import Network.BitTorrent.Tracker as BT +-- discover should hide tracker and DHT communication under the hood +-- thus we can obtain unified interface discover :: SwarmSession -> (TSession -> IO a) -> IO a 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 args <- getArgs let path = if length args == 0 then torrentFileName else head args - Right t <- fromFile path + t <- fromFile path BC.putStr "info hash: " print (ppInfoHash (tInfoHash t)) -- cgit v1.2.3