tabnas-ini (Rust)
The INI grammar plugin for the
tabnas parsing engine, crate
tabnas_ini.
It reads INI text into plain values: [section] headers, dotted section
paths ([a.b.c]), key = value pairs, repeated-key arrays
(tags[] = x), bare boolean keys, quoted values, line and inline
comments, and values that span lines by backslash continuation or by
indent.
It is not standalone. The relaxed-JSON core (val / map / pair)
comes from the tabnas-jsonic
plugin; this crate installs the INI rules over it, sets the start rule to
ini, and lexes keys, values and section-path segments with
tabnas-hoover, because an INI value
is not a JSON value.
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
../ini-grammar.jsonic.
Use
Numbers stay strings. INI has no number syntax, so port = 8080 is the
text 8080, and 2.5, -3 and 0xFF are the text a reader wrote.
The keywords true, false and null are values when they are the
WHOLE value, and text when they are not:
parse reuses one shared instance, because building the grammar costs
much more than a parse. To hold your own instance, build one:
Options are a typed struct with a default per field, so a parser with one setting changed names that setting and nothing else:
use ;
A value spans lines when multiline is set, by a trailing continuation
character, by an indented next line, or by both:
use ;
To layer INI on an instance of your own, install the plugin, or call the
grammar function directly. Either way the instance must already carry the
jsonic grammar, which supplies the val rule both this plugin and hoover
extend:
use IniOptions;
Parse errors are the engine's TabnasError, re-exported as IniError,
with code, row, col and a report that shows the offending source
with a caret under it. This plugin declares two codes of its own,
duplicate_section and unterminated_section; everything else surfaces
through the engine's own codes, unexpected for a malformed section
header and cancel for a document nested past the depth limit among
them. The CODE is the cross-runtime contract, and the message wording is
not.
Install
None of these crates is published to a registry, so they are consumed as
sibling checkouts, the standard tabnas development model. Clone
https://github.com/tabnas/parser, https://github.com/tabnas/jsonic,
https://github.com/tabnas/hoover and https://github.com/tabnas/json
next to this repository, then point at the ones you name directly:
[]
= { = "../ini/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-ini alone
does not put tabnas or tabnas_jsonic in your extern prelude. Only
IniError 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 while resolving, and no example is ever reached.
Running this crate's own test suite needs a fifth 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. What differs is the shape of the API and a few
points where the host language has no way to say what JavaScript says.
Any difference in what a document PARSES TO is recorded in
../DIVERGENCE.md instead.
- The crate adds functions the plugin does not need. TypeScript
exports the plugin and nothing else: a caller builds an engine and
registers
Inion it. This crate also carriesparse,parse_with,makeandmake_with, which is the shape the Go port has, because a Rust caller that wants one line should not have to assemble three crates to get it.iniandpluginare the plugin itself, and are what the TypeScript export corresponds to. - Options are a typed struct. TypeScript takes a plain object whose
every field is optional; here the struct spells the same shape, an
absent field is
None, andIniOptions::default()is the TypeScript{}.Duplicatenames the three settings TypeScript spells as strings, andMultilineOptions::continuationisSome("")where TypeScript writesfalse. The builderswith_multiline,with_inline_commentsandwith_duplicatecover the common settings. - Section state is per parse. The canonical ports keep the set of
declared section headers in a closure shared by every parse, which is
safe there because neither runtime parses one instance from two
threads. A
TabnasisSend + Syncandparseshares one instance, so the set lives on the parse context here. - A section node is rebuilt, not referenced. A
Valuecontainer sits behind anArcand copies when a second handle writes to it, so a table rule cannot hold a live handle on its section the way a JavaScript object reference does. The section is written back into the document as the rule closes, and the document that comes out is the same one. - The lexer hooks read a probe. The four
checkhooks all ask where the parser is, which TypeScript reads fromlex.ctx.rule. A Rust check is handed the lexer alone, so a custom matcher ordered below every built-in family records the rule for them, one step earlier in the same token attempt. - Nesting is bounded. A section path of a few thousand segments
builds a value tree the engine walks with the call stack, so a parse
budget refuses one deeper than 127 levels with the engine's
cancelcode. Every runtime bounds it at the same number. - Key order is document order, because the result is built on an
IndexMap. - An escaped lone surrogate becomes U+FFFD. A single-quoted value is
read as JSON, and
JSON.parsekeeps a lone surrogate because a JavaScript string is UTF-16. A RustStringcannot hold one, so this port substitutes the replacement character, as Go'sencoding/jsondoes, which keeps the type and the shape of the value. A single-quoted JSON value nested past 127 levels keeps its source text, for the reason nesting is bounded above, where the other two runtimes read it as a value. Both are in../DIVERGENCE.md. - 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.md. - 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.
Build and test
The engine, the jsonic core, the hoover block lexer, 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, with a census test so a renamed
or deleted fixture is a failure rather than a silent loss of coverage. It
also runs the third-party corpus in ../test/corpus/ini-corpus.json,
with the same divergence lists the other two suites carry and the same
canonical results in ../test/corpus/ini-canonical.json. Beside them
are the in-language tests for what a fixture cannot express: the option
matrix, the API surface, the embedded grammar, the version constants,
hostile input, and the shared default parser under threads.
License
MIT.