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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
use crate::{
    de::intermediate::IntermediateVisitor, reflect::ReflectIntermediate, versioning::Change,
};
use serde::{
    ser::{
        SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple,
        SerializeTupleStruct, SerializeTupleVariant,
    },
    Deserialize, Deserializer, Serialize, Serializer,
};
use std::collections::{HashMap, HashSet};

/// Serde intermediate data representation.
///
/// # Example
/// ```rust
/// use std::time::SystemTime;
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Debug, PartialEq, Serialize, Deserialize)]
/// enum Login {
///     Email(String),
///     SocialMedia{
///         service: String,
///         token: String,
///         last_login: Option<SystemTime>,
///     }
/// }
///
/// #[derive(Debug, PartialEq, Serialize, Deserialize)]
/// struct Person {
///     // (first name, last name)
///     name: (String, String),
///     age: usize,
///     login: Login,
/// }
///
/// let data = Person {
///     name: ("John".to_owned(), "Smith".to_owned()),
///     age: 40,
///     login: Login::Email("john.smith@gmail.com".to_owned()),
/// };
/// let serialized = serde_intermediate::to_intermediate(&data).unwrap();
/// let deserialized = serde_intermediate::from_intermediate(&serialized).unwrap();
/// assert_eq!(data, deserialized);
/// ```
#[derive(Debug, Default, Clone, PartialEq, PartialOrd)]
pub enum Intermediate {
    /// Unit value: `()`.
    #[default]
    Unit,
    /// Bool value: `true`.
    Bool(bool),
    /// 8-bit signed integer value: `42`.
    I8(i8),
    /// 16-bit signed integer value: `42`.
    I16(i16),
    /// 32-bit signed integer value: `42`.
    I32(i32),
    /// 64-bit signed integer value: `42`.
    I64(i64),
    /// 128-bit signed integer value: `42`.
    I128(i128),
    /// 8-bit unsigned integer value: `42`.
    U8(u8),
    /// 16-bit unsigned integer value: `42`.
    U16(u16),
    /// 32-bit unsigned integer value: `42`.
    U32(u32),
    /// 64-bit unsigned integer value: `42`.
    U64(u64),
    /// 128-bit unsigned integer value: `42`.
    U128(u128),
    /// 32-bit floating point value: `3.14`.
    F32(f32),
    /// 64-bit floating point value: `3.14`.
    F64(f64),
    /// Single character value: `'@'`.
    Char(char),
    /// String value: `"Hello World!"`.
    String(String),
    /// Bytes buffer.
    Bytes(Vec<u8>),
    /// Option value: `Some(42)`.
    Option(
        /// Value.
        Option<Box<Self>>,
    ),
    /// Structure: `struct Foo;`.
    UnitStruct,
    /// Enum unit variant: `enum Foo { Bar }`.
    UnitVariant(
        /// Variant name.
        String,
    ),
    /// Newtype struct: `struct Foo(bool);`.
    NewTypeStruct(Box<Self>),
    /// Enum newtype variant: `enum Foo { Bar(bool) }`.
    NewTypeVariant(
        /// Variant name.
        String,
        /// Value.
        Box<Self>,
    ),
    /// Sequence/list: `Vec<usize>`, `[usize]`.
    Seq(
        /// Items.
        Vec<Self>,
    ),
    /// Tuple: `(bool, char)`.
    Tuple(
        /// Fields.
        Vec<Self>,
    ),
    /// Tuple struct: `struct Foo(bool, char)`.
    TupleStruct(
        /// Fields.
        Vec<Self>,
    ),
    /// Tuple variant: `enum Foo { Bar(bool, char) }`.
    TupleVariant(
        /// Variant name.
        String,
        /// Fields.
        Vec<Self>,
    ),
    /// Map: `HashMap<String, usize>`.
    Map(
        /// Entries: `(key, value)`.
        Vec<(Self, Self)>,
    ),
    /// Struct: `struct Foo { a: bool, b: char }`.
    Struct(
        /// Fields: `(name, value)`.
        Vec<(String, Self)>,
    ),
    /// Enum struct variant: `enum Foo { Bar { a: bool, b: char } }`.
    StructVariant(
        /// Variant name.
        String,
        /// Fields: `(name, value)`.
        Vec<(String, Self)>,
    ),
}

