simd_json_derive/impls/
chrono.rs

1use super::{BaseGenerator, DummyGenerator};
2use crate::{de, Deserialize, Serialize, Tape, Write};
3use chrono::{DateTime, FixedOffset, TimeZone};
4use std::{fmt, io};
5
6impl<Tz: TimeZone> Serialize for DateTime<Tz> {
7    /// Serialize into a rfc3339 time string
8    ///
9    /// See [the `serde` module](./serde/index.html) for alternate
10    /// serializations.
11    fn json_write<W>(&self, writer: &mut W) -> io::Result<()>
12    where
13        W: Write,
14    {
15        struct FormatWrapped<'a, D: 'a> {
16            inner: &'a D,
17        }
18
19        impl<D: fmt::Debug> fmt::Display for FormatWrapped<'_, D> {
20            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21                self.inner.fmt(f)
22            }
23        }
24
25        // Debug formatting is correct RFC3339, and it allows Zulu.
26        DummyGenerator(writer).write_string(&format!("{}", FormatWrapped { inner: &self }))
27    }
28}
29
30impl<'input> Deserialize<'input> for DateTime<FixedOffset> {
31    #[inline]
32    fn from_tape(tape: &mut Tape<'input>) -> de::Result<Self>
33    where
34        Self: Sized + 'input,
35    {
36        match tape.next() {
37            Some(simd_json::Node::String(s)) => DateTime::parse_from_rfc2822(s)
38                .map_err(|e| de::Error::custom(format!("Invalid date string `{s}`: {e}"))),
39            _ => Err(de::Error::expected_string()),
40        }
41    }
42}