Browse Source

Replace EitherT with ExceptT

tags/v0.2.0.0
Peter J. Jones 1 year ago
parent
commit
933a38d227
Signed by: Peter Jones <pjones@devalot.com> GPG Key ID: 9DAFAA8D01941E49
2 changed files with 13 additions and 9 deletions
  1. 1
    1
      playlists-http.cabal
  2. 12
    8
      src/Text/Playlist/HTTP/Full.hs

+ 1
- 1
playlists-http.cabal View File

@@ -52,12 +52,12 @@ library
52 52
   build-depends: attoparsec   >= 0.10  && < 1.0
53 53
                , base         >= 4.6   && < 5
54 54
                , bytestring   >= 0.10  && < 1.0
55
-               , either       >= 4.4   && < 4.5
56 55
                , exceptions   >= 0.8   && < 0.9
57 56
                , http-client  >= 0.4   && < 0.6
58 57
                , mtl          >= 2.2   && < 2.3
59 58
                , playlists    >= 0.5   && < 0.6
60 59
                , text         >= 0.11  && < 1.3
60
+               , transformers >= 0.4   && < 0.6
61 61
 
62 62
 --------------------------------------------------------------------------------
63 63
 executable example

+ 12
- 8
src/Text/Playlist/HTTP/Full.hs View File

@@ -28,7 +28,7 @@ module Text.Playlist.HTTP.Full
28 28
 -- Package imports:
29 29
 import Control.Monad.Catch
30 30
 import Control.Monad.RWS.Strict
31
-import Control.Monad.Trans.Either
31
+import Control.Monad.Trans.Except
32 32
 import qualified Data.Attoparsec.ByteString as Atto
33 33
 import Data.ByteString (ByteString)
34 34
 import qualified Data.ByteString as ByteString
@@ -79,7 +79,7 @@ data State = State
79 79
 -- | Internal type used for managing state, access to the environment,
80 80
 -- and access to IO.
81 81
 newtype Download m a =
82
-  Download { runDownload :: RWST Environment () State (EitherT Error m) a }
82
+  Download { runDownload :: RWST Environment () State (ExceptT Error m) a }
83 83
 
84 84
   deriving ( Functor
85 85
            , Applicative
@@ -92,7 +92,7 @@ newtype Download m a =
92 92
 --------------------------------------------------------------------------------
93 93
 -- | 'MonadThrow' instance for 'Download'.
94 94
 instance (Monad m) => MonadThrow (Download m) where
95
-  throwM = Download . lift . left . FailedOnException . show
95
+  throwM = Download . lift . throwE . FailedOnException . show
96 96
 
97 97
 --------------------------------------------------------------------------------
98 98
 -- | Internal helper function for getting a result out of a 'Download'
@@ -103,7 +103,7 @@ runS :: (Monad m)
103 103
      -> State
104 104
      -> m (Either Error (a, State))
105 105
 runS d e s = do
106
-  result <- runEitherT $ runRWST (runDownload d) e s
106
+  result <- runExceptT $ runRWST (runDownload d) e s
107 107
   case result of
108 108
     Left err         -> return (Left err)
109 109
     Right (x, s', _) -> return (Right (x, s'))
@@ -161,7 +161,11 @@ download env startURL = fmap fst <$> runS go env (State 0) where
161 161
   safeIO :: IO (Either Error a) -> Download m a
162 162
   safeIO action = io (catch action stop)
163 163
     where
164
-      io a = Download (lift (hoistEither =<< liftIO a))
164
+      io a = do x <- liftIO a
165
+                case x of
166
+                  Left e   -> Download . lift $ throwE e
167
+                  Right x' -> return x'
168
+
165 169
       stop = return . Left . ProtocolError
166 170
 
167 171
 --------------------------------------------------------------------------------
@@ -173,7 +177,7 @@ parseBody :: forall m. (MonadIO m)
173 177
           -> Response BodyReader
174 178
           -> Download m Playlist
175 179
 parseBody url response = do
176
-    parser <- Download (lift (hoistEither lookupParser))
180
+    parser <- Download (lift . ExceptT $ return lookupParser)
177 181
     bytes  <- readChunk
178 182
     dispatch (Atto.parse parser bytes)
179 183
 
@@ -190,7 +194,7 @@ parseBody url response = do
190 194
     ----------------------------------------------------------------------------
191 195
     -- | Dispatch an attoparsec response.
192 196
     dispatch :: Atto.Result Playlist -> Download m Playlist
193
-    dispatch (Atto.Fail _ _ err) = Download . lift $ left (FailedToParse err)
197
+    dispatch (Atto.Fail _ _ err) = Download . lift $ throwE (FailedToParse err)
194 198
     dispatch (Atto.Partial f)    = readChunk >>= dispatch . f
195 199
     dispatch (Atto.Done _ r)     = return r
196 200
 
@@ -202,7 +206,7 @@ parseBody url response = do
202 206
       count <- gets httpBytes
203 207
 
204 208
       case check count of
205
-        LimitReached -> Download . lift $ left ResponseTooLarge
209
+        LimitReached -> Download . lift $ throwE ResponseTooLarge
206 210
         Continue     -> do
207 211
           bytes <- liftIO $ brRead (responseBody response)
208 212
           modify' (\s -> s {httpBytes = ByteString.length bytes + count})

Loading…
Cancel
Save