tabnas-ini 0.5.21

INI syntax plugin for the tabnas parser engine.
Documentation
# Agents Guide: rs/

The Rust port of the canonical TypeScript in [`../ts`](../ts). Read
[`../AGENTS.md`](../AGENTS.md) first: it holds the cross-runtime rules,
and this file only covers what is specific to this crate.

## Layout

| Path | |
|---|---|
| `src/lib.rs` | the whole port: the typed options, the embedded grammar text, every closure the grammar names, the hoover blocks, the lexer hooks, the custom value matcher, the `val` rule, `ini`, `plugin`, `make`, `make_with`, `parse`, `parse_with`, and the translation parts `manifest_text` and `render_text`, `include_str!` of the copies in `translate/` |
| `translate/` | the crate's copies of `../tabnas.plugin.json` (as `manifest.json`) and `../alchemy/render.alc`, which a packaged crate needs, written by `npm run embed` in `../ts`; `tests/translate_test.rs` holds them to the files |
| `tests/parity_test.rs` | every shared `../test/spec/*.tsv` fixture through `tabnas_support::Runner`, one runner per file, with the per-file option table |
| `tests/ini_test.rs` | in-language behaviour: the Rust twin of `go/ini_test.go`, plus the construction API, the threading contract and hostile input |
| `tests/conformance_test.rs` | the third-party corpus in `../test/corpus/ini-corpus.json`, with the same divergence lists the other two runtimes carry |
| `tests/grammar_test.rs` | the embedded grammar against `../ini-grammar.jsonic`, and against the TypeScript and Go embeds |
| `tests/perf_test.rs` | `parse` reuses its instance, building one really is dear, and many sections stay near linear |
| `tests/translate_test.rs` | the translation parts: the render the embedded manifest names is the one `render_text()` embeds, the embed it names is the one `translate()` carries (none, for INI), the manifest's shapes, root, language and loss lines, and every render definition named `ini-...` |
| `tests/version_test.rs` | Cargo.toml == `VERSION` == ts/package.json == the Go `const VERSION` |
| `tests/common/mod.rs` | the per-fixture option table and the two conversions the runner needs |
| `README.md` | the crate front page, prose-gated; its `rust` fences are doctests of this crate |

Crate `tabnas-ini`, library `tabnas_ini`. The engine (`tabnas`), the
relaxed-JSON base (`tabnas-jsonic`), the block lexer (`tabnas-hoover`)
and the fixture runner (`tabnas-support`, dev only) are **path
dependencies on sibling checkouts**. jsonic takes the JSON core
(`tabnas-json`) the same way, so that checkout is needed too. All of
them are on crates.io, but the committed manifest names them by path
alone, so there is no registry version to fall back on.

```bash
cargo build --all-targets
cargo test --all-targets && cargo test --doc
cargo clippy --all-targets --all-features -- -D warnings
cargo fmt
```

`make test-rs` from the repository root is the fast loop; `ci/rust/run.sh`
is the full gate and adds `fmt --check`, the lockfile check and the MSRV
pin.

## How the grammar is layered

`ini` does, in order:

1. Refuses an instance with no `val` rule. jsonic supplies `val`, `map`
   and `pair`, and hoover refuses a bare engine, so installing over
   nothing would leave half a grammar.
2. Registers the in-value probe and the four lexer `check` hooks.
3. Registers the custom value matcher factory.
4. Installs the three hoover blocks: `endofline` (`#HV`), `key` (`#HK`)
   and `divekey` (`#DK`).
5. Registers every closure the grammar names.
6. Parses the embedded grammar text with a stock jsonic instance and
   installs the result.
7. Snapshots the string configuration the string check reads, which the
   document above is what sets.
8. Rewrites the `val` rule, removes `list` and `elem`, and installs the
   depth guard.

Order is load bearing at every step. The hoover blocks must exist before
the grammar document, because the document's `val` alternates sit in
front of hoover's. The refs must exist before the document, because the
document is what looks them up. The guard goes after jsonic's, which it
replaces by installing under the same name.

## The in-value probe

Four `check` hooks in the canonical port ask the same question: where is
the parser right now? TypeScript reads `lex.ctx.rule` and Go reads
`lex.Ctx.Rule`. **The Rust engine hands a check only the live `Lexer`,
which carries no rule**, so the answer is recorded one step earlier: a
custom matcher in the band below every built-in family (order 0.5e6,
where the first built-in band is 1e6) receives the rule, writes two flags
into a thread-local, and matches nothing. Every check that runs
afterwards, in the same `next_token` call on the same thread, reads them
back.

Thread-local rather than instance state, because a `Tabnas` is shared
across threads by `parse` and because the flags are written and read
inside one lexer call. Do not move them onto the instance.

