Перейти к содержимому
Топаз6.0 · До релиза
Загрузка →

Поиск в документации

Спецификация 6.0

Выберите результат стрелками и нажмите Enter, чтобы открыть его.

Введите имя API, номер диагностики, правило или тему проекта.

    Библиотека · std.path

    Path.from

    Validates and normalizes text into a logical project-relative Path.

    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.

    Результат

    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.

    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

    Ссылка

    std.path →

    Construct normalized paths with Path.from

    Ссылка
    Правилоtopaz.stdlib.contract.path-from.rule

    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

    Ссылка
    main.tpzConstruct a normalized pathОткрыть песочницу →
    1. import std.path { Path }
    2. import std.test
    3. import std.io
    4. let chapter = test.assertOk(Path.from("chapters/./draft/../main.tpz"))
    5. io.writeLine(chapter.toString())
    Ожидаемый выводКод завершения 0
    chapters/main.tpz
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. chapters/main.tpz

    Reject a path that escapes its relative root

    Ссылка
    main.tpzReject a path that escapes its relative rootОткрыть песочницу →
    1. import std.path { Path }
    2. import std.io
    3. match (Path.from("../chapter.tpz")) {
    4. case Err(_) => io.writeLine("outside root rejected")
    5. case Ok(_) => io.writeLine("accepted")
    6. }
    Ожидаемый выводКод завершения 0
    outside root rejected
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. outside root rejected

    Reject Path.cwdRelative

    Ссылка
    main.tpzReject Path.cwdRelativeОткрыть песочницу →
    1. import std.path { Path }
    2. let value = Path.cwdRelative("chapter.tpz")
    Ожидаемый выводНенулевой код завершения
    Нет стандартного вывода.
    $ topaz check main.tpz --format json · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Этап отклонения check. Ненулевой код завершения.

    Reject Path.project

    Ссылка
    main.tpzReject Path.projectОткрыть песочницу →
    1. import std.path { Path }
    2. let value = Path.project("chapter.tpz")
    Ожидаемый выводНенулевой код завершения
    Нет стандартного вывода.
    $ topaz check main.tpz --format json · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Этап отклонения check. Ненулевой код завершения.

    Reject Path.normalize

    Ссылка
    main.tpzReject Path.normalizeОткрыть песочницу →
    1. import std.path { Path }
    2. function inspect(path: Path) -> () {
    3. let value = path.normalize()
    4. return ()
    5. }
    Ожидаемый выводНенулевой код завершения
    Нет стандартного вывода.
    $ topaz check main.tpz --format json · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Этап отклонения check. Ненулевой код завершения.

    Distinguish rejected ASCII prefixes from relative path text

    Ссылка
    main.tpzDistinguish rejected ASCII prefixes from relative path textОткрыть песочницу →
    1. import std.path { Path }
    2. import std.test
    3. import std.io
    4. test.assertErr(Path.from("a:"))
    5. test.assertErr(Path.from("1:"))
    6. test.assertErr(Path.from("!:"))
    7. io.writeLine(test.assertOk(Path.from("aa:")).toString())
    8. io.writeLine(test.assertOk(Path.from("é:")).toString())
    9. io.writeLine(test.assertOk(Path.from("가:")).toString())
    Ожидаемый выводКод завершения 0
    aa:
    é:
    가:
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. aa:
    2. é:
    3. 가:

    Diagnostics

    Ссылка

    Unknown member or field →

    Меню

    Загрузка →