from(text: string) - > Result<Path, string>- 수신자
- 해당 없음
- 최소 인자 수
- 1
- 최대 인자 수
- 1
Declaration and parameters
링크사용 조건
기본 포함 · 미정
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.
수신자
매개변수 제약
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.
관련 규칙 · Construct normalized paths with Path.from →결과
The declared result type is Result<Path, string>.
효과
실패
Result and effects
링크Construct a normalized relative Path and return invalid path text as Err. Existing Path values already satisfy the normalization invariant.
Why
링크Normalizing at construction makes every Path operation use the same representation. A second constructor or an identity normalize call adds no path meaning.
Related entries
링크Construct normalized paths with Path.from
링크Construct 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.
이유
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.
Construct a normalized path
링크import std.path { Path }import std.testimport std.iolet chapter = test.assertOk(Path.from("chapters/./draft/../main.tpz"))io.writeLine(chapter.toString())
chapters/main.tpz
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
chapters/main.tpz
Reject a path that escapes its relative root
링크import 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
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
outside root rejected
Reject Path.cwdRelative
링크import std.path { Path }let value = Path.cwdRelative("chapter.tpz")
표준 출력 없음.기대 결과
Reject Path.project
링크import std.path { Path }let value = Path.project("chapter.tpz")
표준 출력 없음.기대 결과
Reject Path.normalize
링크import std.path { Path }function inspect(path: Path) -> () {let value = path.normalize()return ()}
표준 출력 없음.기대 결과
Distinguish rejected ASCII prefixes from relative path text
링크import 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:
é:
가:
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
aa:é:가: