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
use serde::de::{Deserialize, DeserializeSeed, Deserializer, Error as SerdeError, IntoDeserializer, MapAccess, Visitor};
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::{self, Formatter};
use std::mem;
use types::{DateTime, JsonValue};

macro_rules! string_enums {
    (
        $(
            $(#[$attr:meta])*
            pub enum $E:ident<$lifetime:tt> {
                $(
                    $(#[$v_attr:meta])*
                    :$V:ident($by:expr) // The leading (ugly) colon is to suppress local ambiguity error.
                ),*;
                $(#[$u_attr:meta])*
                :$U:ident(_),
            }
        )*
    ) => {
        $(
            $(#[$attr])*
            pub enum $E<$lifetime> {
                $(
                    $(#[$v_attr])*
                    $V,
                )*
                $(#[$u_attr])*
                $U(::std::borrow::Cow<$lifetime, str>),
            }

            impl<'de: 'a, 'a> ::serde::Deserialize<'de> for $E<'a> {
                fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> ::std::result::Result<Self, D::Error> {
                    struct V;

                    impl<'a> ::serde::de::Visitor<'a> for V {
                        type Value = $E<'a>;

                        fn visit_str<E>(self, s: &str) -> ::std::result::Result<$E<'a>, E> {
                            match s {
                                $($by => Ok($E::$V),)*
                                _ => Ok($E::$U(s.to_owned().into())),
                            }
                        }

                        fn visit_borrowed_str<E>(self, s: &'a str) -> ::std::result::Result<$E<'a>, E> {
                            match s {
                                $($by => Ok($E::$V),)*
                                _ => Ok($E::$U(s.into())),
                            }
                        }

                        fn visit_string<E>(self, s: String) -> ::std::result::Result<$E<'a>, E> {
                            match s.as_str() {
                                $($by => Ok($E::$V),)*
                                _ => Ok($E::$U(s.into())),
                            }
                        }

                        fn expecting(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                            write!(f, "a string")
                        }
                    }

                    d.deserialize_str(V)
                }
            }

            impl<'a> ::std::convert::AsRef<str> for $E<'a> {
                fn as_ref(&self) -> &str {
                    match *self {
                        $($E::$V => $by,)*
                        $E::$U(ref s) => s.as_ref(),
                    }
                }
            }

            impl<'a> ::std::cmp::PartialEq for $E<'a> {
                fn eq(&self, other: &$E) -> bool {
                    match *self {
                        $($E::$V => match *other {
                            $E::$V => true,
                            $E::$U(ref s) if $by == s => true,
                            _ => false,
                        },)*
                        $E::$U(ref s) => match *other {
                            $($E::$V => $by == s,)*
                            $E::$U(ref t) => s == t,
                        },
                    }
                }
            }

            impl<'a> ::std::hash::Hash for $E<'a> {
                fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
                    match *self {
                        $($E::$V => $by.hash(state),)*
                        $E::$U(ref s) => s.hash(state),
                    }
                }
            }

            impl<'a> ::std::cmp::Eq for $E<'a> {}
        )*
    }
}

#[derive(Deserialize, Eq, PartialEq, Hash)]
pub struct CowStr<'a>(
    #[serde(borrow)]
    pub Cow<'a, str>
);

/// A `MapAccess` that first yields values from `keys` and `vals`, and after they are exhausted, yields from `tail`.
pub struct MapAccessChain<I, J, A> where I: IntoIterator, J: IntoIterator {
    keys: I::IntoIter,
    vals: J::IntoIter,
    tail: A,
}

impl<'a> AsRef<str> for CowStr<'a> {
    fn as_ref(&self) -> &str {
        self.0.as_ref()
    }
}

impl<I, J, A> MapAccessChain<I, J, A> where I: IntoIterator, J: IntoIterator {
    pub fn new(keys: I, vals: J, a: A) -> MapAccessChain<I, J, A> {
        MapAccessChain {
            keys: keys.into_iter(),
            vals: vals.into_iter(),
            tail: a,
        }
    }
}

impl<'de: 'a, 'a, I, J, A> MapAccess<'de> for MapAccessChain<I, J, A>
    where I: IntoIterator<Item=Cow<'a, str>>, J: IntoIterator<Item=JsonValue>, A: MapAccess<'de>
{
    type Error = A::Error;

    fn next_key_seed<K: DeserializeSeed<'de>>(&mut self, seed: K) -> Result<Option<K::Value>, A::Error> {
        if let Some(k) = self.keys.next() {
            seed.deserialize(k.into_deserializer()).map(Some)
        } else {
            self.tail.next_key_seed(seed)
        }
    }

    fn next_value_seed<V: DeserializeSeed<'de>>(&mut self, seed: V) -> Result<V::Value, A::Error> {
        if let Some(v) = self.vals.next() {
            seed.deserialize(v).map_err(A::Error::custom)
        } else {
            self.tail.next_value::<JsonValue>().and_then(|v| {
                seed.deserialize(v).map_err(A::Error::custom)
            })

            // FIXME: The above code unnecessarily copies data from the input. Initially, I wrote that like below:
            // `self.tail.next_value_seed(seed)`
            // ... but this somehow caused `message::tests::parse` test to fail, with the error being as following:
            // `ErrorImpl { code: ExpectedObjectCommaOrEnd, line: 67, column: 38 }`
        }
    }
}

