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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
use std::{
    borrow::Cow,
    cell::RefCell,
    collections::HashMap,
    convert::TryFrom,
    rc::Rc,
    sync::{Arc, Mutex},
};

use crate::{Error, Intermediate, Map, Number};

/// Serialize trait.
///
/// The trait can be implemented by objects the can serialized to the
/// intermediate representation.
pub trait Serialize {
    /// Serialize the object.
    fn serialize(&self) -> Result<Intermediate, Error>;
}

impl Serialize for bool {
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        Ok(Intermediate::Bool(*self))
    }
}

impl Serialize for i64 {
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        Ok(Intermediate::Number(Number::SignedInt(*self)))
    }
}

impl Serialize for u64 {
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        Ok(Intermediate::Number(Number::UnsignedInt(*self)))
    }
}

impl Serialize for f32 {
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        Ok(Intermediate::Number(Number::Float(*self as _)))
    }
}

impl Serialize for f64 {
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        Ok(Intermediate::Number(Number::Float(*self)))
    }
}

macro_rules! serialize_for_signed_int {
    ( $x:ty ) => {
        impl Serialize for $x {
            #[inline]
            fn serialize(&self) -> Result<Intermediate, Error> {
                Ok(Intermediate::Number(Number::SignedInt(i64::from(*self))))
            }
        }
    };
}

macro_rules! serialize_for_unsigned_int {
    ( $x:ty ) => {
        impl Serialize for $x {
            #[inline]
            fn serialize(&self) -> Result<Intermediate, Error> {
                Ok(Intermediate::Number(Number::UnsignedInt(u64::from(*self))))
            }
        }
    };
}

serialize_for_signed_int!(i8);
serialize_for_signed_int!(i16);
serialize_for_signed_int!(i32);

serialize_for_unsigned_int!(u8);
serialize_for_unsigned_int!(u16);
serialize_for_unsigned_int!(u32);

impl Serialize for i128 {
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        i64::try_from(*self)
            .map(|v| Intermediate::Number(Number::SignedInt(v)))
            .map_err(|_| Error::OutOfBounds)
    }
}

impl Serialize for u128 {
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        u64::try_from(*self)
            .map(|v| Intermediate::Number(Number::UnsignedInt(v)))
            .map_err(|_| Error::OutOfBounds)
    }
}

impl Serialize for isize {
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        i64::try_from(*self)
            .map(|v| Intermediate::Number(Number::SignedInt(v)))
            .map_err(|_| Error::OutOfBounds)
    }
}

impl Serialize for usize {
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        u64::try_from(*self)
            .map(|v| Intermediate::Number(Number::UnsignedInt(v)))
            .map_err(|_| Error::OutOfBounds)
    }
}

impl Serialize for char {
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        Ok(Intermediate::String(Cow::Owned(self.to_string())))
    }
}

impl Serialize for String {
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        Ok(Intermediate::String(Cow::Owned(self.clone())))
    }
}

impl<'a> Serialize for &'a str {
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        Ok(Intermediate::String(Cow::Owned(String::from(*self))))
    }
}

impl<T> Serialize for Option<T>
where
    T: Serialize,
{
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        if let Some(inner) = self.as_ref() {
            inner.serialize()
        } else {
            Ok(Intermediate::None)
        }
    }
}

impl<'a, T> Serialize for &'a [T]
where
    T: Serialize,
{
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        serialize_slice(self)
    }
}

impl<'a, T> Serialize for &'a mut [T]
where
    T: Serialize,
{
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        serialize_slice(self)
    }
}

impl<T> Serialize for Vec<T>
where
    T: Serialize,
{
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        serialize_slice(self)
    }
}

impl<T> Serialize for [T; 0] {
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        Ok(Intermediate::Array(Vec::new()))
    }
}

macro_rules! serialize_array {
    ( $len:expr ) => {
        impl<T> Serialize for [T; $len]
        where
            T: Serialize,
        {
            #[inline]
            fn serialize(&self) -> Result<Intermediate, Error> {
                serialize_slice(&self[..])
            }
        }
    };
}

