Skip to main content

unflatten_value

Function unflatten_value 

Source
pub fn unflatten_value(
    nodes: &[ValueNode],
    root: u32,
) -> Result<Value, ShapeError>
Expand description

Rebuild a Value from an arena — the read direction, for a guest turning a get-option-value answer back into a tree before from_value.

Range- and cycle-checked, for the host’s reasons in reverse: the arena a guest receives is well-formed by construction today, but a guest that assumed so and recursed would be one host bug away from an unbounded walk in wasm, where the failure is a trap the user sees as the plugin crashing.

§Errors

A ShapeError (with an empty path) when any reachable index is out of range or a node is reached again from its own descendants. A node shared by two siblings is not a cycle and is accepted.

§Examples

use lattice_plugin_sdk::shape::{Value, ValueNode, unflatten_value};

let nodes = [ValueNode::Int(7), ValueNode::List(vec![0, 0])];
assert_eq!(
    unflatten_value(&nodes, 1),
    Ok(Value::List(vec![Value::Int(7), Value::Int(7)])),
);

// A self-referencing list is refused rather than walked forever.
assert!(unflatten_value(&[ValueNode::List(vec![0])], 0).is_err());