Skip to main content

fold

Function fold 

Source
pub fn fold(root: &Value, options: &FoldOptions<'_>) -> Result<Value, FoldError>
Expand description

Fold a finished tree: walk it, reduce every key operator site bottom-up through options.evaluate, and return a NEW tree; the input is never changed, though what a resolver returns, site.node() among it, is placed in the output as it is.

Values flow up through evaluate; context flows down through inherit, worked out per path from each map’s own ancestors and kept. One result per location, so every site naming one target shares one folded value. Every container being folded is on an active stack, and a site whose target is on it closes a cycle, which the resolver is told about (or which fails the fold, under FoldCycle::Error). Dynamic key operators are reported and folded as plain maps.

A TOKEN operator is recognised only on a live expression node, so a fold that should reduce + as well as $ref runs inside crate::parse_scope, on the value the parse hands the closure. A realized array, the operator description followed by its terms, is plain data here and is never read as an operator: a document can spell exactly that array, and reading it would let untrusted input forge one.

max_depth bounds nesting and reference hops together. The walk over nested containers is iterative, so nesting costs no stack. Each reference hop re-enters the fold through the resolver, and the fold also fails with FOLD_DEPTH once the hops have used FoldOptions::max_stack bytes of stack, a megabyte by default, rather than let a thread with the default 2 MiB overflow, which would end the process.