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
use std::{
    borrow::{Borrow, Cow},
    cell::RefCell,
    collections::HashMap,
    hash::Hash,
    ops::DerefMut,
    rc::Rc,
    sync::{Arc, Mutex},
};

use crate::{Deserialize, Error, Intermediate};

/// Update trait.
///
/// The trait can be used for objects that can be updated from the intermediate
/// representation.
pub trait Update: Deserialize {
    /// Update the object.
    fn update(&mut self, val: &Intermediate) -> Result<(), Error>;
}

macro_rules! update_by_replace {
    ( $x:ty ) => {
        impl Update for $x {
            #[inline]
            fn update(&mut self, val: &Intermediate) -> Result<(), Error> {
                *self = <$x as Deserialize>::deserialize(val)?;

                Ok(())
            }
        }
    };
}

update_by_replace!(bool);

update_by_replace!(i8);
update_by_replace!(i16);
update_by_replace!(i32);
update_by_replace!(i64);
update_by_replace!(i128);
update_by_replace!(isize);

update_by_replace!(u8);
update_by_replace!(u16);
update_by_replace!(u32);
update_by_replace!(u64);
update_by_replace!(u128);
update_by_replace!(usize);

update_by_replace!(f32);
update_by_replace!(f64);

update_by_replace!(char);

update_by_replace!(String);

impl<T> Update for Option<T>
where
    T: Update,
{
    #[inline]
    fn update(&mut self, val: &Intermediate) -> Result<(), Error> {
        if val.is_none() {
            *self = None;
        } else if let Some(inner) = self {
            T::update(inner, val)?;
        } else {
            *self = T::deserialize(val).map(Some)?;
        }

        Ok(())
    }
}

impl<T> Update for Vec<T>
where
    T: Update,
{
    fn update(&mut self, val: &Intermediate) -> Result<(), Error> {
        if let Some(val) = val.as_array() {
            match val.len() {
                len if self.len() > len => self.truncate(len),
                len if self.len() < len => self.reserve(len - self.len()),
                _ => (),
            }

            for (index, elem) in val.iter().enumerate() {
                if let Some(current) = self.get_mut(index) {
                    current.update(elem)?;
                } else {
                    self.push(T::deserialize(elem)?);
                }
            }

            Ok(())
        } else {
            Err(Error::invalid_value_static("array"))
        }
    }
}

impl<T> Update for [T; 0] {
    #[inline]
    fn update(&mut self, _: &Intermediate) -> Result<(), Error> {
        Ok(())
    }
}

macro_rules! update_array {
    ( $len:expr => ($($n:tt)+) ) => {
        impl<T> Update for [T; $len]
        where
            T: Update,
        {
            fn update(&mut self, val: &Intermediate) -> Result<(), Error> {
                if let Some(val) = val.as_array() {
                    if val.len() < $len {
                        return Err(Error::invalid_value_static(concat!(
                            "an array of length ",
                            $len
                        )));
                    }

                    for (index, elem) in val.iter().enumerate() {
                        self[index].update(elem)?;
                    }

                    Ok(())
                } else {
                    Err(Error::invalid_value_static(concat!(
                        "an array of length ",
                        $len
                    )))
                }
            }
        }
    };
}

update_array!(1 => (0));
update_array!(2 => (0 1));
update_array!(3 => (0 1 2));
update_array!(4 => (0 1 2 3));
update_array!(5 => (0 1 2 3 4));
update_array!(6 => (0 1 2 3 4 5));
update_array!(7 => (0 1 2 3 4 5 6));
update_array!(8 => (0 1 2 3 4 5 6 7));
update_array!(9 => (0 1 2 3 4 5 6 7 8));
update_array!(10 => (0 1 2 3 4 5 6 7 8 9));
update_array!(11 => (0 1 2 3 4 5 6 7 8 9 10));
update_array!(12 => (0 1 2 3 4 5 6 7 8 9 10 11));
update_array!(13 => (0 1 2 3 4 5 6 7 8 9 10 11 12));
update_array!(14 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13));
update_array!(15 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14));
update_array!(16 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15));
update_array!(17 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16));
update_array!(18 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17));
update_array!(19 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18));
update_array!(20 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19));
update_array!(21 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20));
update_array!(22 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21));
update_array!(23 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22));
update_array!(24 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23));
update_array!(25 => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24));
update_array!(26 => (0 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));
update_array!(27 => (0 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));
update_array!(28 => (0 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));
update_array!(29 => (0 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));
update_array!(30 => (0 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));
update_array!(31 => (0 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));
update_array!(32 => (0 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));

