Skip to main content

Module document

Module document 

Source
Expand description

The Document model — a canonical tree of ordered, labeled edges.

Ported from ~/dev/omnist/omnist/document.py (see issue #4). A Document node is either a leaf holding a Scalar, or an internal node holding an ordered list of edges, each a (label, child) pair. Labels may repeat — “many members” is the label member appearing several times, not a field pointing to an array.

§Architecture (per issue #1, “architecture freedom”)

This port uses an arena: nodes live in a Vec<Entry> inside Doc, referenced by NodeId (an index newtype), not Rc<RefCell<_>>. Two consequences that don’t mirror Python/TypeScript 1:1:

  • No cycle detection. Python/TS guard against cycles because a plain dict/object can be made self-referential through shared mutable references. Value (this port’s “plain value” input type, analogous to a parsed JSON value) is a plain owned tree — building a self-referential Value without unsafe or Rc<RefCell<_>> isn’t possible, so the whole bug class is closed by the type system rather than checked at runtime (see the workflow playbook’s “what NOT to carry over unexamined”).
  • Integer-digit security cap. Python’s _check_int_digits defends against str()-converting an arbitrary-precision int with thousands of digits (a superlinear operation). Since issue #104, Scalar::Int is backed by arbitrary-precision num_bigint::BigInt, and format decoders (JSON, YAML, TOML) enforce the security cap (crate::limits::MAX_INT_DIGITS, 4300 digits) during lexing/parsing before BigInt construction.

Observable behavior for everything else (construction, depth guard, edge ordering, mutation semantics) matches the Python spec.

Structs§

Cursor
A read-only cursor into a Doc’s tree, tracking its own path.
Doc
A guarded handle on a Document tree: an arena of nodes plus the root.
NodeId
An index into a Doc’s arena. Opaque outside this module’s crate.

Enums§

RawNode
The raw canonical Document node: either a leaf scalar, or an ordered list of (label, child) edges that may repeat and interleave a label arbitrarily ([("b",1),("c",2),("b",3)] is representable exactly).
Scalar
A leaf value.
Value
A plain input value (analogous to a parsed JSON/YAML/TOML value), the thing Doc::of/Doc::add/Doc::set turn into canonical nodes.

Constants§

MAX_DEPTH
Maximum nesting depth for a Document node (matches Python’s _MAX_DEPTH).
MAX_NODES
Maximum total node count for a single Document (matches the reference default in omnist-spec docs/02-document-model.md Sec2.4: a depth limit alone doesn’t bound a shallow-but-enormous document, e.g. a million sibling edges at depth 1). Enforced once, in push, the single arena choke point every construction path (build_node, push_raw) funnels through – see omnist-rs#78: previously the only node-count guard in this crate was scoped narrowly to formats::yaml’s anchor/alias amplification defense, leaving every other construction path unbounded.