1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// =============================================================================
// Copyright (c) 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Defines callbacks for mutable JSON tree processing.
use Value;
use JsonTreeContext;
use JsonTreeControl;
/// Mutates JSON nodes after the complete input tree has passed admission.
///
/// Output admission runs only after every visitor callback succeeds. Returning
/// [`JsonTreeControl::SkipSubtree`] skips descendant callbacks but does not
/// skip final output accounting.
///
/// Callbacks can visit descendants added by earlier callbacks. Output
/// `max_nodes` and `max_depth` apply only to the complete final tree; they do
/// not bound callback count or intermediate expansion. A visitor that adds
/// descendants must enforce its own expansion bound and ensure traversal
/// terminates. `SkipSubtree` skips descent from the current node, but does not
/// exempt the resulting tree from
/// final output admission. See the
/// [mutable tree traversal guidance](https://github.com/qubit-ltd/rs-json/blob/main/doc/user_guide.md#read-and-mutate-existing-trees).
/// [`crate::value::traverse::JsonTreeMutator::process`] supplies the context to
/// each callback during traversal.
///
/// # Examples
///
/// ```
/// use qubit_budget::json::{JsonResource, JsonValueBudget, JsonValueLimits};
/// use qubit_json::value::traverse::{
/// JsonTreeContext, JsonTreeControl, JsonTreeMutator, JsonTreeMutVisitor,
/// };
/// use serde_json::Value;
///
/// struct Visitor;
/// impl JsonTreeMutVisitor for Visitor {
/// type Error = std::convert::Infallible;
///
/// fn visit(
/// &mut self,
/// value: &mut Value,
/// _: JsonTreeContext<'_>,
/// ) -> Result<JsonTreeControl, Self::Error> {
/// *value = Value::Null;
/// 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 visitor = Visitor;
/// let mut value = Value::Bool(true);
/// mutator.process(&mut value, &mut visitor).unwrap();
/// assert_eq!(value, Value::Null);
/// ```