Construct and manipulate opaque filesystem paths without accessing the filesystem.
Import
import stdlib/path as path
from_text()
pure from_text(value: Str): Result[Path, PathError]
Parameters
value: Str— nonempty path text without NUL.
Returns: Result[Path,PathError].
Pure. Encodes path text without checking whether it exists.
Errors: EmptyPath or PathContainsNul.
from_native()
pure from_native(value: Bytes): Result[Path, PathError]
Parameters
value: Bytes— nonempty native POSIX path bytes without NUL.
Returns: Result[Path,PathError].
Preserves native bytes, including names which are not valid UTF-8. Does not access the filesystem.
Errors: EmptyPath or PathContainsNul.
current()
pure current(): Path
Parameters
None.
Returns: Path, the lexical current-directory path.
Pure. This is not a host call resolving an absolute working directory.
Path methods
All methods are pure and operate on the path value without accessing the filesystem.
join()
pure join(child: Path): Result[Path,PathError]
Parameters
child: Path: the relative path to append.
Returns: Result[Path,PathError]. Ok contains the joined path.
Errors: AbsolutePathAppend if the child is absolute. Joining does not establish filesystem confinement.
parent()
pure parent(): Path
Parameters
None.
Returns: Path, the lexical parent.
The compatibility method directory() has the same signature and behaviour.
filename()
pure filename(): Option[Path]
Parameters
None.
Returns: Option[Path]. Some contains the final path component; None means there is no filename component.
is_absolute()
pure is_absolute(): Bool
Parameters
None.
Returns: Bool, true when the path has an absolute form.
native_bytes()
pure native_bytes(): Bytes
Parameters
None.
Returns: Bytes, the exact native representation, including bytes which are not valid UTF-8.
to_text()
pure to_text(): Result[Str,PathError]
Parameters
None.
Returns: Result[Str,PathError]. Ok contains the path decoded as UTF-8.
Errors: PathNotUtf8 if the native bytes are not valid UTF-8.
display()
pure display(): Str
Parameters
None.
Returns: Str, diagnostic text even when the path is not valid UTF-8.
Do not use display text to reconstruct a native path.
PathError
PathError is EmptyPath, PathContainsNul, PathNotUtf8 or AbsolutePathAppend. These are path-value failures, separate from FilesystemError returned by host operations.
Example
The temporary-file example uses paths without treating them as strings.
Implementation: view source on GitHub.