Two flags, not one: `@line-check` asks only whether the rule is a `val`,
where the other three ask the fuller question the TypeScript `inValue`
asks (a `val` in its open state whose parent is a `pair` or an `elem`).

## Why the section tree is taken apart and put back together

A container here is a `Value`, which is an `Arc`, not a reference. The
canonical ports hold a live reference to the section object and mutate it
in place; this port cannot, so `open_section` walks the dive path down
from the document root and `close_section` writes the finished section
back up. Three rules about that code:

- **Iterative, never recursive.** A section header thousands of segments
  deep aborted the process through the call stack.
- **Take, do not clone.** `object_take` replaces a slot's value with
  nothing and keeps the key in place, so the container has ONE owner
  while it is written to. Reading with `object_get` (which clones) leaves
  two owners, and the next write copies the whole map: a document of
  5,000 sections took 5 seconds instead of 0.6.
- **The key keeps its position.** A remove-then-insert would move a
  section to the end of its parent every time it was reopened.

The same rule governs the declared-section set and the `table` rule's
`child_node`. `table_before_close` drops its copy of a child node that
shares its own cell, which is what the canonical `Object.assign(x, x)`
costs nothing to do.

## Per-parse state lives on the context

The canonical ports keep `declaredSections` in a closure, which is
instance state shared by every parse. `Tabnas` is `Send + Sync` and
`parse` shares one instance across threads, so here it lives in
`context.u` under `ini_declared`. `concurrent_parses_do_not_share_declared_sections`
in `ini_test.rs` fails if it moves back.

## `dive` on a table means the NEXT table

A table reads its section path from `r.prev.u.dive`, so `dive` on a
table's `u` bag means "the header that opened the table after this one",
not "the section this table is writing into". Recording the path under
that name would make every table inherit its predecessor's section. The
path this table is writing into is `ini_path`; keep the two apart.

## A rule cannot write its parent's `u` bag

`rule.parent_rule` is an immutable snapshot, so the canonical
`r.parent.u.dive.push(...)`, which grows an array the parent already
holds, has no counterpart. Each level reads its CHILD's bag instead:
`@dive-push` builds its own path from its parent's, `@dive-bc` copies the
completed child's path up, and `@table-close-dive` does the same at the
table. The Go port added `@dive-bc` for the same reason, a slice being a
value there.

The array a `k[] = v` pair grows is read back off the parent's NODE for
the same reason, rather than out of the parent's `u` bag.

## The fixed-token chain writes back into every link

A value that starts with `[`, `]`, `=` or `.` replaces the `val` rule
once per leading fixed token, and only the LAST `val` of that chain runs
the after-close hook. `val_after_close` walks the chain through
`prev_rule`, prepends each link's token source, and writes the text
accumulated so far into EVERY link's node, as the canonical ports do
(`p.node = r.node` at `../ts/src/ini.ts:530`, `p.Node = r.Node` at
`../go/ini.go:1033`).