pub fn deserialize_default<'de, D, T>(d: D) -> Result<T, D::Error>
    where D: Deserializer<'de>, T: Default + Deserialize<'de>
{
    Option::deserialize(d).map(|o| o.unwrap_or_else(T::default))
}

pub fn parse_datetime(s: &str) -> ::chrono::format::ParseResult<DateTime> {
    use chrono::Utc;
    use chrono::format::{self, Fixed, Item, Numeric, Pad, Parsed};

    // "%a %b %e %H:%M:%S %z %Y"
    const ITEMS: &'static [Item<'static>] = &[
        Item::Fixed(Fixed::ShortWeekdayName),
        Item::Space(" "),
        Item::Fixed(Fixed::ShortMonthName),
        Item::Space(" "),
        Item::Numeric(Numeric::Day, Pad::Space),
        Item::Space(" "),
        Item::Numeric(Numeric::Hour, Pad::Zero),
        Item::Literal(":"),
        Item::Numeric(Numeric::Minute, Pad::Zero),
        Item::Literal(":"),
        Item::Numeric(Numeric::Second, Pad::Zero),
        Item::Space(" "),
        Item::Fixed(Fixed::TimezoneOffset),
        Item::Space(" "),
        Item::Numeric(Numeric::Year, Pad::Zero),
    ];

    let mut parsed = Parsed::new();
    format::parse(&mut parsed, s, ITEMS.iter().cloned())?;
    parsed.to_datetime_with_timezone(&Utc)
}

pub fn deserialize_datetime<'x, D: Deserializer<'x>>(d: D) -> Result<DateTime, D::Error> {
    struct DTVisitor;

    impl<'x> Visitor<'x> for DTVisitor {
        type Value = DateTime;

        fn visit_str<E: SerdeError>(self, s: &str) -> Result<DateTime, E> {
            parse_datetime(s).map_err(|e| E::custom(e.to_string()))
        }

        fn expecting(&self, f: &mut Formatter) -> fmt::Result {
            write!(f, "a formatted date and time string")
        }
    }

    d.deserialize_str(DTVisitor)
}

/// Deserializes a map of strings in zero-copy fashion.
pub fn deserialize_map_cow_str<'de: 'a, 'a, D>(d: D) -> Result<HashMap<Cow<'a, str>, Cow<'a, str>>, D::Error>
    where D: Deserializer<'de>
{
    #[derive(Deserialize)]
    struct MapDeserialize<'a>(
        #[serde(borrow)]
        HashMap<CowStr<'a>, CowStr<'a>>
    );

    MapDeserialize::deserialize(d).map(|m| unsafe {
        mem::transmute::<MapDeserialize<'a>, HashMap<Cow<'a, str>, Cow<'a, str>>>(m)
    }).map_err(D::Error::custom)
}

/// Deserializes an optional string in zero-copy fashion.
pub fn deserialize_opt_cow_str<'de: 'a, 'a, D: Deserializer<'de>>(d: D) -> Result<Option<Cow<'a, str>>, D::Error> {
    #[derive(Deserialize)]
    struct OptDeserialize<'a>(
        #[serde(borrow)]
        Option<CowStr<'a>>
    );

    OptDeserialize::deserialize(d).map(|o| unsafe {
        mem::transmute::<OptDeserialize<'a>, Option<Cow<'a, str>>>(o)
    }).map_err(D::Error::custom)
}

/// Deserializes a sequenve of strings in zero-copy fashion.
pub fn deserialize_vec_cow_str<'de: 'a, 'a, D: Deserializer<'de>>(d: D) -> Result<Vec<Cow<'a, str>>, D::Error> {
    #[derive(Deserialize)]
    struct VecDeserialize<'a>(
        #[serde(borrow)]
        Vec<CowStr<'a>>
    );

    VecDeserialize::deserialize(d).map(|v| unsafe {
        mem::transmute::<VecDeserialize<'a>, Vec<Cow<'a, str>>>(v)
    }).map_err(D::Error::custom)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_deserialize_default() {
        use json;

        #[derive(Debug, Default, Deserialize, PartialEq)]
        struct S {
            #[serde(deserialize_with = "deserialize_default")]
            #[serde(default)]
            n: u32,
            #[serde(deserialize_with = "deserialize_default")]
            #[serde(default)]
            o: Option<bool>,
            #[serde(deserialize_with = "deserialize_default")]
            #[serde(default)]
            s: String,
            #[serde(deserialize_with = "deserialize_default")]
            #[serde(default)]
            v: Vec<u8>,
        }

        assert_eq!(
            json::from_str::<S>(r#"{"n":null,"s":null}"#).unwrap(),
            json::from_str(r#"{"o":null,"v":null}"#).unwrap()
        );
        assert_eq!(
            S { n: 1, o: Some(true), s: "s".to_owned(), v: vec![255] },
            json::from_str(r#"{"n":1,"o":true,"s":"s","v":[255]}"#).unwrap()
        );
    }

    #[test]
    fn test_parse_datetime() {
        use chrono::{NaiveDate, Utc};
        use types::DateTime;

        assert_eq!(
            DateTime::from_utc(NaiveDate::from_ymd(2017, 5, 1).and_hms(0, 1, 2), Utc),
            parse_datetime("Mon May 01 00:01:02 +0000 2017").unwrap()
        );
        assert!(parse_datetime("2017-05-01T00:01:02Z").is_err());
    }
}