from(text: string) - > Result<Path, string>- Module
- std.
path - Receiver
- Not applicable
- Minimum arity
- 1
- Maximum arity
- 1
Declaration and parameters
LinkAvailability
Included by default: Pending
Import std.path in language mode 5.21.
The current compiler selects std/path.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1735 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 54 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1735 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 182 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1973
Receiver
Parameter constraints
Path.from(text) is the constructor for normalized relative paths and returns Result<Path, string>.
Path.from normalizes path separators and removes empty and dot components. Parent components must stay within the relative root. A path reduced to no components is represented by a dot.
Empty text, NUL, absolute paths, and parent traversal beyond the relative root return Err. These input failures are values that a caller can handle.
A leading slash or backslash returns Err. Text that starts with one ASCII character followed immediately by a colon returns Err.
Path.cwdRelative and Path.project are absent. A Path value already satisfies normalization and has no normalize member.
Path operations inspect text without accessing the filesystem. Constructing a Path does not establish that its target exists or is accessible.
Related rule: Construct normalized paths with Path.fromResult
The declared result type is Result<Path, string>.
Effects
Failures
Result and effects
LinkConstruct a normalized relative Path and return invalid path text as Err. Existing Path values already satisfy the normalization invariant.
Why
LinkNormalizing at construction makes every Path operation use the same representation. A second constructor or an identity normalize call adds no path meaning.
Related entries
LinkConstruct normalized paths with Path.from
LinkConstruct normalized paths with Path.from
Path.from(text) is the constructor for normalized relative paths and returns Result<Path, string>.
Path.from normalizes path separators and removes empty and dot components. Parent components must stay within the relative root. A path reduced to no components is represented by a dot.
Empty text, NUL, absolute paths, and parent traversal beyond the relative root return Err. These input failures are values that a caller can handle.
A leading slash or backslash returns Err. Text that starts with one ASCII character followed immediately by a colon returns Err.
Path.cwdRelative and Path.project are absent. A Path value already satisfies normalization and has no normalize member.
Path operations inspect text without accessing the filesystem. Constructing a Path does not establish that its target exists or is accessible.
Why
One constructor gives relative path text one interpretation. Normalization is a Path invariant, so a separate identity operation gives the same value a second spelling.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 260 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 317 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 423 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1391 -
topaz.stdlib.contract.path-fromatworking-tree - topaz.decision.path-from.1 at
topaz.decisi
Construct a normalized path
Linkimport std.path { Path }import std.testimport std.iolet chapter = test.assertOk(Path.from("chapters/./draft/../main.tpz"))io.writeLine(chapter.toString())
chapters/main.tpz
Expected result
Expected result
Execution completes. Exit code 0.
chapters/main.tpz
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 260 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 317 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 423 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1391 -
topaz.stdlib.contract.path-fromatworking-tree - topaz.decision.path-from.1 at
topaz.decisi
Reject a path that escapes its relative root
Linkimport std.path { Path }import std.iomatch (Path.from("../chapter.tpz")) {case Err(_) => io.writeLine("outside root rejected")case Ok(_) => io.writeLine("accepted")}
outside root rejected
Expected result
Expected result
Execution completes. Exit code 0.
outside root rejected
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 260 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 317 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 423 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1391 -
topaz.stdlib.contract.path-fromatworking-tree - topaz.decision.path-from.1 at
topaz.decisi
Reject Path.cwdRelative
Linkimport std.path { Path }let value = Path.cwdRelative("chapter.tpz")
No stdout.
Expected result
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 260 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 317 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 423 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1391 -
topaz.stdlib.contract.path-fromatworking-tree - topaz.decision.path-from.1 at
topaz.decisi
Reject Path.project
Linkimport std.path { Path }let value = Path.project("chapter.tpz")
No stdout.
Expected result
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 260 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 317 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 423 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1391 -
topaz.stdlib.contract.path-fromatworking-tree - topaz.decision.path-from.1 at
topaz.decisi
Reject Path.normalize
Linkimport std.path { Path }function inspect(path: Path) -> () {let value = path.normalize()return ()}
No stdout.
Expected result
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 260 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 317 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 423 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1391 -
topaz.stdlib.contract.path-fromatworking-tree - topaz.decision.path-from.1 at
topaz.decisi
Distinguish rejected ASCII prefixes from relative path text
Linkimport std.path { Path }import std.testimport std.iotest.assertErr(Path.from("a:"))test.assertErr(Path.from("1:"))test.assertErr(Path.from("!:"))io.writeLine(test.assertOk(Path.from("aa:")).toString())io.writeLine(test.assertOk(Path.from("é:")).toString())io.writeLine(test.assertOk(Path.from("가:")).toString())
aa: é: 가:
Expected result
Expected result
Execution completes. Exit code 0.
aa:é:가:
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 260 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 317 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 423 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1391 -
topaz.stdlib.contract.path-fromatworking-tree - topaz.decision.path-from.1 at
topaz.decisi