pub struct JsonTreeMutator<'input_transaction, 'input_budget, 'output_transaction, 'output_budget, R, Q>where
Q: ResourceQuantity,{ /* private fields */ }Expand description
Mutates a JSON tree between independent input and output transactions.
The complete original tree is admitted before the first visitor callback. After all callbacks succeed, the complete mutated tree is admitted against the output transaction. Visitor failures and panics can retain partial mutations, while output accounting does not begin until visitor success.
§Type Parameters
R- Resource identity shared by the two transactions.Q- Quantity representation shared by the two transactions.
§Examples
use qubit_budget::json::{JsonResource, JsonValueBudget, JsonValueLimits};
use qubit_json::value::traverse::{
JsonTreeContext, JsonTreeControl, JsonTreeMutVisitor, JsonTreeMutator,
};
use serde_json::Value;
struct Visitor;
impl JsonTreeMutVisitor for Visitor {
type Error = std::convert::Infallible;
fn visit(
&mut self,
_: &mut Value,
_: JsonTreeContext<'_>,
) -> Result<JsonTreeControl, Self::Error> {
Ok(JsonTreeControl::SkipSubtree)
}
}
let limits = JsonValueLimits::<JsonResource, usize>::default();
let mut input_budget = JsonValueBudget::new(limits);
let mut output_budget = JsonValueBudget::new(limits);
let mut input = input_budget.transaction();
let mut output = output_budget.transaction();
let mut mutator = JsonTreeMutator::new(&mut input, &mut output);
let mut value = Value::Null;
assert!(mutator.process(&mut value, &mut Visitor).is_ok());Implementations§
Source§impl<'input_transaction, 'input_budget, 'output_transaction, 'output_budget, R, Q> JsonTreeMutator<'input_transaction, 'input_budget, 'output_transaction, 'output_budget, R, Q>where
R: Clone,
Q: ResourceQuantity,
impl<'input_transaction, 'input_budget, 'output_transaction, 'output_budget, R, Q> JsonTreeMutator<'input_transaction, 'input_budget, 'output_transaction, 'output_budget, R, Q>where
R: Clone,
Q: ResourceQuantity,
Sourcepub fn new(
input: &'input_transaction mut JsonValueTransaction<'input_budget, R, Q>,
output: &'output_transaction mut JsonValueTransaction<'output_budget, R, Q>,
) -> Self
pub fn new( input: &'input_transaction mut JsonValueTransaction<'input_budget, R, Q>, output: &'output_transaction mut JsonValueTransaction<'output_budget, R, Q>, ) -> Self
Sourcepub fn process<V>(
&mut self,
root: &mut Value,
visitor: &mut V,
) -> Result<(), JsonTreeMutateError<R, Q, V::Error>>where
V: JsonTreeMutVisitor,
pub fn process<V>(
&mut self,
root: &mut Value,
visitor: &mut V,
) -> Result<(), JsonTreeMutateError<R, Q, V::Error>>where
V: JsonTreeMutVisitor,
Admits the original tree, mutates it, and admits the final tree.
Both admissions and all visitor callbacks use explicit stacks rather
than Rust recursion. SkipSubtree affects callbacks only; final output
admission always covers every resulting descendant.
§Type Parameters
V- Visitor controlling mutations and descendant callbacks.
§Parameters
root- Root JSON value to process and mutate.visitor- Visitor applied after complete input admission.
§Returns
Ok(()) after both complete trees fit and every callback succeeds.
§Errors
Returns JsonTreeMutateError::InputBudget before mutation when the
original tree is rejected, JsonTreeMutateError::Visitor after a
callback failure, or JsonTreeMutateError::OutputBudget after a
complete mutation whose result is rejected. Visitor and output failures
retain mutations already made to root.