The first link is the one that matters. The pair rule reads its child
node from the rule it PUSHED, which is the first link, not the link that
popped. The engine has done so since parser `a801621` ("keep the
parent's child link on the rule it pushed"), which aligned Rust with
TypeScript and Go. Before it, the parent took the LAST link, so setting
only the current rule's node was enough; against the engine after it,
that left the first link's node unset, and the value came out `null`.

The write goes THROUGH the shared `Rc<RefCell<Value>>`
(`*link.node.borrow_mut() = ...`), not through `set_node`. The engine
freezes the first link's cell as the pair's child link when that link is
replaced, so only a write into that cell reaches the node the pair
reads. `set_node` swaps in a new `Rc` and leaves the frozen handle
holding the old value.

No link shares the enclosing map's cell, so the write cannot clobber the
map. The `val` before-open hook in `install_val_rule` gives every link,
each replacement included, a fresh cell before its alternates run:
`set_node(rule, Value::Undefined)`. The `a = .b\nc = 1` row of
`../test/spec/value-fixed-token-start.tsv` pins that the map keeps its
other keys.

## The depth guard is a crash fix

`ini` ends by installing a parse guard, named `depth`, that refuses
nesting past `DEPTH_LIMIT` (127) with the engine's `cancel` code,
counting `map`, `list` and `dive` rules alike. jsonic installs the
same check over `map` and `list` only, which leaves a section header,
the one place an INI document nests, unbounded. The engine parses iteratively, but displaying,
converting or dropping a `Value` walks the tree with the call stack, and
a header ten thousand segments deep aborted the process rather than
erroring. TypeScript and Go carry the same limit, at the same number
and over the same three rules, so the boundary is a shared fixture
(`../test/spec/sections-depth-limit.tsv`) rather than a divergence.

The limit is a guard, and not the parse budget it once was, because the
budget is one slot: a caller's `parse_budget` replaced it in place, and
the limit went with it. The guard holds whatever budget the caller sets.
TypeScript and Go keep theirs in the budget, since their engines have no
guards, so there a caller's own budget still replaces the limit. The
default boundary, the one the shared fixture pins, is the same in all
three; the difference shows only when a caller sets a budget. It is a
divergence, measured in `../DIVERGENCE.md` ("The depth limit under a
caller's parse budget") and pinned in all three suites.

## A divergent corpus document is compared against TypeScript

Twelve of the thirty valid corpus documents read differently from the
npm/ini oracle, each for a documented dialect reason, so the oracle
cannot say whether this port read them correctly. Asserting only that
they DIFFER from the oracle leaves any third value green, which is 40% of
the valid corpus parsed and not measured.
`../test/corpus/ini-canonical.json` holds what `ts/src/ini.ts` produces
for each one, generated by `../scripts/build-ini-canonical.js` and
committed because neither this suite nor the Go one can run node, and
`valid_documents_match_the_oracle` compares against that as well. The
key set is asserted to be exactly the divergence list, so neither can be
changed without the other.

All three halves read that one file, so what the canonical means by
those twelve documents is decided once.

## Reading a single-quoted value as JSON

`@val-ac` hands a single-quoted value to `serde_json` where the canonical
port hands it to `JSON.parse`. The two agree except on a number literal
too large for a double: `JSON.parse` rounds it to an infinity,
`serde_json` refuses it with `number out of range`, and `serde_json`'s
own `Number` cannot hold a non-finite value at all.

Where the WHOLE value is that literal, `overflowed_json_number` checks
the JSON number grammar by hand and parses it with Rust's float parser,
which rounds to an infinity as the specification does. Where the literal
is inside an array or an object it cannot be recovered, and that is the
recorded divergence in `../DIVERGENCE.md`. Do not replace the grammar
check with "Rust parsed it as a float": Rust's parser also takes `inf`,
`NaN`, `+1`, `1.` and `.5`, none of which `JSON.parse` accepts.

## `String()` of a parsed value is not its JSON

`js_string` is the JavaScript `String()`, used by the fixed-token
concatenation in `@val-ac`. Rendering a composite as JSON there is wrong
in both directions: an array coerces by `join(',')` and flattens, so
`[1,[2,3]]` is `1,2,3` and `[]` is empty, a null or undefined ELEMENT
contributes nothing, and an object is `[object Object]`. Numbers inside
go through `js_number_to_string`, the ECMA-262 formatter, and never
Rust's. This is the array-join defect class the csv port met; the repair
is the same shape there.

## The error alternate with no tokens

`table.open`'s duplicate-section alternate matches zero tokens, and the
engine fills its lookahead buffer to the length the alternate under test
asks for, so `context.t0()` is empty exactly where TypeScript and Go have
a positionless token in it. `alt_err_token` therefore ends in a fresh
NOTOKEN sentinel, which is the Go port's last fallback. The rendered
diagnostic is byte for byte the one the other two runtimes render, caret
and all; a fallback to the last consumed token would have pointed
somewhere else.

## The grammar is embedded, and embedded as TEXT

`../ini-grammar.jsonic` is the source of truth for every runtime, and
`ts/embed-grammar.js` copies it verbatim into `ts/src/ini.ts`,
`go/ini.go` and `src/lib.rs`. All three parse that text with their own
jsonic at load time: one grammar, three readers. Never hand-edit between
the `BEGIN/END EMBEDDED` markers; run `npm run embed` from `ts/`.
`grammar_test.rs` compares the embedded block against the file and
against the other two embeds.

Two adjustments are made to the parsed document before it is installed,
and both mirror what the other ports do in code: the `QUOTE_CHARS`
placeholder becomes the real quote characters, and the three `check`
hooks the canonical port installs through `options.config.modify`
callbacks are named as ordinary serialized `check` references. A third is
this port's alone: jsonic parses every number as a double, so `b: 1`
arrives as `1.0`, which the engine's integer fields refuse. `integralize`
rounds whole doubles back before the document is installed.

## The docs are gated

`README.md` is in the published set: no em dashes in prose, no first
person singular, no links to any `AGENTS.md`, no project history. This
file is internal and may be blunt.

## The README is doctested

`src/lib.rs` includes `README.md` as rustdoc under `#[cfg(doctest)]`, so
every `rust` fence in it runs on `cargo test --doc`. rustdoc runs each
fence as written, so a fence must be a complete program: wrap it in
`fn main() -> Result<(), Box<dyn std::error::Error>> { ... Ok(()) }`
rather than using `?` at the top level, and never use hidden `# ` lines,
which render as garbage on GitHub.