@@ -72,33 +72,35 @@ resolveFile x y =
7272 where fp = toFilePath x FP. </> y
7373 Just fp -> return fp
7474
75+ -- Internal helper to define resolveDirMaybe and resolveFileMaybe in one
76+ resolveCheckParse :: (Functor m , MonadIO m )
77+ => (FilePath -> IO Bool ) -- check if file/dir does exist
78+ -> (FilePath -> m a ) -- parse into absolute file/dir
79+ -> Path Abs Dir
80+ -> FilePath
81+ -> m (Maybe a )
82+ resolveCheckParse check parse x y = do
83+ let fp = toFilePath x FP. </> y
84+ exists <- liftIO $ check fp
85+ if exists
86+ then do
87+ canonic <- liftIO $ canonicalizePath fp
88+ fmap Just (parse canonic)
89+ else return Nothing
90+
7591-- | Appends a stringly-typed relative path to an absolute path, and then
7692-- canonicalizes it. If the path doesn't exist (and therefore cannot
7793-- be canonicalized, 'Nothing' is returned).
7894resolveDirMaybe :: (MonadIO m ,MonadThrow m )
7995 => Path Abs Dir -> FilePath -> m (Maybe (Path Abs Dir ))
80- resolveDirMaybe x y = do
81- let fp = toFilePath x FP. </> y
82- exists <- liftIO $ doesDirectoryExist fp
83- if exists
84- then do
85- dir <- liftIO $ canonicalizePath fp
86- liftM Just (parseAbsDir dir)
87- else return Nothing
96+ resolveDirMaybe = resolveCheckParse doesDirectoryExist parseAbsDir
8897
8998-- | Appends a stringly-typed relative path to an absolute path, and then
9099-- canonicalizes it. If the path doesn't exist (and therefore cannot
91100-- be canonicalized, 'Nothing' is returned).
92101resolveFileMaybe :: (MonadIO m ,MonadThrow m )
93102 => Path Abs Dir -> FilePath -> m (Maybe (Path Abs File ))
94- resolveFileMaybe x y = do
95- let fp = toFilePath x FP. </> y
96- exists <- liftIO $ doesFileExist fp
97- if exists
98- then do
99- file <- liftIO $ canonicalizePath fp
100- liftM Just (parseAbsFile file)
101- else return Nothing
103+ resolveFileMaybe = resolveCheckParse doesFileExist parseAbsFile
102104
103105-- | List objects in a directory, excluding "@.@" and "@..@". Entries are not sorted.
104106listDirectory :: (MonadIO m ,MonadThrow m ) => Path Abs Dir -> m ([Path Abs Dir ],[Path Abs File ])
0 commit comments