parse(text: string) - > Result<TOML Value, string>- Модуль
- std.
toml → - Получатель
- Неприменимо
- Минимальное число аргументов
- 1
- Максимальное число аргументов
- 1
Declaration and parameters
СсылкаДоступность
Включено по умолчанию · Не определено
Import std.toml in language mode 5.21.
The current compiler selects std/toml.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
Получатель
Ограничения параметров
parse accepts a complete TOML 1.0 document. An empty document or comments with whitespace produce an empty root table. Basic and literal strings, multiline strings, dotted and quoted keys, inline tables, arrays and arrays of tables retain their TOML meanings.
Integers must fit the signed 64 bit range. TOML decimal, hexadecimal, octal and binary spellings and valid underscore separators are accepted. Floating values use IEEE binary64 rounding. The TOML inf and nan forms are valid TOML values. Finite overflow follows the TOML numeric error boundary and returns Err.
Offset date times, local date times, local dates and local times remain distinct TOML kinds. Invalid calendar fields and time fields return Err. Strings that resemble dates remain strings.
A key or table definition may not redefine an existing value under the TOML 1.0 rules. Names are compared after decoding. Malformed escapes, leading zeroes and misplaced underscore separators return Err without exposing a partial document.
A failure returns a nonempty string identifying the cause, one based line and Unicode scalar column. CRLF is a single line break. The first offending scalar is the location and an incomplete input points immediately after its last scalar. Source comments and layout are not retained in TOMLValue.
The text argument is evaluated once. Parsing consumes the complete document, performs no I/O and does not invoke JSON conversion. Syntax failures use Err rather than a runtime diagnostic.
Результат
The declared result type is Result<TOML.
Эффекты
Ошибки
Result and effects
СсылкаParse a complete TOML 1.0 document and report invalid input with a cause and position.
Related entries
СсылкаParse complete TOML 1.0 documents
СсылкаParse complete TOML 1.0 documents
parse accepts a complete TOML 1.0 document. An empty document or comments with whitespace produce an empty root table. Basic and literal strings, multiline strings, dotted and quoted keys, inline tables, arrays and arrays of tables retain their TOML meanings.
Integers must fit the signed 64 bit range. TOML decimal, hexadecimal, octal and binary spellings and valid underscore separators are accepted. Floating values use IEEE binary64 rounding. The TOML inf and nan forms are valid TOML values. Finite overflow follows the TOML numeric error boundary and returns Err.
Offset date times, local date times, local dates and local times remain distinct TOML kinds. Invalid calendar fields and time fields return Err. Strings that resemble dates remain strings.
A key or table definition may not redefine an existing value under the TOML 1.0 rules. Names are compared after decoding. Malformed escapes, leading zeroes and misplaced underscore separators return Err without exposing a partial document.
A failure returns a nonempty string identifying the cause, one based line and Unicode scalar column. CRLF is a single line break. The first offending scalar is the location and an incomplete input points immediately after its last scalar. Source comments and layout are not retained in TOMLValue.
The text argument is evaluated once. Parsing consumes the complete document, performs no I/O and does not invoke JSON conversion. Syntax failures use Err rather than a runtime diagnostic.
Причина
Configuration files need a named format version and predictable rejection of invalid definitions. Preserving TOML kinds lets callers choose a separate and explicit conversion into JSON.
Parse a table and write its deterministic form
Ссылкаimport std.tomlimport std.testimport std.iolet value = test.assertOk(toml.parse("title = \"Topaz\"\ncount = 6\nactive = true\n[page]\nwidth = 210\n"))io.writeLine(test.assertOk(toml.stringify(value)))let empty = test.assertOk(toml.parse(" # comment\n"))test.assertEq(test.assertOk(toml.stringify(empty)), "")
active = true
count = 6
title = "Topaz"
[page]
width = 210
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
active = truecount = 6title = "Topaz"[page]width = 210
Accept literal strings, multiline values, radix integers and dates
Ссылкаimport std.tomlimport std.iofor text in (["title = 'ink#paper'\n", "text = \"\"\"\nfirst\nsecond\"\"\"\n", "value = 0xff\n", "date = 1979-05-27T07:32:00Z\n", "value = inf\n", "value = nan\n"]) {match (toml.parse(text)) {case Ok(_) => io.writeLine("accepted")case Err(_) => io.writeLine("rejected")}}
accepted
accepted
accepted
accepted
accepted
accepted
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
acceptedacceptedacceptedacceptedacceptedaccepted
Reject malformed numeric spellings and repeated definitions
Ссылкаimport std.tomlimport std.iofor text in (["n = 1__0\n", "n = 01\n", "[a]\nx = 1\n[a]\ny = 2\n", "x = 1\nx = 2\n"]) {match (toml.parse(text)) {case Ok(_) => io.writeLine("accepted")case Err(_) => io.writeLine("invalid")}}
invalid
invalid
invalid
invalid
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
invalidinvalidinvalidinvalid
Return line and scalar column in a TOML error
Ссылкаimport std.tomlimport std.testimport std.iolet error = test.assertErr(toml.parse("x = ?"))test.assertContains(error, "line 1")test.assertContains(error, "column 5")io.writeLine("located")
located
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
located