Skip to main content

Module dump

Module dump 

Source
Expand description

Typed AST → ParseTree reverse walk (DslDump).

DslBuild turns a validated ParseTree into a typed AST value; this module is its inverse. A type deriving #[derive(DslDump)] (in dsl-kit-macros) re-emits the tree shape its own DslBuild derive accepts, so the two derives stay a round-trip pair by construction:

from_parse_tree(&ast.to_parse_tree()?, &ids)  ≙  ast   (modulo NodeId)

Chaining the emitted tree through to_canonical_json yields the serde-bridge JSON serializer downstream consumers want — see dump_canonical_json. Because to_canonical_json runs check_conformance on the way down, a derive bug that emitted a non-conforming shape surfaces as loud diagnostics rather than silently wrong JSON.

§Emission rules (the duals of the build defaults)

  • id: NodeId is not serialized — re-parsing mints fresh ids, which is the same contract every front-end already has. The id is only used to look up the node’s $allow annotation in the caller-supplied AllowTable.
  • An absent Option<T> payload / child omits its key entirely (never null), matching the canonical-JSON spelling and build_field_optional’s absent → None rule.
  • Vec<T> payloads always emit, including as [] when empty — one unambiguous spelling. (build_field_vec accepts the omitted spelling too, so both round-trip; the emit side just never produces it.)
  • Vec<Self> child slots always emit the slot, including empty. A slot declared non_empty fails conformance downstream in that case, which is the declared-constraint violation surfacing — not something to paper over here.
  • Keyed slots (BTreeMap) emit in map iteration order, which is already ascending by key — the sortedness check_conformance demands. Empty maps omit the slot (the PEG front-end’s “nothing to record” spelling; both spellings are accepted everywhere it matters).
  • Spans are None: a synthesized tree has no source text.

One known asymmetry, inherited rather than introduced: nested Option<Option<T>> payloads cannot distinguish Some(None) from None in JSON (both spell as an absent key / null), exactly as on the build side.

Traits§

DslDump
Contract for re-emitting a typed AST value as the ParseTree its DslBuild impl accepts.

Functions§

dump_canonical_json
Serializes a typed AST straight to canonical serde-bridge JSON: DslDump::to_parse_tree chained through to_canonical_json against the type’s own derived schema.
dump_canonical_json_with
dump_canonical_json with an AllowTable so $allow annotations survive into the JSON (and therefore into content hashes computed over it).
dump_child_many
Emits a Multiplicity::Many child slot — always, including as an empty slot, so the emitted spelling is unambiguous. Dual of build_child_many.
dump_child_map
Emits a Multiplicity::Map child slot from (key, node) entries, omitting the slot when empty. Dual of build_child_map.
dump_child_one
Emits a Multiplicity::One child slot. Dual of build_child_one.
dump_child_optional
Emits a Multiplicity::Optional child slot, omitting the slot entirely when None. Dual of build_child_optional.
dump_field
Serializes one payload field into the tree as RawValue::Json.
dump_field_optional
Serializes an optional payload field, omitting the key when None.
dump_scalar_map
Emits a scalar-valued keyed slot (BTreeMap<String, V> where V is a payload type) as keyed entries each carrying a single value field — the exact shape the JSON front-end builds on ingest and build_scalar_map reads back. Omits the slot when the map is empty.