impl Eq for Intermediate {}

impl Intermediate {
    pub fn unit_struct() -> Self {
        Self::UnitStruct
    }

    pub fn unit_variant<T>(name: T) -> Self
    where
        T: ToString,
    {
        Self::UnitVariant(name.to_string())
    }

    pub fn newtype_struct(value: Self) -> Self {
        Self::NewTypeStruct(Box::new(value))
    }

    pub fn newtype_variant<T>(name: T, value: Self) -> Self
    where
        T: ToString,
    {
        Self::NewTypeVariant(name.to_string(), Box::new(value))
    }

    pub fn seq() -> Self {
        Self::Seq(vec![])
    }

    pub fn tuple() -> Self {
        Self::Tuple(vec![])
    }

    pub fn tuple_struct() -> Self {
        Self::TupleStruct(vec![])
    }

    pub fn tuple_variant<T>(name: T) -> Self
    where
        T: ToString,
    {
        Self::TupleVariant(name.to_string(), vec![])
    }

    pub fn map() -> Self {
        Self::Map(vec![])
    }

    pub fn struct_type() -> Self {
        Self::Struct(vec![])
    }

    pub fn struct_variant<T>(name: T) -> Self
    where
        T: ToString,
    {
        Self::StructVariant(name.to_string(), vec![])
    }

    pub fn item<T>(mut self, value: T) -> Self
    where
        T: Into<Self>,
    {
        match &mut self {
            Self::Seq(v) | Self::Tuple(v) | Self::TupleStruct(v) | Self::TupleVariant(_, v) => {
                v.push(value.into())
            }
            _ => {}
        }
        self
    }

    pub fn property<K, T>(mut self, key: K, value: T) -> Self
    where
        K: Into<Self>,
        T: Into<Self>,
    {
        if let Self::Map(v) = &mut self {
            v.push((key.into(), value.into()));
        }
        self
    }

    pub fn field<K, T>(mut self, key: K, value: T) -> Self
    where
        K: ToString,
        T: Into<Self>,
    {
        match &mut self {
            Self::Struct(v) | Self::StructVariant(_, v) => v.push((key.to_string(), value.into())),
            _ => {}
        }
        self
    }

    pub fn total_bytesize(&self) -> usize {
        fn string_bytesize(v: &str) -> usize {
            v.as_bytes().len() * std::mem::size_of::<u8>()
        }

        std::mem::size_of_val(self)
            + match self {
                Self::String(v) => string_bytesize(v),
                Self::Bytes(v) => v.len() * std::mem::size_of::<u8>(),
                Self::Option(v) => v.as_ref().map(|v| v.total_bytesize()).unwrap_or_default(),
                Self::UnitVariant(n) => string_bytesize(n),
                Self::NewTypeStruct(v) => v.total_bytesize(),
                Self::NewTypeVariant(n, v) => string_bytesize(n) + v.total_bytesize(),
                Self::Seq(v) | Self::Tuple(v) | Self::TupleStruct(v) => {
                    v.iter().map(|v| v.total_bytesize()).sum()
                }
                Self::TupleVariant(n, v) => {
                    string_bytesize(n) + v.iter().map(|v| v.total_bytesize()).sum::<usize>()
                }
                Self::Map(v) => v
                    .iter()
                    .map(|(k, v)| k.total_bytesize() + v.total_bytesize())
                    .sum(),
                Self::Struct(v) => v
                    .iter()
                    .map(|(k, v)| string_bytesize(k) + v.total_bytesize())
                    .sum(),
                Self::StructVariant(n, v) => {
                    string_bytesize(n)
                        + v.iter()
                            .map(|(k, v)| string_bytesize(k) + v.total_bytesize())
                            .sum::<usize>()
                }
                _ => 0,
            }
    }
}

