Path
Edit on GitHubUtilities for working with system paths.
This module treats paths purely as a data representation and does not provide functionality for interacting with the file system.
This module explicitly encodes whether a path is absolute or relative, and
whether it refers to a file or a directory, as part of the Path
type.
Paths in this module abide by a special POSIX-like representation/grammar rather than one defined by a specific operating system. The rules are as follows:
- Path separators are denoted by
/
for POSIX-like paths - Absolute paths may be rooted either at the POSIX-like root
/
or at Windows-like drive roots likeC:/
- Paths referencing files must not include trailing forward slashes, but paths referencing directories may
- The path segment
.
indicates the relative “current” directory of a path, and..
indicates the parent directory of a path
Added in 0.5.5
No other changes yet.
Types
Type declarations included in the Path module.
Path.AbsoluteRoot
Represents an absolute path’s anchor point.
Path.Relative
Represents a relative path.
Path.Absolute
Represents an absolute path.
Path.File
Represents a path referencing a file.
Path.Directory
Represents a path referencing a directory.
Path.TypedPath
Represents a path typed on (Absolute
or Relative
) and (File
or
Directory
)
Path.Path
1 | enum Path { |
Represents a system path.
Path.Platform
Represents a platform-specific path encoding scheme.
Path.PathOperationError
Represents an error that can occur when finding a property of a path.
Path.AppendError
Represents an error that can occur when appending paths.
Path.AncestryStatus
Represents the status of an ancestry check between two paths.
Path.IncompatibilityError
Represents an error that can occur when the types of paths are incompatible for an operation.
Path.RelativizationError
Represents possible errors for the relativeTo
operation.
Values
Functions and constants included in the Path module.
Path.fromString
Added in 0.5.5
version | changes |
---|---|
0.6.0 | Merged with `fromPlatformString`; modified signature to accept platform |
Parses a path string into a Path
using the path separators appropriate to
the given platform (/
for Posix
and either /
or \
for Windows
).
Paths will be parsed as file paths rather than directory paths if there is
ambiguity.
Parameters:
param | type | description |
---|---|---|
pathStr |
String |
The string to parse as a path |
?platform |
Platform |
The platform whose path separators should be used for parsing |
Returns:
type | description |
---|---|
Path |
The path wrapped with details encoded within the type |
Examples:
Path.toString
Added in 0.5.5
version | changes |
---|---|
0.6.0 | Merged with `toPlatformString`; modified signature to accept platform |
Converts the given Path
into a string, using the canonical path separator
appropriate to the given platform (/
for Posix
and \
for Windows
).
A trailing slash is added to directory paths.
Parameters:
param | type | description |
---|---|---|
path |
Path |
The path to convert to a string |
?platform |
Platform |
The Platform to use to represent the path as a string |
Returns:
type | description |
---|---|
String |
A string representing the given path |
Examples:
Path.isDirectory
Added in 0.5.5
No other changes yet.
Determines whether the path is a directory path.
Parameters:
param | type | description |
---|---|---|
path |
Path |
The path to inspect |
Returns:
type | description |
---|---|
Bool |
true if the path is a directory path or false otherwise |
Examples:
Path.isAbsolute
Determines whether the path is an absolute path.
Parameters:
param | type | description |
---|---|---|
path |
Path |
The path to inspect |
Returns:
type | description |
---|---|
Bool |
true if the path is absolute or false otherwise |
Examples:
Path.append
Added in 0.5.5
No other changes yet.
Creates a new path by appending a relative path segment to a directory path.
Parameters:
param | type | description |
---|---|---|
path |
Path |
The base path |
toAppend |
Path |
The relative path to append |
Returns:
type | description |
---|---|
Result<Path, AppendError> |
Ok(path) combining the base and appended paths or Err(err) if the paths are incompatible |
Examples:
1 | append(fromString("./dir/"), fromString("/dir2")) == Err(AppendAbsolute) // cannot append an absolute path |
Path.relativeTo
Added in 0.5.5
No other changes yet.
Attempts to construct a new relative path which will lead to the destination path from the source path.
If the source and destination are incompatible in their bases, the result
will be Err(IncompatibilityError)
.
If the route to the destination cannot be concretely determined from the
source, the result will be Err(ImpossibleRelativization)
.
Parameters:
param | type | description |
---|---|---|
source |
Path |
The source path |
dest |
Path |
The destination path to resolve |
Returns:
type | description |
---|---|
Result<Path, RelativizationError> |
Ok(path) containing the relative path if successfully resolved or Err(err) otherwise |
Examples:
Path.ancestry
Added in 0.5.5
No other changes yet.
Determines the relative ancestry betwen two paths.
Parameters:
param | type | description |
---|---|---|
base |
Path |
The first path to consider |
path |
Path |
The second path to consider |
Returns:
type | description |
---|---|
Result<AncestryStatus, IncompatibilityError> |
Ok(ancestryStatus) with the relative ancestry between the paths if they are compatible or Err(err) if they are incompatible |
Examples:
Path.parent
Added in 0.5.5
No other changes yet.
Retrieves the path corresponding to the parent directory of the given path.
Parameters:
param | type | description |
---|---|---|
path |
Path |
The path to inspect |
Returns:
type | description |
---|---|
Path |
A path corresponding to the parent directory of the given path |
Examples:
Path.basename
Added in 0.5.5
No other changes yet.
Retrieves the basename (named final segment) of a path.
Parameters:
param | type | description |
---|---|---|
path |
Path |
The path to inspect |
Returns:
type | description |
---|---|
Option<String> |
Some(path) containing the basename of the path or None if the path does not have one |
Examples:
Path.stem
Added in 0.5.5
No other changes yet.
Retrieves the basename of a file path without the extension.
Parameters:
param | type | description |
---|---|---|
path |
Path |
The path to inspect |
Returns:
type | description |
---|---|
Result<String, PathOperationError> |
Ok(path) containing the stem of the file path or Err(err) if the path is a directory path |
Examples:
Path.extension
Added in 0.5.5
No other changes yet.
Retrieves the extension on the basename of a file path.
Parameters:
param | type | description |
---|---|---|
path |
Path |
The path to inspect |
Returns:
type | description |
---|---|
Result<String, PathOperationError> |
Ok(path) containing the extension of the file path or Err(err) if the path is a directory path |
Examples:
1 | extension(fromString("/dir/")) == Err(IncompatiblePathType) // can only take extension of a file path |
Path.root
Added in 0.5.5
No other changes yet.
Retrieves the root of the absolute path.
Parameters:
param | type | description |
---|---|---|
path |
Path |
The path to inspect |
Returns:
type | description |
---|---|
Result<AbsoluteRoot, PathOperationError> |
Ok(root) containing the root of the path or Err(err) if the path is a relative path |
Examples: