path-0.4.0: Path

Safe HaskellNone
LanguageHaskell98

Path

Contents

Description

A normalizing well-typed path type.

Synopsis

Types

data Path b t Source

Path of some base and type.

Internally is a string. The string can be of two formats only:

  1. File format: file.txt, foo/bar.txt, /foo/bar.txt
  2. Directory format: foo/, /foo/bar/

All directories end in a trailing separator. There are no duplicate path separators //, no .., no ./, no ~/, etc.

Instances

Eq (Path b t)

String equality.

The following property holds:

show x == show y ≡ x == y
Ord (Path b t)

String ordering.

The following property holds:

show x `compare` show y ≡ x `compare` y
Show (Path b t)

Same as toFilePath.

The following property holds:

x == y ≡ show x == show y
Typeable (* -> * -> *) Path 

data Abs Source

An absolute path.

Instances

data Rel Source

A relative path; one without a root.

Instances

data File Source

A file path.

Instances

data Dir Source

A directory path.

Instances

Parsing

parseAbsDir :: MonadThrow m => FilePath -> m (Path Abs Dir) Source

Get a location for an absolute directory. Produces a normalized path which always ends in a path separator.

Throws: PathParseException

parseRelDir :: MonadThrow m => FilePath -> m (Path Rel Dir) Source

Get a location for a relative directory. Produces a normalized path which always ends in a path separator.

Throws: PathParseException

parseAbsFile :: MonadThrow m => FilePath -> m (Path Abs File) Source

Get a location for an absolute file.

Throws: PathParseException

parseRelFile :: MonadThrow m => FilePath -> m (Path Rel File) Source

Get a location for a relative file.

Throws: PathParseException

Constructors

mkAbsDir :: FilePath -> Q Exp Source

Make a 'Path Abs Dir'.

Remember: due to the nature of absolute paths this (e.g. /home/foo) may compile on your platform, but it may not compile on another platform (Windows).

mkRelDir :: FilePath -> Q Exp Source

Make a 'Path Rel Dir'.

mkAbsFile :: FilePath -> Q Exp Source

Make a 'Path Abs File'.

Remember: due to the nature of absolute paths this (e.g. /home/foo) may compile on your platform, but it may not compile on another platform (Windows).

mkRelFile :: FilePath -> Q Exp Source

Make a 'Path Rel File'.

Operations

(</>) :: Path b Dir -> Path Rel t -> Path b t Source

Append two paths.

The following cases are valid and the equalities hold:

$(mkAbsDir x) </> $(mkRelDir y) = $(mkAbsDir (x ++ "/" ++ y))
$(mkAbsDir x) </> $(mkRelFile y) = $(mkAbsFile (x ++ "/" ++ y))
$(mkRelDir x) </> $(mkRelDir y) = $(mkRelDir (x ++ "/" ++ y))
$(mkRelDir x) </> $(mkRelFile y) = $(mkRelFile (x ++ "/" ++ y))

The following are proven not possible to express:

$(mkAbsFile …) </> x
$(mkRelFile …) </> x
x </> $(mkAbsFile …)
x </> $(mkAbsDir …)

stripDir :: MonadThrow m => Path b Dir -> Path b t -> m (Path Rel t) Source

Strip directory from path, making it relative to that directory. Returns Nothing if directory is not a parent of the path.

The following properties hold:

stripDir parent (parent </> child) = child

Cases which are proven not possible:

stripDir (a :: Path Abs …) (b :: Path Rel …)
stripDir (a :: Path Rel …) (b :: Path Abs …)

In other words the bases must match.

Throws: Couldn'tStripPrefixDir

isParentOf :: Path b Dir -> Path b t -> Bool Source

Is p a parent of the given location? Implemented in terms of stripDir. The bases must match.

parent :: Path Abs t -> Path Abs Dir Source

Take the absolute parent directory from the absolute path.

The following properties hold:

parent (parent </> child) == parent

On the root, getting the parent is idempotent:

parent (parent "/") = "/"

filename :: Path b File -> Path Rel File Source

Extract the file part of a path.

The following properties hold:

filename (parent </> filename a) == a

dirname :: Path b Dir -> Path Rel Dir Source

Extract the last directory name of a path.

The following properties hold:

dirname (parent </> dirname a) == a

Conversion

toFilePath :: Path b t -> FilePath Source

Convert to a FilePath type.