{-# LANGUAGE DataKinds #-}
module Path.Find
( findFileUp
) where
import Control.Monad.Catch
import Control.Monad.IO.Class
import Data.List
import Path
import Path.IO
findFileUp ::
(MonadIO m, MonadThrow m)
=> Path Abs Dir
-> (Path Abs File -> Bool)
-> Maybe (Path Abs Dir)
-> m (Maybe (Path Abs File))
findFileUp = findPathUp snd
findPathUp ::
(MonadIO m, MonadThrow m)
=> (([Path Abs Dir], [Path Abs File]) -> [Path Abs t])
-> Path Abs Dir
-> (Path Abs t -> Bool)
-> Maybe (Path Abs Dir)
-> m (Maybe (Path Abs t))
findPathUp pathType dir p upperBound = do
entries <- listDir dir
case find p (pathType entries) of
Just path -> return (Just path)
Nothing
| Just dir == upperBound -> return Nothing
| parent dir == dir -> return Nothing
| otherwise -> findPathUp pathType (parent dir) p upperBound