{-# LANGUAGE DataKinds #-}
module HIndent.Path.Find
( findFileUp
) where
import Control.Monad.Catch
import Control.Monad.IO.Class
import Data.List
import Path
import Path.IO hiding (findFiles)
findFileUp ::
(MonadIO m, MonadThrow m)
=> Path Abs Dir
-> (Path Abs File -> Bool)
-> Maybe (Path Abs Dir)
-> m (Maybe (Path Abs File))
findFileUp :: forall (m :: * -> *).
(MonadIO m, MonadThrow m) =>
Path Abs Dir
-> (Path Abs File -> Bool)
-> Maybe (Path Abs Dir)
-> m (Maybe (Path Abs File))
findFileUp = (([Path Abs Dir], [Path Abs File]) -> [Path Abs File])
-> Path Abs Dir
-> (Path Abs File -> Bool)
-> Maybe (Path Abs Dir)
-> m (Maybe (Path Abs File))
forall (m :: * -> *) t.
(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 ([Path Abs Dir], [Path Abs File]) -> [Path Abs File]
forall a b. (a, b) -> b
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 :: forall (m :: * -> *) t.
(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 ([Path Abs Dir], [Path Abs File]) -> [Path Abs t]
pathType Path Abs Dir
dir Path Abs t -> Bool
p Maybe (Path Abs Dir)
upperBound = do
([Path Abs Dir], [Path Abs File])
entries <- Path Abs Dir -> m ([Path Abs Dir], [Path Abs File])
forall (m :: * -> *) b.
MonadIO m =>
Path b Dir -> m ([Path Abs Dir], [Path Abs File])
listDir Path Abs Dir
dir
case (Path Abs t -> Bool) -> [Path Abs t] -> Maybe (Path Abs t)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find Path Abs t -> Bool
p (([Path Abs Dir], [Path Abs File]) -> [Path Abs t]
pathType ([Path Abs Dir], [Path Abs File])
entries) of
Just Path Abs t
path -> Maybe (Path Abs t) -> m (Maybe (Path Abs t))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Path Abs t -> Maybe (Path Abs t)
forall a. a -> Maybe a
Just Path Abs t
path)
Maybe (Path Abs t)
Nothing
| Path Abs Dir -> Maybe (Path Abs Dir)
forall a. a -> Maybe a
Just Path Abs Dir
dir Maybe (Path Abs Dir) -> Maybe (Path Abs Dir) -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe (Path Abs Dir)
upperBound -> Maybe (Path Abs t) -> m (Maybe (Path Abs t))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Path Abs t)
forall a. Maybe a
Nothing
| Path Abs Dir -> Path Abs Dir
forall b t. Path b t -> Path b Dir
parent Path Abs Dir
dir Path Abs Dir -> Path Abs Dir -> Bool
forall a. Eq a => a -> a -> Bool
== Path Abs Dir
dir -> Maybe (Path Abs t) -> m (Maybe (Path Abs t))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Path Abs t)
forall a. Maybe a
Nothing
| Bool
otherwise -> (([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))
forall (m :: * -> *) t.
(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 ([Path Abs Dir], [Path Abs File]) -> [Path Abs t]
pathType (Path Abs Dir -> Path Abs Dir
forall b t. Path b t -> Path b Dir
parent Path Abs Dir
dir) Path Abs t -> Bool
p Maybe (Path Abs Dir)
upperBound