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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use super::Key;
use crate::traitdef::{Mutable, Value};
use rustc_serialize::json::{Json as RustcJson, Object};
use std::{collections::btree_map::Entry::*, mem};
impl Value for RustcJson {
type Key = Key;
type Item = RustcJson;
fn items<'a>(&'a self) -> Option<Box<dyn Iterator<Item = (Self::Key, &'a Self::Item)> + 'a>> {
match *self {
RustcJson::String(_)
| RustcJson::U64(_)
| RustcJson::I64(_)
| RustcJson::F64(_)
| RustcJson::Boolean(_)
| RustcJson::Null => None,
RustcJson::Array(ref inner) => Some(Box::new(
inner.iter().enumerate().map(|(i, v)| (Key::Index(i), v)),
)),
RustcJson::Object(ref inner) => Some(Box::new(
inner.iter().map(|(s, v)| (Key::String(s.clone()), v)),
)),
}
}
}
impl Mutable for RustcJson {
type Key = Key;
type Item = RustcJson;
fn set(&mut self, keys: &[Self::Key], v: &Self::Item) {
if keys.is_empty() {
*self = v.clone();
} else {
let mut c = self;
let last_key_index = keys.len() - 1;
let object_or_value = |index| {
if index == last_key_index {
v.clone()
} else {
RustcJson::Object(Object::new())
}
};
fn runup_array_or_value<'a>(
array: &'a mut Vec<RustcJson>,
target_index: usize,
key_index: usize,
last_key_index: usize,
v: &RustcJson,
) -> &'a mut RustcJson {
for _ in array.len()..target_index {
array.push(RustcJson::Null);
}
let value = if key_index == last_key_index {
v.clone()
} else {
RustcJson::Null
};
if target_index == array.len() {
array.push(value);
} else {
array[target_index] = value;
}
&mut array[target_index]
};
for (i, k) in keys.iter().enumerate() {
c = match *k {
Key::String(ref k) => match { c } {
&mut RustcJson::Object(ref mut obj) => match obj.entry(k.clone()) {
Vacant(e) => e.insert(object_or_value(i)),
Occupied(e) => {
if i == last_key_index {
*e.into_mut() = v.clone();
return;
} else {
e.into_mut()
}
}
},
c @ &mut RustcJson::String(_)
| c @ &mut RustcJson::F64(_)
| c @ &mut RustcJson::Boolean(_)
| c @ &mut RustcJson::Null
| c @ &mut RustcJson::U64(_)
| c @ &mut RustcJson::Array(_)
| c @ &mut RustcJson::I64(_) => {
drop(mem::replace(
c,
RustcJson::Object({
let mut o = Object::new();
o.insert(k.clone(), object_or_value(i));
o
}),
));
if i == last_key_index {
return;
}
match c {
RustcJson::Object(ref mut obj) => {
obj.get_mut(k).expect("previous insertion")
}
_ => unreachable!(),
}
}
},
Key::Index(idx) => match { c } {
&mut RustcJson::Array(ref mut a) => {
runup_array_or_value(a, idx, i, last_key_index, v)
}
c @ &mut RustcJson::String(_)
| c @ &mut RustcJson::F64(_)
| c @ &mut RustcJson::Boolean(_)
| c @ &mut RustcJson::Null
| c @ &mut RustcJson::U64(_)
| c @ &mut RustcJson::Object(_)
| c @ &mut RustcJson::I64(_) => {
let mut a = Vec::new();
runup_array_or_value(&mut a, idx, i, last_key_index, v);
drop(mem::replace(c, RustcJson::Array(a)));
if i == last_key_index {
return;
}
match c {
RustcJson::Array(ref mut a) => {
a.get_mut(idx).expect("previous insertion")
}
_ => unreachable!(),
}
}
},
}
}
}
}
fn remove(&mut self, keys: &[Self::Key]) {
let mut c = self;
let last_key_index = keys.len().checked_sub(1).expect("at least one key");
for (i, k) in keys.iter().enumerate() {
c = match *k {
Key::String(ref k) => match { c } {
RustcJson::Object(ref mut obj) => {
if i == last_key_index {
obj.remove(k);
return;
} else {
match obj.get_mut(k) {
Some(json) => json,
None => return,
}
}
}
_ => return,
},
Key::Index(idx) => match { c } {
RustcJson::Array(ref mut a) => {
if i == last_key_index {
a.remove(idx);
return;
} else {
match a.get_mut(idx) {
Some(json) => json,
None => return,
}
}
}
_ => return,
},
}
}
}
}