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
/// Defines a `struct` or `enum` such that it can flexibly be parsed via
/// `serde::Deserialize` as either:
/// - Only the first field/variant type
/// - Key/value pairs
macro_rules! flexible {
    // Parse struct
    (
        $(#[serde($($t_serde:meta),+ $(,)?)])*
        $(#[$t_meta:meta])*
        $t_vis:vis struct $t:ident $(<$l:lifetime>)? {
            // The field used for the `Simple` form.
            $(#[doc = $s_doc:literal])+
            $(#[serde($($s_serde:meta),+ $(,)?)])*
            $s_vis:vis $s:ident: $s_ty:ty,

            $(
                $(#[doc = $f_doc:literal])+
                $(#[serde($($f_serde:meta),+ $(,)?)])*
                $f_vis:vis $f:ident: $f_ty:ty,
            )+
        }
    ) => {
        // The actual struct definition.
        $(#[$t_meta])*
        $t_vis struct $t $(<$l>)? {
            $(#[doc = $s_doc])+
            $s_vis $s: $s_ty,

            $(
                $(#[doc = $f_doc])+
                $f_vis $f: $f_ty,
            )*
        }

        impl<'de $(: $l)? $(, $l)?> serde::Deserialize<'de> for $t $(<$l>)? {
            #[inline]
            fn deserialize<D>(de: D) -> Result<Self, D::Error>
            where
                D: serde::Deserializer<'de>
            {
                use crate::flexible::{Flexible, Detailed};

                // Wrapper type is in a module so that it can have the same
                // name as the original.
                mod wrapper {
                    #[allow(unused_imports)]
                    use super::*;

                    $(#[serde($($t_serde),+)])*
                    #[derive(Deserialize)]
                    pub struct $t $(<$l>)? {
                        $(#[serde($($s_serde),+)])*
                        #[allow(unused)]
                        $s: $s_ty,

                        $(
                            $(#[serde($($f_serde),+)])*
                            #[allow(unused)]
                            $f: $f_ty,
                        )*
                    }
                }

                impl $(<$l>)? Detailed for wrapper::$t $(<$l>)? {
                    type Simple = $s_ty;
                }

                impl $(<$l>)? Detailed for $t $(<$l>)? {
                    type Simple = $s_ty;
                }

                Flexible::<wrapper::$t>::deserialize(de)
                    .map(|wrapper| unsafe {
                        std::mem::transmute::<_, Flexible<$t>>(wrapper)
                    })
                    .map(Flexible::into_detailed)
            }
        }
    };
    // Parse enum
    (
        $(#[serde($($t_serde:meta),+ $(,)?)])*
        $(#[$t_meta:meta])*
        $t_vis:vis enum $t:ident $(<$l:lifetime>)? {
            // The variant used for the `Simple` form.
            $(#[doc = $s_doc:literal])+
            $(#[serde($($s_serde:meta),+ $(,)?)])*
            $s:ident ($s_ty:ty),

            $(
                $(#[doc = $v_doc:literal])+
                $(#[serde($($v_serde:meta),+ $(,)?)])*
                $v:ident ($v_ty:ty),
            )+
        }
    ) => {
        // The actual enum definition.
        $(#[$t_meta])*
        $t_vis enum $t $(<$l>)? {
            $(#[doc = $s_doc])+
            $s($s_ty),

            $(
                $(#[doc = $v_doc])+
                $v($v_ty),
            )*
        }

        impl<'de $(: $l)? $(, $l)?> serde::Deserialize<'de> for $t $(<$l>)? {
            #[inline]
            fn deserialize<D>(de: D) -> Result<Self, D::Error>
            where
                D: serde::Deserializer<'de>
            {
                use crate::flexible::{Flexible, Detailed};

                // Wrapper type is in a module so that it can have the same
                // name as the original.
                mod wrapper {
                    #[allow(unused_imports)]
                    use super::*;

                    $(#[serde($($t_serde),+)])*
                    #[derive(Deserialize)]
                    $t_vis enum $t $(<$l>)? {
                        $(#[serde($($s_serde),+)])*
                        #[allow(unused)]
                        $s($s_ty),

                        $(
                            $(#[serde($($v_serde),+)])*
                            #[allow(unused)]
                            $v($v_ty),
                        )*
                    }
                }

                impl $(<$l>)? Detailed for wrapper::$t $(<$l>)? {
                    type Simple = $s_ty;
                }

                impl $(<$l>)? Detailed for $t $(<$l>)? {
                    type Simple = $s_ty;
                }

                Flexible::<wrapper::$t>::deserialize(de)
                    .map(|wrapper| unsafe {
                        std::mem::transmute::<_, Flexible<$t>>(wrapper)
                    })
                    .map(Flexible::into_detailed)
            }
        }
    };
}

/// A type that has detailed information.
pub(crate) trait Detailed: Sized {
    /// The basic version of this type.
    type Simple;
}

/// A type that can either be parsed as simple or detailed information.
#[derive(Deserialize)]
#[serde(untagged)]
pub(crate) enum Flexible<D: Detailed> {
    /// The minimal amount of information that is within `D`.
    Simple(D::Simple),
    /// All information stored within `D`.
    Detailed(D),
}

impl<D: Detailed> Flexible<D> {
    /// Converts `self` into the detailed form `D` so that all information can
    /// be used in a simple way without extra `match`ing.
    #[inline]
    pub fn into_detailed(self) -> D where D::Simple: Into<D> {
        match self {
            Self::Simple(s)   => s.into(),
            Self::Detailed(d) => d,
        }
    }
}