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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#[cfg(test)]
mod tests;
use serde::de::DeserializeOwned;
use serde_intermediate::{from_intermediate, Change, Intermediate};
use std::{
collections::{HashMap, HashSet},
hash::Hash,
};
pub trait ReflectIntermediate {
fn patch_change(&mut self, _change: &Change) {}
}
macro_rules! impl_reflect {
(@atom $type:ty => $( $variant:ident ),+ ) => {
impl ReflectIntermediate for $type {
fn patch_change(&mut self, change: &Change) {
#[allow(clippy::collapsible_match)]
if let Change::Changed(v) = change {
match v {
$(
Intermediate::$variant(v) => if let Ok(v) = Self::try_from(*v) {
*self = v;
}
)+
_ => {}
}
}
}
}
};
}
impl ReflectIntermediate for () {}
impl_reflect! { @atom bool => Bool }
impl_reflect! { @atom i8 => Bool, I8, I16, I32, I64, I128, U8, U16, U32, U64, U128 }
impl_reflect! { @atom i16 => Bool, I8, I16, I32, I64, I128, U8, U16, U32, U64, U128 }
impl_reflect! { @atom i32 => Bool, I8, I16, I32, I64, I128, U8, U16, U32, U64, U128 }
impl_reflect! { @atom i64 => Bool, I8, I16, I32, I64, I128, U8, U16, U32, U64, U128 }
impl_reflect! { @atom i128 => Bool, I8, I16, I32, I64, I128, U8, U16, U32, U64, U128 }
impl_reflect! { @atom isize => Bool, I8, I16, I32, I64, I128, U8, U16, U32, U64, U128 }
impl_reflect! { @atom u8 => Bool, I8, I16, I32, I64, I128, U8, U16, U32, U64, U128, Char }
impl_reflect! { @atom u16 => Bool, I8, I16, I32, I64, I128, U8, U16, U32, U64, U128 }
impl_reflect! { @atom u32 => Bool, I8, I16, I32, I64, I128, U8, U16, U32, U64, U128, Char }
impl_reflect! { @atom u64 => Bool, I8, I16, I32, I64, I128, U8, U16, U32, U64, U128, Char }
impl_reflect! { @atom u128 => Bool, I8, I16, I32, I64, I128, U8, U16, U32, U64, U128, Char }
impl_reflect! { @atom usize => Bool, I8, I16, I32, I64, I128, U8, U16, U32, U64, U128 }
impl_reflect! { @atom f32 => I8, I16, U8, U16, F32 }
impl_reflect! { @atom f64 => I8, I16, I32, U8, U16, U32, F32, F64 }
impl_reflect! { @atom char => U8, U32, Char }
impl ReflectIntermediate for String {
fn patch_change(&mut self, change: &Change) {
if let Change::Changed(v) = change {
match v {
Intermediate::Char(v) => {
if let Ok(v) = Self::try_from(*v) {
*self = v;
}
}
Intermediate::String(v) => *self = v.to_owned(),
_ => {}
}
}
}
}
impl<T> ReflectIntermediate for Option<T>
where
T: ReflectIntermediate + DeserializeOwned,
{
fn patch_change(&mut self, change: &Change) {
match change {
Change::Changed(v) => {
if let Ok(v) = from_intermediate(v) {
*self = v;
}
}
Change::PartialChange(change) => {
if let Some(content) = self {
content.patch_change(change);
}
}
_ => {}
}
}
}
impl<T, E> ReflectIntermediate for Result<T, E>
where
T: ReflectIntermediate + DeserializeOwned,
E: ReflectIntermediate + DeserializeOwned,
{
fn patch_change(&mut self, change: &Change) {
match change {
Change::Changed(v) => {
if let Ok(v) = from_intermediate(v) {
*self = v;
}
}
Change::PartialChange(change) => match self {
Ok(content) => content.patch_change(change),
Err(content) => content.patch_change(change),
},
_ => {}
}
}
}
impl<T, const N: usize> ReflectIntermediate for [T; N]
where
T: ReflectIntermediate + DeserializeOwned,
{
fn patch_change(&mut self, change: &Change) {
match change {
Change::Changed(Intermediate::Seq(v)) => {
for (item, v) in self.iter_mut().zip(v.iter()) {
if let Ok(v) = from_intermediate(v) {
*item = v;
}
}
}
Change::PartialSeq(v) => {
for (index, change) in v {
if *index < N {
self[*index].patch_change(change);
}
}
}
_ => {}
}
}
}
impl<T> ReflectIntermediate for (T,)
where
T: ReflectIntermediate + DeserializeOwned,
{
fn patch_change(&mut self, change: &Change) {
match change {
Change::Changed(Intermediate::Seq(v)) => {
if let Some(v) = v.get(0) {
if let Ok(v) = from_intermediate(v) {
self.0 = v;
}
}
}
Change::PartialSeq(v) => {
for (index, change) in v {
if *index == 0 {
self.0.patch_change(change);
}
}
}
_ => {}
}
}
}
macro_rules! impl_tuple {
( $( $id:ident : $index:tt ),+ ) => {
impl< $( $id ),+ > ReflectIntermediate for ( $( $id ),+ )
where
$( $id: ReflectIntermediate + DeserializeOwned ),+
{
fn patch_change(&mut self, change: &Change) {
match change {
Change::Changed(Intermediate::Seq(v)) => {
$(
if let Some(v) = v.get($index) {
if let Ok(v) = from_intermediate(v) {
self.$index = v;
}
}
)+
}
Change::PartialSeq(v) => {
$(
if let Some((_,change)) = v.iter().find(|(i,_)| *i == $index) {
self.$index.patch_change(change);
}
)+
}
_ => {}
}
}
}
};
}
impl_tuple! { A:0, B:1 }
impl_tuple! { A:0, B:1, C:2 }
impl_tuple! { A:0, B:1, C:2, D:3 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17, S:18 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17, S:18, T:19 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17, S:18, T:19, U:20 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17, S:18, T:19, U:20, V:21 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17, S:18, T:19, U:20, V:21, X:22 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17, S:18, T:19, U:20, V:21, X:22, Y:23 }
impl_tuple! { A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17, S:18, T:19, U:20, V:21, X:22, Y:23, Z:24 }
impl<T> ReflectIntermediate for Vec<T>
where
T: ReflectIntermediate + DeserializeOwned,
{
fn patch_change(&mut self, change: &Change) {
match change {
Change::Changed(v) => {
if let Ok(v) = from_intermediate(v) {
*self = v;
}
}
Change::PartialSeq(v) => {
for (index, change) in v {
match change {
Change::Removed => {
self.remove(*index);
}
Change::Added(v) => {
if let Ok(v) = from_intermediate(v) {
self.insert(*index, v);
}
}
change => {
if let Some(item) = self.get_mut(*index) {
item.patch_change(change);
}
}
}
}
}
_ => {}
}
}
}
impl<T> ReflectIntermediate for HashSet<T>
where
T: ReflectIntermediate + DeserializeOwned + Hash + Eq + Clone,
{
fn patch_change(&mut self, change: &Change) {
match change {
Change::Changed(v) => {
if let Ok(v) = from_intermediate(v) {
*self = v;
}
}
Change::PartialSeq(v) => {
let mut data = self.iter().cloned().collect::<Vec<_>>();
for (index, change) in v {
match change {
Change::Removed => {
data.remove(*index);
}
Change::Added(v) => {
if let Ok(v) = from_intermediate(v) {
data.insert(*index, v);
}
}
change => {
if let Some(item) = data.get_mut(*index) {
item.patch_change(change);
}
}
}
}
*self = data.into_iter().collect();
}
_ => {}
}
}
}
impl<K, V> ReflectIntermediate for HashMap<K, V>
where
K: ReflectIntermediate + DeserializeOwned + Hash + Eq,
V: ReflectIntermediate + DeserializeOwned,
{
fn patch_change(&mut self, change: &Change) {
match change {
Change::Changed(v) => {
if let Ok(v) = from_intermediate(v) {
*self = v;
}
}
Change::PartialMap(v) => {
for (key, change) in v {
if let Ok(key) = from_intermediate(key) {
match change {
Change::Removed => {
self.remove(&key);
}
Change::Added(v) => {
if let Ok(v) = from_intermediate(v) {
self.insert(key, v);
}
}
change => {
if let Some(item) = self.get_mut(&key) {
item.patch_change(change);
}
}
}
}
}
}
_ => {}
}
}
}
impl<T> ReflectIntermediate for Box<T>
where
T: ReflectIntermediate + DeserializeOwned,
{
fn patch_change(&mut self, change: &Change) {
match change {
Change::Changed(v) => {
if let Ok(v) = from_intermediate(v) {
*self = v;
}
}
Change::PartialChange(change) => {
self.patch_change(change);
}
_ => {}
}
}
}