impl ReflectIntermediate for Intermediate {
    fn patch_change(&mut self, change: &Change) {
        if let Ok(Some(v)) = change.patch(self) {
            *self = v;
        }
    }
}

impl std::fmt::Display for Intermediate {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let content = crate::to_string_compact(self).map_err(|_| std::fmt::Error)?;
        write!(f, "{}", content)
    }
}

macro_rules! impl_as {
    (@copy $method:ident : $type:ty => $variant:ident) => {
        pub fn $method(&self) -> Option<$type> {
            match self {
                Self::$variant(v) => Some(*v),
                _ => None,
            }
        }
    };
    (@ref $method:ident : $type:ty => $variant:ident) => {
        pub fn $method(&self) -> Option<$type> {
            match self {
                Self::$variant(v) => Some(v),
                _ => None,
            }
        }
    };
}

impl Intermediate {
    pub fn as_unit(&self) -> Option<()> {
        match self {
            Self::Unit => Some(()),
            _ => None,
        }
    }

    impl_as! {@copy as_bool : bool => Bool}
    impl_as! {@copy as_i8 : i8 => I8}
    impl_as! {@copy as_i16 : i16 => I16}
    impl_as! {@copy as_i32 : i32 => I32}
    impl_as! {@copy as_i64 : i64 => I64}
    impl_as! {@copy as_i128 : i128 => I128}
    impl_as! {@copy as_u8 : u8 => U8}
    impl_as! {@copy as_u16 : u16 => U16}
    impl_as! {@copy as_u32 : u32 => U32}
    impl_as! {@copy as_u64 : u64 => U64}
    impl_as! {@copy as_u128 : u128 => U128}
    impl_as! {@copy as_f32 : f32 => F32}
    impl_as! {@copy as_f64 : f64 => F64}
    impl_as! {@copy as_char : char => Char}
    impl_as! {@ref as_str : &str => String}
    impl_as! {@ref as_bytes : &[u8] => Bytes}
    impl_as! {@ref as_seq : &[Self] => Seq}
    impl_as! {@ref as_tuple : &[Self] => Tuple}
    impl_as! {@ref as_tuple_struct : &[Self] => TupleStruct}
    impl_as! {@ref as_map : &[(Self, Self)] => Map}
    impl_as! {@ref as_struct : &[(String, Self)] => Struct}

    pub fn as_option(&self) -> Option<&Self> {
        match self {
            Self::Option(Some(v)) => Some(v),
            _ => None,
        }
    }

    pub fn as_unit_variant(&self) -> Option<&str> {
        match self {
            Self::UnitVariant(n) => Some(n),
            _ => None,
        }
    }

    pub fn as_new_type_struct(&self) -> Option<&Self> {
        match self {
            Self::NewTypeStruct(v) => Some(v),
            _ => None,
        }
    }

    pub fn as_new_type_variant(&self) -> Option<(&str, &Self)> {
        match self {
            Self::NewTypeVariant(n, v) => Some((n, v)),
            _ => None,
        }
    }

    pub fn as_tuple_variant(&self) -> Option<(&str, &[Self])> {
        match self {
            Self::TupleVariant(n, v) => Some((n, v)),
            _ => None,
        }
    }

    #[allow(clippy::type_complexity)]
    pub fn as_struct_variant(&self) -> Option<(&str, &[(String, Self)])> {
        match self {
            Self::StructVariant(n, v) => Some((n, v)),
            _ => None,
        }
    }
}

macro_rules! impl_from_wrap {
    ($type:ty, $variant:ident) => {
        impl From<$type> for Intermediate {
            fn from(v: $type) -> Self {
                Self::$variant(v)
            }
        }
    };
}