serialize_array!(1);
serialize_array!(2);
serialize_array!(3);
serialize_array!(4);
serialize_array!(5);
serialize_array!(6);
serialize_array!(7);
serialize_array!(8);
serialize_array!(9);
serialize_array!(10);
serialize_array!(11);
serialize_array!(12);
serialize_array!(13);
serialize_array!(14);
serialize_array!(15);
serialize_array!(16);
serialize_array!(17);
serialize_array!(18);
serialize_array!(19);
serialize_array!(20);
serialize_array!(21);
serialize_array!(22);
serialize_array!(23);
serialize_array!(24);
serialize_array!(25);
serialize_array!(26);
serialize_array!(27);
serialize_array!(28);
serialize_array!(29);
serialize_array!(30);
serialize_array!(31);
serialize_array!(32);

impl Serialize for () {
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        Ok(Intermediate::Array(Vec::new()))
    }
}

macro_rules! serialize_tuple {
    ( $len:expr => ($($n:tt $ty:ident)+) ) => {
        impl<$($ty),+> Serialize for ($($ty,)+)
        where
            $($ty: Serialize,)+
        {
            fn serialize(&self) -> Result<Intermediate, Error> {
                let res = vec![
                    $(
                        self.$n.serialize()?,
                    )+
                ];

                Ok(Intermediate::Array(res))
            }
        }
    };
}

serialize_tuple!(1 => (0 T0));
serialize_tuple!(2 => (0 T0 1 T1));
serialize_tuple!(3 => (0 T0 1 T1 2 T2));
serialize_tuple!(4 => (0 T0 1 T1 2 T2 3 T3));
serialize_tuple!(5 => (0 T0 1 T1 2 T2 3 T3 4 T4));
serialize_tuple!(6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5));
serialize_tuple!(7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6));
serialize_tuple!(8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7));
serialize_tuple!(9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8));
serialize_tuple!(10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9));
serialize_tuple!(11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10));
serialize_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));
serialize_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));
serialize_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));
serialize_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));
serialize_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> Serialize for HashMap<K, V>
where
    K: ToString,
    V: Serialize,
{
    fn serialize(&self) -> Result<Intermediate, Error> {
        let mut res = Map::with_capacity(self.len());

        for (k, v) in self.iter() {
            res.insert_with_owned_key(k.to_string(), v.serialize()?);
        }

        Ok(Intermediate::Map(res))
    }
}

#[cfg(feature = "preserve-order")]
impl<K, V> Serialize for indexmap::IndexMap<K, V>
where
    K: ToString,
    V: Serialize,
{
    fn serialize(&self) -> Result<Intermediate, Error> {
        let mut res = Map::with_capacity(self.len());

        for (k, v) in self.iter() {
            res.insert_with_owned_key(k.to_string(), v.serialize()?);
        }

        Ok(Intermediate::Map(res))
    }
}

impl<'a, T> Serialize for &'a T
where
    T: Serialize + ?Sized,
{
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        <T as Serialize>::serialize(self)
    }
}

impl<'a, T> Serialize for &'a mut T
where
    T: Serialize + ?Sized,
{
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        <T as Serialize>::serialize(self)
    }
}

macro_rules! serialize_wrapper {
    ( $x:ident ) => {
        impl<T> Serialize for $x<T>
        where
            T: Serialize + ?Sized,
        {
            #[inline]
            fn serialize(&self) -> Result<Intermediate, Error> {
                <T as Serialize>::serialize(&*self)
            }
        }
    };
}

serialize_wrapper!(Box);
serialize_wrapper!(Rc);
serialize_wrapper!(Arc);

impl<T> Serialize for Mutex<T>
where
    T: Serialize + ?Sized,
{
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        self.lock().unwrap().serialize()
    }
}

impl<T> Serialize for RefCell<T>
where
    T: Serialize + ?Sized,
{
    #[inline]
    fn serialize(&self) -> Result<Intermediate, Error> {
        self.borrow().serialize()
    }
}

/// Helper function.
fn serialize_slice<T>(v: &[T]) -> Result<Intermediate, Error>
where
    T: Serialize,
{
    let mut res = Vec::with_capacity(v.len());

    for elem in v.iter() {
        res.push(elem.serialize()?);
    }

    Ok(Intermediate::Array(res))
}