From 0f1c8dafe837d614922963af9a14504fdbea93dd Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Fri, 18 Dec 2015 15:37:14 +0100 Subject: [PATCH] LIB: use whenM/unlessM --- src/IO/Error.hs | 24 +++++++++--------------- src/IO/File.hs | 1 - src/IO/Utils.hs | 5 +++++ 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/IO/Error.hs b/src/IO/Error.hs index 1e421bd..9b5f71a 100644 --- a/src/IO/Error.hs +++ b/src/IO/Error.hs @@ -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 diff --git a/src/IO/File.hs b/src/IO/File.hs index f7edc9c..d9ea510 100644 --- a/src/IO/File.hs +++ b/src/IO/File.hs @@ -11,7 +11,6 @@ import Control.Monad ( unless , void - , when ) import Data.DirTree import Data.DirTree.Zipper diff --git a/src/IO/Utils.hs b/src/IO/Utils.hs index 5da8b8b..52e638d 100644 --- a/src/IO/Utils.hs +++ b/src/IO/Utils.hs @@ -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)