Skip to content

Commit

Permalink
Move multipleExitPoints helper to H.S.Util.Exit module
Browse files Browse the repository at this point in the history
  • Loading branch information
hvr committed Jan 25, 2018
1 parent 4362587 commit afd5072
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
1 change: 1 addition & 0 deletions hackage-security/hackage-security.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ library
Hackage.Security.Util.JSON
Hackage.Security.Util.Stack
Hackage.Security.Util.TypedEmbedded
Hackage.Security.Util.Exit
Prelude
-- We support ghc 7.4 (bundled with Cabal 1.14) and up
build-depends: base >= 4.5 && < 5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ module Hackage.Security.Client.Repository.Remote (
import Control.Concurrent
import Control.Exception
import Control.Monad.Cont
import Control.Monad.Except
import Data.List (nub, intercalate)
import Data.Typeable
import Network.URI hiding (uriPath, path)
Expand All @@ -50,6 +49,7 @@ import Hackage.Security.Util.IO
import Hackage.Security.Util.Path
import Hackage.Security.Util.Pretty
import Hackage.Security.Util.Some
import Hackage.Security.Util.Exit
import qualified Hackage.Security.Client.Repository.Cache as Cache

{-------------------------------------------------------------------------------
Expand Down Expand Up @@ -686,39 +686,3 @@ verifyRemoteFile remoteTemp trustedInfo = do
, temp
]

{-------------------------------------------------------------------------------
Auxiliary: multiple exit points
-------------------------------------------------------------------------------}

-- | Multiple exit points
--
-- We can simulate the imperative code
--
-- > if (cond1)
-- > return exp1;
-- > if (cond2)
-- > return exp2;
-- > if (cond3)
-- > return exp3;
-- > return exp4;
--
-- as
--
-- > multipleExitPoints $ do
-- > when (cond1) $
-- > exit exp1
-- > when (cond) $
-- > exit exp2
-- > when (cond)
-- > exit exp3
-- > return exp4
multipleExitPoints :: Monad m => ExceptT a m a -> m a
multipleExitPoints = liftM aux . runExceptT
where
aux :: Either a a -> a
aux (Left a) = a
aux (Right a) = a

-- | Function exit point (see 'multipleExitPoints')
exit :: Monad m => e -> ExceptT e m a
exit = throwError
40 changes: 40 additions & 0 deletions hackage-security/src/Hackage/Security/Util/Exit.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Hackage.Security.Util.Exit where

import Control.Monad.Except

{-------------------------------------------------------------------------------
Auxiliary: multiple exit points
-------------------------------------------------------------------------------}

-- | Multiple exit points
--
-- We can simulate the imperative code
--
-- > if (cond1)
-- > return exp1;
-- > if (cond2)
-- > return exp2;
-- > if (cond3)
-- > return exp3;
-- > return exp4;
--
-- as
--
-- > multipleExitPoints $ do
-- > when (cond1) $
-- > exit exp1
-- > when (cond) $
-- > exit exp2
-- > when (cond)
-- > exit exp3
-- > return exp4
multipleExitPoints :: Monad m => ExceptT a m a -> m a
multipleExitPoints = liftM aux . runExceptT
where
aux :: Either a a -> a
aux (Left a) = a
aux (Right a) = a

-- | Function exit point (see 'multipleExitPoints')
exit :: Monad m => e -> ExceptT e m a
exit = throwError

0 comments on commit afd5072

Please sign in to comment.