impl From<()> for Intermediate {
    fn from(_: ()) -> Self {
        Self::Unit
    }
}

impl_from_wrap!(bool, Bool);
impl_from_wrap!(i8, I8);
impl_from_wrap!(i16, I16);
impl_from_wrap!(i32, I32);
impl_from_wrap!(i64, I64);
impl_from_wrap!(i128, I128);
impl_from_wrap!(u8, U8);
impl_from_wrap!(u16, U16);
impl_from_wrap!(u32, U32);
impl_from_wrap!(u64, U64);
impl_from_wrap!(u128, U128);
impl_from_wrap!(f32, F32);
impl_from_wrap!(f64, F64);
impl_from_wrap!(char, Char);
impl_from_wrap!(String, String);
impl_from_wrap!(Vec<u8>, Bytes);

impl From<isize> for Intermediate {
    fn from(v: isize) -> Self {
        Self::I64(v as _)
    }
}

impl From<usize> for Intermediate {
    fn from(v: usize) -> Self {
        Self::U64(v as _)
    }
}

impl From<&str> for Intermediate {
    fn from(v: &str) -> Self {
        Self::String(v.to_owned())
    }
}

impl From<Option<Intermediate>> for Intermediate {
    fn from(v: Option<Self>) -> Self {
        Self::Option(v.map(Box::new))
    }
}

impl From<Result<Intermediate, Intermediate>> for Intermediate {
    fn from(v: Result<Self, Self>) -> Self {
        match v {
            Ok(v) => Self::NewTypeVariant("Ok".to_owned(), Box::new(v)),
            Err(v) => Self::NewTypeVariant("Err".to_owned(), Box::new(v)),
        }
    }
}

impl<const N: usize> From<[Intermediate; N]> for Intermediate {
    fn from(v: [Self; N]) -> Self {
        Self::Seq(v.to_vec())
    }
}

impl From<(Intermediate,)> for Intermediate {
    fn from(v: (Self,)) -> Self {
        Self::Tuple(vec![v.0])
    }
}

macro_rules! impl_from_tuple {
    ( $( $id:ident ),+ ) => {
        impl< $( $id ),+ > From<( $( $id ),+ )> for Intermediate where $( $id: Into<Intermediate> ),+ {
            #[allow(non_snake_case)]
            fn from(v: ( $( $id ),+ )) -> Self {
                let ( $( $id ),+ ) = v;
                Self::Tuple(vec![$( $id.into() ),+])
            }
        }
    };
}

impl_from_tuple!(A, B);
impl_from_tuple!(A, B, C);
impl_from_tuple!(A, B, C, D);
impl_from_tuple!(A, B, C, D, E);
impl_from_tuple!(A, B, C, D, E, F);
impl_from_tuple!(A, B, C, D, E, F, G);
impl_from_tuple!(A, B, C, D, E, F, G, H);
impl_from_tuple!(A, B, C, D, E, F, G, H, I);
impl_from_tuple!(A, B, C, D, E, F, G, H, I, J);
impl_from_tuple!(A, B, C, D, E, F, G, H, I, J, K);
impl_from_tuple!(A, B, C, D, E, F, G, H, I, J, K, L);
impl_from_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M);
impl_from_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N);
impl_from_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
impl_from_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);
impl_from_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q);
impl_from_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R);
impl_from_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S);
impl_from_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T);
impl_from_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U);
impl_from_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V);
impl_from_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, X);
impl_from_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, X, Y);
impl_from_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, X, Y, Z);

impl From<Vec<Intermediate>> for Intermediate {
    fn from(v: Vec<Self>) -> Self {
        Self::Seq(v)
    }
}

impl From<HashSet<Intermediate>> for Intermediate {
    fn from(v: HashSet<Self>) -> Self {
        Self::Seq(v.into_iter().collect())
    }
}

impl From<HashMap<Intermediate, Intermediate>> for Intermediate {
    fn from(v: HashMap<Self, Self>) -> Self {
        Self::Map(v.into_iter().collect())
    }
}

