parse(text: string) - > Result<TOML Value, string>- Module
- std.
toml - Receiver
- Not applicable
- Minimum arity
- 1
- Maximum arity
- 1
Declaration and parameters
LinkAvailability
Included by default: Pending
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.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1790 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 70 - compiler/crates/topaz_resolve/src/std/521/toml.tpz at
b48527d0a174· 3 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1790 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 208 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1973 - compiler/crates/topaz_self_frontend/topaz/checker/call_facts.tpz at
b48527d0a174· 978 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1254
Receiver
Parameter constraints
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.
Result
The declared result type is Result<TOML.
Effects
Failures
Result and effects
LinkParse a complete TOML 1.0 document and report invalid input with a cause and position.
Related entries
LinkParse complete TOML 1.0 documents
LinkParse 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.
Why
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.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 919 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1031 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1123 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1276 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 136 - toml.md at
1.0.0· 1 -
topaz.stdlib.contract.toml-parseatworking-tree - topaz.decision.toml-one-and-conversion.1 at
topaz.decisi
Parse a table and write its deterministic form
Linkimport 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
Expected result
Expected result
Execution completes. Exit code 0.
active = truecount = 6title = "Topaz"[page]width = 210
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 919 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1031 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1123 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1276 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 136 - toml.md at
1.0.0· 1 -
topaz.stdlib.contract.toml-parseatworking-tree - topaz.decision.toml-one-and-conversion.1 at
topaz.decisi
Accept literal strings, multiline values, radix integers and dates
Linkimport 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
Expected result
Expected result
Execution completes. Exit code 0.
acceptedacceptedacceptedacceptedacceptedaccepted
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 919 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1031 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1123 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1276 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 136 - toml.md at
1.0.0· 1 -
topaz.stdlib.contract.toml-parseatworking-tree - topaz.decision.toml-one-and-conversion.1 at
topaz.decisi
Reject malformed numeric spellings and repeated definitions
Linkimport 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
Expected result
Expected result
Execution completes. Exit code 0.
invalidinvalidinvalidinvalid
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 919 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1031 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1123 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1276 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 136 - toml.md at
1.0.0· 1 -
topaz.stdlib.contract.toml-parseatworking-tree - topaz.decision.toml-one-and-conversion.1 at
topaz.decisi
Return line and scalar column in a TOML error
Linkimport 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
Expected result
Expected result
Execution completes. Exit code 0.
located
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 919 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1031 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1123 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1276 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 136 - toml.md at
1.0.0· 1 -
topaz.stdlib.contract.toml-parseatworking-tree - topaz.decision.toml-one-and-conversion.1 at
topaz.decisi