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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// =============================================================================
// Copyright (c) 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Builds values whose partial trees retain protected cleanup.
use serde_json::Value;
use super::super::SafeJsonValue;
/// Owns a partially built tree and drops it with the owner's iterative cleanup.
pub(crate) struct SafeValueBuilder {
/// `None` only after `finish` transfers the completed tree.
value: Option<Value>,
}
impl SafeValueBuilder {
/// Starts an empty array builder.
pub(crate) fn array() -> Self {
Self {
value: Some(Value::Array(Vec::new())),
}
}
/// Starts an empty array builder with a caller-bounded allocation hint.
///
/// # Parameters
///
/// * `capacity` - Initial allocation capacity for array elements.
pub(crate) fn array_with_capacity(capacity: usize) -> Self {
Self {
value: Some(Value::Array(Vec::with_capacity(capacity))),
}
}
/// Starts an empty object builder.
pub(crate) fn object() -> Self {
Self {
value: Some(Value::Object(serde_json::Map::new())),
}
}
/// Appends a child owner while keeping an unwind-safe placeholder in place.
///
/// # Parameters
///
/// * `child` - Owned JSON subtree to append to this array.
pub(crate) fn push(&mut self, child: SafeJsonValue) {
let Value::Array(values) = self.value.as_mut().expect("builder is active") else {
unreachable!()
};
values.push(Value::Null);
*values.last_mut().expect("just inserted a slot") = child.into_value();
super::super::safe_json_value::maybe_panic_after_guarded_insert();
}
/// Inserts a child owner while keeping an unwind-safe placeholder in place.
///
/// # Parameters
///
/// * `key` - Object key associated with the child subtree.
/// * `child` - Owned JSON subtree to insert at that key.
pub(crate) fn insert(&mut self, key: String, child: SafeJsonValue) {
let Value::Object(values) = self.value.as_mut().expect("builder is active") else {
unreachable!()
};
let slot = values.entry(key).or_insert(Value::Null);
*slot = child.into_value();
super::super::safe_json_value::maybe_panic_after_guarded_insert();
}
/// Inserts or replaces a member and iteratively drops any replaced tree.
///
/// # Parameters
///
/// * `key` - Object key associated with the child subtree.
/// * `child` - Owned JSON subtree to insert at that key.
pub(crate) fn insert_last_wins(&mut self, key: String, child: SafeJsonValue) {
let Value::Object(values) = self.value.as_mut().expect("builder is active") else {
unreachable!()
};
let slot = values.entry(key).or_insert(Value::Null);
let old = std::mem::replace(slot, Value::Null);
let old = SafeJsonValue::new(old);
*slot = child.into_value();
drop(old);
super::super::safe_json_value::maybe_panic_after_guarded_insert();
}
/// Transfers the completed tree out of the guarded builder.
pub(crate) fn finish(mut self) -> Value {
self.value.take().expect("builder is active")
}
}
impl Drop for SafeValueBuilder {
/// Iteratively destroys any unfinished tree to avoid recursive cleanup.
fn drop(&mut self) {
if let Some(mut value) = self.value.take() {
super::super::safe_json_value::drop_tree(&mut value);
}
}
}