impl From<HashMap<String, Intermediate>> for Intermediate {
    fn from(v: HashMap<String, Self>) -> Self {
        Self::Struct(v.into_iter().collect())
    }
}

impl Serialize for Intermediate {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Self::Unit => serializer.serialize_unit(),
            Self::Bool(v) => serializer.serialize_bool(*v),
            Self::I8(v) => serializer.serialize_i8(*v),
            Self::I16(v) => serializer.serialize_i16(*v),
            Self::I32(v) => serializer.serialize_i32(*v),
            Self::I64(v) => serializer.serialize_i64(*v),
            Self::I128(v) => serializer.serialize_i128(*v),
            Self::U8(v) => serializer.serialize_u8(*v),
            Self::U16(v) => serializer.serialize_u16(*v),
            Self::U32(v) => serializer.serialize_u32(*v),
            Self::U64(v) => serializer.serialize_u64(*v),
            Self::U128(v) => serializer.serialize_u128(*v),
            Self::F32(v) => serializer.serialize_f32(*v),
            Self::F64(v) => serializer.serialize_f64(*v),
            Self::Char(v) => serializer.serialize_char(*v),
            Self::String(v) => serializer.serialize_str(v),
            Self::Bytes(v) => serializer.serialize_bytes(v),
            Self::Option(v) => match v {
                Some(v) => serializer.serialize_some(v),
                None => serializer.serialize_none(),
            },
            Self::UnitStruct => serializer.serialize_unit_struct("Intermediate"),
            Self::UnitVariant(n) => serializer.serialize_unit_variant("Intermediate", 0, unsafe {
                std::mem::transmute(n.as_str())
            }),
            Self::NewTypeStruct(v) => serializer.serialize_newtype_struct("Intermediate", v),
            Self::NewTypeVariant(n, v) => serializer.serialize_newtype_variant(
                "Intermediate",
                0,
                unsafe { std::mem::transmute(n.as_str()) },
                v,
            ),
            Self::Seq(v) => {
                let mut seq = serializer.serialize_seq(Some(v.len()))?;
                for item in v {
                    seq.serialize_element(item)?;
                }
                seq.end()
            }
            Self::Tuple(v) => {
                let mut tup = serializer.serialize_tuple(v.len())?;
                for item in v {
                    tup.serialize_element(item)?;
                }
                tup.end()
            }
            Self::TupleStruct(v) => {
                let mut tup = serializer.serialize_tuple_struct("Intermediate", v.len())?;
                for item in v {
                    tup.serialize_field(item)?;
                }
                tup.end()
            }
            Self::TupleVariant(n, v) => {
                let mut tv = serializer.serialize_tuple_variant(
                    "Intermediate",
                    0,
                    unsafe { std::mem::transmute(n.as_str()) },
                    v.len(),
                )?;
                for item in v {
                    tv.serialize_field(item)?;
                }
                tv.end()
            }
            Self::Map(v) => {
                let mut map = serializer.serialize_map(Some(v.len()))?;
                for (k, v) in v {
                    map.serialize_entry(k, v)?;
                }
                map.end()
            }
            Self::Struct(v) => {
                let mut st = serializer.serialize_struct("Intermediate", v.len())?;
                for (k, v) in v {
                    st.serialize_field(unsafe { std::mem::transmute(k.as_str()) }, v)?;
                }
                st.end()
            }
            Self::StructVariant(n, v) => {
                let mut sv = serializer.serialize_struct_variant(
                    "Intermediate",
                    0,
                    unsafe { std::mem::transmute(n.as_str()) },
                    v.len(),
                )?;
                for (k, v) in v {
                    sv.serialize_field(unsafe { std::mem::transmute(k.as_str()) }, v)?;
                }
                sv.end()
            }
        }
    }
}

impl<'de> Deserialize<'de> for Intermediate {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_any(IntermediateVisitor)
    }
}