from(text: string) - > Result<Path, string>- Модуль
- std.
path → - Получатель
- Неприменимо
- Минимальное число аргументов
- 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")
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Этап отклонения check. Ненулевой код завершения.
stdlib-removed-member at
main.tpz:2:18
Reject Path.project
Ссылкаimport std.path { Path }let value = Path.project("chapter.tpz")
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Этап отклонения check. Ненулевой код завершения.
stdlib-removed-member at
main.tpz:2:18
Reject Path.normalize
Ссылкаimport std.path { Path }function inspect(path: Path) -> () {let value = path.normalize()return ()}
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Этап отклонения check. Ненулевой код завершения.
stdlib-removed-member at
main.tpz:3:20
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:é:가: