{-# LANGUAGE FlexibleContexts #-}
module Foundation.VFS.Path
(
Path(..)
, parent
, filename
, prefix
, suffix
) where
import Basement.Compat.Base
class Path path where
type PathEnt path
type PathPrefix path
type PathSuffix path
(</>) :: path -> PathEnt path -> path
splitPath :: path -> ( PathPrefix path
, [PathEnt path]
, PathSuffix path
)
buildPath :: ( PathPrefix path
, [PathEnt path]
, PathSuffix path
)
-> path
parent :: Path path => path -> path
parent :: forall path. Path path => path -> path
parent path
path = (PathPrefix path, [PathEnt path], PathSuffix path) -> path
forall path.
Path path =>
(PathPrefix path, [PathEnt path], PathSuffix path) -> path
buildPath (PathPrefix path
p, [PathEnt path] -> [PathEnt path]
forall a. [a] -> [a]
init [PathEnt path]
ps, PathSuffix path
s)
where
(PathPrefix path
p, [PathEnt path]
ps, PathSuffix path
s) = path -> (PathPrefix path, [PathEnt path], PathSuffix path)
forall path.
Path path =>
path -> (PathPrefix path, [PathEnt path], PathSuffix path)
splitPath path
path
filename :: (Path path, Monoid (PathEnt path)) => path -> PathEnt path
filename :: forall path.
(Path path, Monoid (PathEnt path)) =>
path -> PathEnt path
filename path
path = case [PathEnt path]
ps of
[] -> PathEnt path
forall a. Monoid a => a
mempty
[PathEnt path]
_ -> [PathEnt path] -> PathEnt path
forall a. [a] -> a
last [PathEnt path]
ps
where
(PathPrefix path
_, [PathEnt path]
ps , PathSuffix path
_) = path -> (PathPrefix path, [PathEnt path], PathSuffix path)
forall path.
Path path =>
path -> (PathPrefix path, [PathEnt path], PathSuffix path)
splitPath path
path
init :: [a] -> [a]
init :: forall a. [a] -> [a]
init [] = []
init [a
_] = []
init (a
x:[a]
xs) = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> [a]
forall a. [a] -> [a]
init [a]
xs
last :: [a] -> a
last :: forall a. [a] -> a
last [] = a
forall a. HasCallStack => a
undefined
last [a
x] = a
x
last (a
_:[a]
xs) = [a] -> a
forall a. [a] -> a
last [a]
xs
prefix :: Path path => path -> PathPrefix path
prefix :: forall path. Path path => path -> PathPrefix path
prefix path
p = PathPrefix path
pre
where
(PathPrefix path
pre, [PathEnt path]
_, PathSuffix path
_) = path -> (PathPrefix path, [PathEnt path], PathSuffix path)
forall path.
Path path =>
path -> (PathPrefix path, [PathEnt path], PathSuffix path)
splitPath path
p
suffix :: Path path => path -> PathSuffix path
suffix :: forall path. Path path => path -> PathSuffix path
suffix path
p = PathSuffix path
suf
where
(PathPrefix path
_, [PathEnt path]
_, PathSuffix path
suf) = path -> (PathPrefix path, [PathEnt path], PathSuffix path)
forall path.
Path path =>
path -> (PathPrefix path, [PathEnt path], PathSuffix path)
splitPath path
p