impl Update for () {
    #[inline]
    fn update(&mut self, _: &Intermediate) -> Result<(), Error> {
        Ok(())
    }
}

macro_rules! update_tuple {
    ( $len:expr => ($($n:tt $ty:ident)+) ) => {
        impl<$($ty),+> Update for ($($ty,)+)
        where
            $($ty: Update,)+
        {
            fn update(&mut self, val: &Intermediate) -> Result<(), Error> {
                if let Some(val) = val.as_array() {
                    if val.len() < $len {
                        return Err(Error::invalid_value_static(concat!("an array of length ", $len)));
                    }

                    $(
                        self.$n.update(&val[$n])?;
                    )+

                    Ok(())
                } else {
                    Err(Error::invalid_value_static(concat!("an array of length ", $len)))
                }
            }
        }
    };
}

update_tuple!(1 => (0 T0));
update_tuple!(2 => (0 T0 1 T1));
update_tuple!(3 => (0 T0 1 T1 2 T2));
update_tuple!(4 => (0 T0 1 T1 2 T2 3 T3));
update_tuple!(5 => (0 T0 1 T1 2 T2 3 T3 4 T4));
update_tuple!(6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5));
update_tuple!(7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6));
update_tuple!(8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7));
update_tuple!(9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8));
update_tuple!(10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9));
update_tuple!(11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10));
update_tuple!(12 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11));
update_tuple!(13 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12));
update_tuple!(14 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13));
update_tuple!(15 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14));
update_tuple!(16 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15));

impl<K, V> Update for HashMap<K, V>
where
    K: From<Cow<'static, str>> + Borrow<str> + Eq + Hash,
    V: Update,
{
    fn update(&mut self, val: &Intermediate) -> Result<(), Error> {
        let val = val
            .as_map()
            .ok_or_else(|| Error::invalid_value_static("map"))?;

        for (name, value) in val {
            if let Some(inner) = self.get_mut(name) {
                V::update(inner, value)?;
            } else {
                let k = K::from(name.clone());
                let v = V::deserialize(value)?;

                self.insert(k, v);
            }
        }

        Ok(())
    }
}

#[cfg(feature = "preserve-order")]
impl<K, V> Update for indexmap::IndexMap<K, V>
where
    K: From<Cow<'static, str>> + Borrow<str> + Eq + Hash,
    V: Update,
{
    fn update(&mut self, val: &Intermediate) -> Result<(), Error> {
        let val = val
            .as_map()
            .ok_or_else(|| Error::invalid_value_static("map"))?;

        for (name, value) in val {
            if let Some(inner) = self.get_mut(name as &str) {
                V::update(inner, value)?;
            } else {
                let k = K::from(name.clone());
                let v = V::deserialize(value)?;

                self.insert(k, v);
            }
        }

        Ok(())
    }
}

impl<T> Update for Box<T>
where
    T: Update,
{
    #[inline]
    fn update(&mut self, val: &Intermediate) -> Result<(), Error> {
        self.deref_mut().update(val)
    }
}

impl<T> Update for Mutex<T>
where
    T: Update,
{
    #[inline]
    fn update(&mut self, val: &Intermediate) -> Result<(), Error> {
        self.get_mut().unwrap().update(val)
    }
}

impl<T> Update for Arc<Mutex<T>>
where
    T: Update,
{
    #[inline]
    fn update(&mut self, val: &Intermediate) -> Result<(), Error> {
        self.lock().unwrap().update(val)
    }
}

impl<T> Update for RefCell<T>
where
    T: Update,
{
    #[inline]
    fn update(&mut self, val: &Intermediate) -> Result<(), Error> {
        self.borrow_mut().update(val)
    }
}

impl<T> Update for Rc<RefCell<T>>
where
    T: Update,
{
    #[inline]
    fn update(&mut self, val: &Intermediate) -> Result<(), Error> {
        self.borrow_mut().update(val)
    }
}