tabnas-toml (Rust)
The TOML grammar plugin for the
tabnas parsing engine, crate
tabnas_toml.
It reads TOML into plain values: bare, quoted and dotted keys, [table]
headers, [[array-of-tables]], inline tables, arrays, basic, literal and
multi-line strings, integers and floats including inf and nan,
booleans, and date and time values.
It is not standalone. The relaxed-JSON core (val / map / list /
pair / elem) comes from the
tabnas-jsonic plugin; this crate
installs the TOML rules over it, sets the start rule to toml, and adds
a TOML string matcher and context-aware date and time matchers.
This is the Rust port of the canonical TypeScript implementation in
../ts; the TypeScript version is authoritative and this crate
tracks it. The Go port is in ../go. All three embed the same
grammar, authored once in
../toml-grammar.jsonic.
Use
parse reuses one shared instance, because building the grammar costs
much more than a parse. To hold your own instance, build one:
To layer TOML on an instance of your own, install the plugin, or call the grammar function directly:
A date, time or date-time carries its kind alongside its source text.
Read it with toml_time:
Parse errors are the engine's TabnasError, re-exported as TomlError,
with code, row, col and a report that shows the offending source
with a caret under the [toml/<code>] tag. Beyond the engine's own
codes, this plugin raises toml_key_conflict for a key TOML does not
allow to be redefined, invalid_datetime for a date or time whose
components cannot denote a real instant, and unterminated_string,
unprintable, invalid_ascii and invalid_unicode from the string
matcher.
Install
The crate, the jsonic core and the engine are published on crates.io.
The engine's package is tabnas-parser, imported in code as tabnas:
To build from source instead, as this repository does, they are consumed as
sibling checkouts, the standard tabnas development model. Clone all
three of https://github.com/tabnas/parser,
https://github.com/tabnas/jsonic and https://github.com/tabnas/json
next to this repository, then point at the two you name directly:
[]
= { = "../toml/rs" }
= { = "../jsonic/rs" }
= { = "tabnas-parser", = "../parser/rs" }
All three entries are needed for the examples above. A crate's
dependencies are not passed on to its dependents, so tabnas-toml alone
does not put tabnas or tabnas_jsonic in your extern prelude. Only
TomlError is re-exported.
json is the one checkout with no entry in that table, and it is not
optional: tabnas-jsonic reaches the strict-JSON core through its own
path dependency on ../../json/rs. Cargo reads every manifest in the
graph before it compiles anything, so without that checkout the build
stops at failed to get tabnas-json as a dependency of package tabnas-jsonic while resolving, and no example is ever reached.
Running this crate's own test suite needs a fourth checkout,
https://github.com/tabnas/support, which holds the shared fixture
loader and runner. It is a development dependency, so a crate that only
consumes the library does not need it.
Differences from the canonical TypeScript
The shared fixtures in ../test/spec hold all three
runtimes to one answer, and the
BurntSushi/toml-test corpus
holds this crate to the same counts as TypeScript: every one of its 268
valid documents, and the same 278 rejections of its 509 invalid ones,
recorded in ../test/conformance.tsv. What
differs is the shape of the API and a few points where the host language
has no way to say what JavaScript says:
- A date is a
Textvalue, not aDate.tabnas::Valueis a closed enum with no room for a host type, so a date, time or date-time travels as aValue::Textwhosestringis the source and whosequoteis the kind.toml_timereads it back. It renders as the source text, which is close to whatJSON.stringifydoes to the TypeScriptDateand more readable than the Go struct. - Configuration is a type, not an options object.
TomlOptionscarries nothing in any port;make_withtakes it so that adding an option later is not a breaking change. - Key order is document order, because the result is built on an
IndexMap. - A table node is a path, not a reference. A
Valuecontainer is behind anArcand copies when a second handle writes to it, so a table rule holds the route from the document root to its table rather than a handle on it. The document that comes out is the same one. - This crate refuses nesting past 127 levels with the engine's
cancelcode, from a parse guard of its own (DEPTH_LIMIT,DEPTH_GUARD). Every container the value sits in counts as a level, the root table first: each table a dotted key or a header passes through, an array of tables and its element, an inline table, an array. A dotted key inside an inline table counts on from the keys outside it. The number is jsonic's, and the reason is the Rust value's: displaying, converting or dropping aValuewalks it with the call stack, so an unbounded document would end the process rather than return an error. TypeScript and Go have no limit. The two rows in../test/divergent.tsvrecord that. - Lifting the guard lifts every depth bound. The guard replaces the
one jsonic installs under the same name, which bounds inline tables and
arrays alone, so after
parser.remove_parse_guard(DEPTH_GUARD)nothing bounds them either, and a document of any depth parses. A caller that lifts the guard needs a stack deep enough to display and drop the value it accepts. - Columns count Unicode scalar values. An astral character advances
the column by one, where TypeScript counts UTF-16 units and advances by
two. That is the engine's unit, recorded in its own
DIVERGENCE.mdand in../test/divergent.tsv, which every runtime executes. - Malformed UTF-8 cannot reach the parser. The engine parses a
&str, so a document that is not valid UTF-8 has to be decoded before it arrives. The conformance suite decodes with replacement characters, which is what Node hands the TypeScript suite. - Lone surrogates fold to U+FFFD, and the regular expression dialect
is the
regexcrate's. Both come from the engine, and both are recorded there. A lone surrogate is the one difference the register cannot carry: its cells are JSON, and the JSON decoders that read them fold a lone surrogate to U+FFFD as well, so every cell would compare equal. Go answers U+FFFD here too. - A lax hexadecimal escape follows TypeScript.
"\u12g4"reads as U+0012 in TypeScript and here, because the canonical scan accepts every ASCII letter and then takes the longest hexadecimal prefix. Go rejects it. The row in../test/divergent.tsvrecords the three answers, and Go's rejection is the repair target: TOML requires exactly four hexadecimal digits, so"\u12g4"is not a TOML document and Go is the only one of the three that says so. The ports that move are TypeScript and this one.
Build and test
The engine, the jsonic core and the fixture runner are path dependencies on sibling checkouts, so there is nothing to fetch:
Or, from the repository root, make test-rs. For what CI would say,
including formatting and the lockfile check, run ci/rust/run.sh.
The suite runs every shared ../test/spec/*.tsv fixture, the same files
the TypeScript and Go suites run, discovered by listing the directory so
a new fixture runs everywhere at once. Beside them are the divergence
register, the conformance corpus (both halves, and it never skips: if the
corpus cannot be fetched the suite fails), and the in-language tests for
what a fixture cannot express: the API surface, special floats,
multi-line strings, date kinds, the leading byte order mark, error
columns, key conflicts, the embedded grammar, and the shared default
parser under threads.
The conformance corpus is not committed. ../scripts/fetch-toml-test.sh
clones it, pinned to an exact commit, and the suite runs that script
itself when the corpus is missing.
License
MIT.