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

View File

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

View File

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