LIB: use whenM/unlessM

This commit is contained in:
Julian Ospald 2015-12-18 15:37:14 +01:00
parent ffe8037160
commit 0f1c8dafe8
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
3 changed files with 14 additions and 16 deletions

View File

@ -16,6 +16,7 @@ import Data.List
isPrefixOf
)
import Data.Typeable
import IO.Utils
import System.Directory
(
doesDirectoryExist
@ -46,17 +47,13 @@ instance Exception FmIOException
-- Throws an exception if the filepath is not absolute
-- or the file does not exist.
fileSanityThrow :: FilePath -> IO ()
fileSanityThrow fp = do
throwNotAbsolute fp
throwFileDoesNotExist fp
fileSanityThrow fp = throwNotAbsolute fp >> throwFileDoesNotExist fp
-- Throws an exception if the filepath is not absolute
-- or the dir does not exist.
dirSanityThrow :: FilePath -> IO ()
dirSanityThrow fp = do
throwNotAbsolute fp
throwDirDoesNotExist fp
dirSanityThrow fp = throwNotAbsolute fp >> throwDirDoesNotExist fp
throwNotAbsolute :: FilePath -> IO ()
@ -64,21 +61,18 @@ throwNotAbsolute fp = unless (isAbsolute fp) (throw $ PathNotAbsolute fp)
throwDirDoesExist :: FilePath -> IO ()
throwDirDoesExist fp = do
exists <- doesDirectoryExist fp
when exists (throw $ DirDoesExist fp)
throwDirDoesExist fp =
whenM (doesDirectoryExist fp) (throw $ DirDoesExist fp)
throwDirDoesNotExist :: FilePath -> IO ()
throwDirDoesNotExist fp = do
exists <- doesDirectoryExist fp
unless exists (throw $ FileDoesNotExist fp)
throwDirDoesNotExist fp =
unlessM (doesDirectoryExist fp) (throw $ FileDoesNotExist fp)
throwFileDoesNotExist :: FilePath -> IO ()
throwFileDoesNotExist fp = do
exists <- doesFileExist fp
unless exists (throw $ FileDoesNotExist fp)
throwFileDoesNotExist fp =
unlessM (doesFileExist fp) (throw $ FileDoesNotExist fp)
throwSameFile :: FilePath -- ^ should be canonicalized

View File

@ -11,7 +11,6 @@ import Control.Monad
(
unless
, void
, when
)
import Data.DirTree
import Data.DirTree.Zipper

View File

@ -16,6 +16,7 @@ import Control.Concurrent.STM.TVar
import Control.Monad
(
when
, unless
)
@ -29,3 +30,7 @@ modifyTVarIO tvar f = atomically $ modifyTVar tvar f
whenM :: Monad m => m Bool -> m () -> m ()
whenM mb a = mb >>= (`when` a)
unlessM :: Monad m => m Bool -> m () -> m ()
unlessM mb a = mb >>= (`unless` a)