Merge pull request #611 from nickelc/serde-module

This commit is contained in:
Milo 2021-10-28 19:20:24 +01:00 committed by GitHub
commit fea3392034
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 294 additions and 294 deletions

View File

@ -1250,6 +1250,95 @@ pub mod serde {
}
}
/// Serialize into a rfc3339 time string
///
/// See [the `serde` module](./serde/index.html) for alternate
/// serializations.
impl<Tz: TimeZone> ser::Serialize for DateTime<Tz> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
struct FormatWrapped<'a, D: 'a> {
inner: &'a D,
}
impl<'a, D: fmt::Debug> fmt::Display for FormatWrapped<'a, D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.inner.fmt(f)
}
}
// Debug formatting is correct RFC3339, and it allows Zulu.
serializer.collect_str(&FormatWrapped { inner: &self })
}
}
struct DateTimeVisitor;
impl<'de> de::Visitor<'de> for DateTimeVisitor {
type Value = DateTime<FixedOffset>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a formatted date and time string or a unix timestamp")
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
value.parse().map_err(E::custom)
}
}
/// Deserialize a value that optionally includes a timezone offset in its
/// string representation
///
/// The value to be deserialized must be an rfc3339 string.
///
/// See [the `serde` module](./serde/index.html) for alternate
/// deserialization formats.
impl<'de> de::Deserialize<'de> for DateTime<FixedOffset> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
deserializer.deserialize_str(DateTimeVisitor)
}
}
/// Deserialize into a UTC value
///
/// The value to be deserialized must be an rfc3339 string.
///
/// See [the `serde` module](./serde/index.html) for alternate
/// deserialization formats.
impl<'de> de::Deserialize<'de> for DateTime<Utc> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
deserializer.deserialize_str(DateTimeVisitor).map(|dt| dt.with_timezone(&Utc))
}
}
/// Deserialize a value that includes no timezone in its string
/// representation
///
/// The value to be deserialized must be an rfc3339 string.
///
/// See [the `serde` module](./serde/index.html) for alternate
/// serialization formats.
#[cfg(feature = "clock")]
impl<'de> de::Deserialize<'de> for DateTime<Local> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
deserializer.deserialize_str(DateTimeVisitor).map(|dt| dt.with_timezone(&Local))
}
}
/// Ser/de to/from timestamps in nanoseconds
///
/// Intended for use with `serde`'s `with` attribute.
@ -1366,18 +1455,18 @@ pub mod serde {
where
D: de::Deserializer<'de>,
{
Ok(d.deserialize_i64(NanoSecondsTimestampVisitor)?)
d.deserialize_i64(NanoSecondsTimestampVisitor)
}
impl<'de> de::Visitor<'de> for NanoSecondsTimestampVisitor {
type Value = DateTime<Utc>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "a unix timestamp in nanoseconds")
formatter.write_str("a unix timestamp in nanoseconds")
}
/// Deserialize a timestamp in nanoseconds since the epoch
fn visit_i64<E>(self, value: i64) -> Result<DateTime<Utc>, E>
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -1388,7 +1477,7 @@ pub mod serde {
}
/// Deserialize a timestamp in nanoseconds since the epoch
fn visit_u64<E>(self, value: u64) -> Result<DateTime<Utc>, E>
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -1403,6 +1492,162 @@ pub mod serde {
}
}
/// Ser/de to/from timestamps in nanoseconds
///
/// Intended for use with `serde`'s `with` attribute.
///
/// # Example:
///
/// ```rust
/// # // We mark this ignored so that we can test on 1.13 (which does not
/// # // support custom derive), and run tests with --ignored on beta and
/// # // nightly to actually trigger these.
/// #
/// # #[macro_use] extern crate serde_derive;
/// # #[macro_use] extern crate serde_json;
/// # extern crate chrono;
/// # use chrono::{TimeZone, DateTime, Utc};
/// use chrono::serde::ts_nanoseconds_option;
/// #[derive(Deserialize, Serialize)]
/// struct S {
/// #[serde(with = "ts_nanoseconds_option")]
/// time: Option<DateTime<Utc>>
/// }
///
/// # fn example() -> Result<S, serde_json::Error> {
/// let time = Some(Utc.ymd(2018, 5, 17).and_hms_nano(02, 04, 59, 918355733));
/// let my_s = S {
/// time: time.clone(),
/// };
///
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
/// let my_s: S = serde_json::from_str(&as_string)?;
/// assert_eq!(my_s.time, time);
/// # Ok(my_s)
/// # }
/// # fn main() { example().unwrap(); }
/// ```
pub mod ts_nanoseconds_option {
use core::fmt;
use serdelib::{de, ser};
use {DateTime, Utc};
use super::NanoSecondsTimestampVisitor;
/// Serialize a UTC datetime into an integer number of nanoseconds since the epoch or none
///
/// Intended for use with `serde`s `serialize_with` attribute.
///
/// # Example:
///
/// ```rust
/// # // We mark this ignored so that we can test on 1.13 (which does not
/// # // support custom derive), and run tests with --ignored on beta and
/// # // nightly to actually trigger these.
/// #
/// # #[macro_use] extern crate serde_derive;
/// # #[macro_use] extern crate serde_json;
/// # extern crate chrono;
/// # use chrono::{TimeZone, DateTime, Utc};
/// use chrono::serde::ts_nanoseconds_option::serialize as to_nano_tsopt;
/// #[derive(Serialize)]
/// struct S {
/// #[serde(serialize_with = "to_nano_tsopt")]
/// time: Option<DateTime<Utc>>
/// }
///
/// # fn example() -> Result<String, serde_json::Error> {
/// let my_s = S {
/// time: Some(Utc.ymd(2018, 5, 17).and_hms_nano(02, 04, 59, 918355733)),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
/// # Ok(as_string)
/// # }
/// # fn main() { example().unwrap(); }
/// ```
pub fn serialize<S>(opt: &Option<DateTime<Utc>>, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
match *opt {
Some(ref dt) => serializer.serialize_some(&dt.timestamp_nanos()),
None => serializer.serialize_none(),
}
}
/// Deserialize a `DateTime` from a nanosecond timestamp or none
///
/// Intended for use with `serde`s `deserialize_with` attribute.
///
/// # Example:
///
/// ```rust
/// # // We mark this ignored so that we can test on 1.13 (which does not
/// # // support custom derive), and run tests with --ignored on beta and
/// # // nightly to actually trigger these.
/// #
/// # #[macro_use] extern crate serde_derive;
/// # #[macro_use] extern crate serde_json;
/// # extern crate chrono;
/// # use chrono::{DateTime, Utc};
/// use chrono::serde::ts_nanoseconds_option::deserialize as from_nano_tsopt;
/// #[derive(Deserialize)]
/// struct S {
/// #[serde(deserialize_with = "from_nano_tsopt")]
/// time: Option<DateTime<Utc>>
/// }
///
/// # fn example() -> Result<S, serde_json::Error> {
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?;
/// # Ok(my_s)
/// # }
/// # fn main() { example().unwrap(); }
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<Option<DateTime<Utc>>, D::Error>
where
D: de::Deserializer<'de>,
{
d.deserialize_option(OptionNanoSecondsTimestampVisitor)
}
struct OptionNanoSecondsTimestampVisitor;
impl<'de> de::Visitor<'de> for OptionNanoSecondsTimestampVisitor {
type Value = Option<DateTime<Utc>>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a unix timestamp in nanoseconds or none")
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>
where
D: de::Deserializer<'de>,
{
d.deserialize_i64(NanoSecondsTimestampVisitor).map(Some)
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(None)
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_unit<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(None)
}
}
}
/// Ser/de to/from timestamps in microseconds
///
/// Intended for use with `serde`'s `with` attribute.
@ -1520,36 +1765,36 @@ pub mod serde {
where
D: de::Deserializer<'de>,
{
Ok(d.deserialize_i64(MicroSecondsTimestampVisitor)?)
d.deserialize_i64(MicroSecondsTimestampVisitor)
}
impl<'de> de::Visitor<'de> for MicroSecondsTimestampVisitor {
type Value = DateTime<Utc>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "a unix timestamp in microseconds")
formatter.write_str("a unix timestamp in microseconds")
}
/// Deserialize a timestamp in milliseconds since the epoch
fn visit_i64<E>(self, value: i64) -> Result<DateTime<Utc>, E>
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
serde_from(
Utc.timestamp_opt(value / 1000_000, ((value % 1000_000) * 1_000) as u32),
Utc.timestamp_opt(value / 1_000_000, ((value % 1_000_000) * 1_000) as u32),
&value,
)
}
/// Deserialize a timestamp in milliseconds since the epoch
fn visit_u64<E>(self, value: u64) -> Result<DateTime<Utc>, E>
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
serde_from(
Utc.timestamp_opt(
(value / 1000_000) as i64,
((value % 1000_000) * 1_000) as u32,
(value / 1_000_000) as i64,
((value % 1_000_000) * 1_000) as u32,
),
&value,
)
@ -1675,7 +1920,7 @@ pub mod serde {
where
D: de::Deserializer<'de>,
{
Ok(d.deserialize_option(OptionMicroSecondsTimestampVisitor)?)
d.deserialize_option(OptionMicroSecondsTimestampVisitor)
}
struct OptionMicroSecondsTimestampVisitor;
@ -1684,11 +1929,11 @@ pub mod serde {
type Value = Option<DateTime<Utc>>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "a unix timestamp in microseconds or none")
formatter.write_str("a unix timestamp in microseconds or none")
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_some<D>(self, d: D) -> Result<Option<DateTime<Utc>>, D::Error>
fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>
where
D: de::Deserializer<'de>,
{
@ -1696,7 +1941,7 @@ pub mod serde {
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_none<E>(self) -> Result<Option<DateTime<Utc>>, E>
fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -1704,163 +1949,7 @@ pub mod serde {
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_unit<E>(self) -> Result<Option<DateTime<Utc>>, E>
where
E: de::Error,
{
Ok(None)
}
}
}
/// Ser/de to/from timestamps in nanoseconds
///
/// Intended for use with `serde`'s `with` attribute.
///
/// # Example:
///
/// ```rust
/// # // We mark this ignored so that we can test on 1.13 (which does not
/// # // support custom derive), and run tests with --ignored on beta and
/// # // nightly to actually trigger these.
/// #
/// # #[macro_use] extern crate serde_derive;
/// # #[macro_use] extern crate serde_json;
/// # extern crate chrono;
/// # use chrono::{TimeZone, DateTime, Utc};
/// use chrono::serde::ts_nanoseconds_option;
/// #[derive(Deserialize, Serialize)]
/// struct S {
/// #[serde(with = "ts_nanoseconds_option")]
/// time: Option<DateTime<Utc>>
/// }
///
/// # fn example() -> Result<S, serde_json::Error> {
/// let time = Some(Utc.ymd(2018, 5, 17).and_hms_nano(02, 04, 59, 918355733));
/// let my_s = S {
/// time: time.clone(),
/// };
///
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
/// let my_s: S = serde_json::from_str(&as_string)?;
/// assert_eq!(my_s.time, time);
/// # Ok(my_s)
/// # }
/// # fn main() { example().unwrap(); }
/// ```
pub mod ts_nanoseconds_option {
use core::fmt;
use serdelib::{de, ser};
use {DateTime, Utc};
use super::NanoSecondsTimestampVisitor;
/// Serialize a UTC datetime into an integer number of nanoseconds since the epoch or none
///
/// Intended for use with `serde`s `serialize_with` attribute.
///
/// # Example:
///
/// ```rust
/// # // We mark this ignored so that we can test on 1.13 (which does not
/// # // support custom derive), and run tests with --ignored on beta and
/// # // nightly to actually trigger these.
/// #
/// # #[macro_use] extern crate serde_derive;
/// # #[macro_use] extern crate serde_json;
/// # extern crate chrono;
/// # use chrono::{TimeZone, DateTime, Utc};
/// use chrono::serde::ts_nanoseconds_option::serialize as to_nano_tsopt;
/// #[derive(Serialize)]
/// struct S {
/// #[serde(serialize_with = "to_nano_tsopt")]
/// time: Option<DateTime<Utc>>
/// }
///
/// # fn example() -> Result<String, serde_json::Error> {
/// let my_s = S {
/// time: Some(Utc.ymd(2018, 5, 17).and_hms_nano(02, 04, 59, 918355733)),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
/// # Ok(as_string)
/// # }
/// # fn main() { example().unwrap(); }
/// ```
pub fn serialize<S>(opt: &Option<DateTime<Utc>>, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
match *opt {
Some(ref dt) => serializer.serialize_some(&dt.timestamp_nanos()),
None => serializer.serialize_none(),
}
}
/// Deserialize a `DateTime` from a nanosecond timestamp or none
///
/// Intended for use with `serde`s `deserialize_with` attribute.
///
/// # Example:
///
/// ```rust
/// # // We mark this ignored so that we can test on 1.13 (which does not
/// # // support custom derive), and run tests with --ignored on beta and
/// # // nightly to actually trigger these.
/// #
/// # #[macro_use] extern crate serde_derive;
/// # #[macro_use] extern crate serde_json;
/// # extern crate chrono;
/// # use chrono::{DateTime, Utc};
/// use chrono::serde::ts_nanoseconds_option::deserialize as from_nano_tsopt;
/// #[derive(Deserialize)]
/// struct S {
/// #[serde(deserialize_with = "from_nano_tsopt")]
/// time: Option<DateTime<Utc>>
/// }
///
/// # fn example() -> Result<S, serde_json::Error> {
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?;
/// # Ok(my_s)
/// # }
/// # fn main() { example().unwrap(); }
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<Option<DateTime<Utc>>, D::Error>
where
D: de::Deserializer<'de>,
{
Ok(d.deserialize_option(OptionNanoSecondsTimestampVisitor)?)
}
struct OptionNanoSecondsTimestampVisitor;
impl<'de> de::Visitor<'de> for OptionNanoSecondsTimestampVisitor {
type Value = Option<DateTime<Utc>>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a unix timestamp in nanoseconds or none")
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_some<D>(self, d: D) -> Result<Option<DateTime<Utc>>, D::Error>
where
D: de::Deserializer<'de>,
{
d.deserialize_i64(NanoSecondsTimestampVisitor).map(Some)
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_none<E>(self) -> Result<Option<DateTime<Utc>>, E>
where
E: de::Error,
{
Ok(None)
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_unit<E>(self) -> Result<Option<DateTime<Utc>>, E>
fn visit_unit<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -1985,7 +2074,7 @@ pub mod serde {
where
D: de::Deserializer<'de>,
{
Ok(d.deserialize_i64(MilliSecondsTimestampVisitor).map(|dt| dt.with_timezone(&Utc))?)
d.deserialize_i64(MilliSecondsTimestampVisitor).map(|dt| dt.with_timezone(&Utc))
}
impl<'de> de::Visitor<'de> for MilliSecondsTimestampVisitor {
@ -1996,7 +2085,7 @@ pub mod serde {
}
/// Deserialize a timestamp in milliseconds since the epoch
fn visit_i64<E>(self, value: i64) -> Result<DateTime<Utc>, E>
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -2007,7 +2096,7 @@ pub mod serde {
}
/// Deserialize a timestamp in milliseconds since the epoch
fn visit_u64<E>(self, value: u64) -> Result<DateTime<Utc>, E>
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -2149,8 +2238,8 @@ pub mod serde {
where
D: de::Deserializer<'de>,
{
Ok(d.deserialize_option(OptionMilliSecondsTimestampVisitor)
.map(|opt| opt.map(|dt| dt.with_timezone(&Utc)))?)
d.deserialize_option(OptionMilliSecondsTimestampVisitor)
.map(|opt| opt.map(|dt| dt.with_timezone(&Utc)))
}
struct OptionMilliSecondsTimestampVisitor;
@ -2163,7 +2252,7 @@ pub mod serde {
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_some<D>(self, d: D) -> Result<Option<DateTime<Utc>>, D::Error>
fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>
where
D: de::Deserializer<'de>,
{
@ -2171,7 +2260,7 @@ pub mod serde {
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_none<E>(self) -> Result<Option<DateTime<Utc>>, E>
fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -2179,7 +2268,7 @@ pub mod serde {
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_unit<E>(self) -> Result<Option<DateTime<Utc>>, E>
fn visit_unit<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -2304,7 +2393,7 @@ pub mod serde {
where
D: de::Deserializer<'de>,
{
Ok(d.deserialize_i64(SecondsTimestampVisitor)?)
d.deserialize_i64(SecondsTimestampVisitor)
}
impl<'de> de::Visitor<'de> for SecondsTimestampVisitor {
@ -2315,7 +2404,7 @@ pub mod serde {
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_i64<E>(self, value: i64) -> Result<DateTime<Utc>, E>
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -2323,7 +2412,7 @@ pub mod serde {
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_u64<E>(self, value: u64) -> Result<DateTime<Utc>, E>
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -2450,7 +2539,7 @@ pub mod serde {
where
D: de::Deserializer<'de>,
{
Ok(d.deserialize_option(OptionSecondsTimestampVisitor)?)
d.deserialize_option(OptionSecondsTimestampVisitor)
}
struct OptionSecondsTimestampVisitor;
@ -2463,7 +2552,7 @@ pub mod serde {
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_some<D>(self, d: D) -> Result<Option<DateTime<Utc>>, D::Error>
fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>
where
D: de::Deserializer<'de>,
{
@ -2471,7 +2560,7 @@ pub mod serde {
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_none<E>(self) -> Result<Option<DateTime<Utc>>, E>
fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -2479,7 +2568,7 @@ pub mod serde {
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_unit<E>(self) -> Result<Option<DateTime<Utc>>, E>
fn visit_unit<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -2488,95 +2577,6 @@ pub mod serde {
}
}
impl<Tz: TimeZone> ser::Serialize for DateTime<Tz> {
/// Serialize into a rfc3339 time string
///
/// See [the `serde` module](./serde/index.html) for alternate
/// serializations.
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
struct FormatWrapped<'a, D: 'a> {
inner: &'a D,
}
impl<'a, D: fmt::Debug> fmt::Display for FormatWrapped<'a, D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.inner.fmt(f)
}
}
// Debug formatting is correct RFC3339, and it allows Zulu.
serializer.collect_str(&FormatWrapped { inner: &self })
}
}
struct DateTimeVisitor;
impl<'de> de::Visitor<'de> for DateTimeVisitor {
type Value = DateTime<FixedOffset>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "a formatted date and time string or a unix timestamp")
}
fn visit_str<E>(self, value: &str) -> Result<DateTime<FixedOffset>, E>
where
E: de::Error,
{
value.parse().map_err(|err: ::format::ParseError| E::custom(err))
}
}
/// Deserialize a value that optionally includes a timezone offset in its
/// string representation
///
/// The value to be deserialized must be an rfc3339 string.
///
/// See [the `serde` module](./serde/index.html) for alternate
/// deserialization formats.
impl<'de> de::Deserialize<'de> for DateTime<FixedOffset> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
deserializer.deserialize_str(DateTimeVisitor)
}
}
/// Deserialize into a UTC value
///
/// The value to be deserialized must be an rfc3339 string.
///
/// See [the `serde` module](./serde/index.html) for alternate
/// deserialization formats.
impl<'de> de::Deserialize<'de> for DateTime<Utc> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
deserializer.deserialize_str(DateTimeVisitor).map(|dt| dt.with_timezone(&Utc))
}
}
/// Deserialize a value that includes no timezone in its string
/// representation
///
/// The value to be deserialized must be an rfc3339 string.
///
/// See [the `serde` module](./serde/index.html) for alternate
/// serialization formats.
#[cfg(feature = "clock")]
impl<'de> de::Deserialize<'de> for DateTime<Local> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
deserializer.deserialize_str(DateTimeVisitor).map(|dt| dt.with_timezone(&Local))
}
}
#[cfg(test)]
extern crate bincode;
#[cfg(test)]

View File

@ -867,7 +867,7 @@ mod weekday_serde {
type Value = Weekday;
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Weekday")
f.write_str("Weekday")
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
@ -1169,7 +1169,7 @@ mod month_serde {
type Value = Month;
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Month")
f.write_str("Month")
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>

View File

@ -1844,11 +1844,11 @@ mod serde {
type Value = NaiveDate;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "a formatted date string")
formatter.write_str("a formatted date string")
}
#[cfg(any(feature = "std", test))]
fn visit_str<E>(self, value: &str) -> Result<NaiveDate, E>
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -1856,7 +1856,7 @@ mod serde {
}
#[cfg(not(any(feature = "std", test)))]
fn visit_str<E>(self, value: &str) -> Result<NaiveDate, E>
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{

View File

@ -1751,10 +1751,10 @@ pub mod serde {
type Value = NaiveDateTime;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "a formatted date and time string")
formatter.write_str("a formatted date and time string")
}
fn visit_str<E>(self, value: &str) -> Result<NaiveDateTime, E>
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -1887,7 +1887,7 @@ pub mod serde {
where
D: de::Deserializer<'de>,
{
Ok(d.deserialize_i64(NaiveDateTimeFromNanoSecondsVisitor)?)
d.deserialize_i64(NaiveDateTimeFromNanoSecondsVisitor)
}
struct NaiveDateTimeFromNanoSecondsVisitor;
@ -1899,7 +1899,7 @@ pub mod serde {
formatter.write_str("a unix timestamp")
}
fn visit_i64<E>(self, value: i64) -> Result<NaiveDateTime, E>
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -1910,7 +1910,7 @@ pub mod serde {
.ok_or_else(|| E::custom(ne_timestamp(value)))
}
fn visit_u64<E>(self, value: u64) -> Result<NaiveDateTime, E>
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -2039,7 +2039,7 @@ pub mod serde {
where
D: de::Deserializer<'de>,
{
Ok(d.deserialize_i64(NaiveDateTimeFromMilliSecondsVisitor)?)
d.deserialize_i64(NaiveDateTimeFromMilliSecondsVisitor)
}
struct NaiveDateTimeFromMilliSecondsVisitor;
@ -2051,7 +2051,7 @@ pub mod serde {
formatter.write_str("a unix timestamp")
}
fn visit_i64<E>(self, value: i64) -> Result<NaiveDateTime, E>
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -2059,7 +2059,7 @@ pub mod serde {
.ok_or_else(|| E::custom(ne_timestamp(value)))
}
fn visit_u64<E>(self, value: u64) -> Result<NaiveDateTime, E>
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -2188,7 +2188,7 @@ pub mod serde {
where
D: de::Deserializer<'de>,
{
Ok(d.deserialize_i64(NaiveDateTimeFromSecondsVisitor)?)
d.deserialize_i64(NaiveDateTimeFromSecondsVisitor)
}
struct NaiveDateTimeFromSecondsVisitor;
@ -2200,7 +2200,7 @@ pub mod serde {
formatter.write_str("a unix timestamp")
}
fn visit_i64<E>(self, value: i64) -> Result<NaiveDateTime, E>
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
@ -2208,7 +2208,7 @@ pub mod serde {
.ok_or_else(|| E::custom(ne_timestamp(value)))
}
fn visit_u64<E>(self, value: u64) -> Result<NaiveDateTime, E>
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{

View File

@ -1490,10 +1490,10 @@ mod serde {
type Value = NaiveTime;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "a formatted time string")
formatter.write_str("a formatted time string")
}
fn visit_str<E>(self, value: &str) -> Result<NaiveTime, E>
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{