diff options
Diffstat (limited to 'vendor/time/src/serde')
| -rw-r--r-- | vendor/time/src/serde/iso8601.rs | 76 | ||||
| -rw-r--r-- | vendor/time/src/serde/mod.rs | 535 | ||||
| -rw-r--r-- | vendor/time/src/serde/rfc2822.rs | 71 | ||||
| -rw-r--r-- | vendor/time/src/serde/rfc3339.rs | 71 | ||||
| -rw-r--r-- | vendor/time/src/serde/timestamp/microseconds.rs | 63 | ||||
| -rw-r--r-- | vendor/time/src/serde/timestamp/milliseconds.rs | 63 | ||||
| -rw-r--r-- | vendor/time/src/serde/timestamp/milliseconds_i64.rs | 66 | ||||
| -rw-r--r-- | vendor/time/src/serde/timestamp/mod.rs | 65 | ||||
| -rw-r--r-- | vendor/time/src/serde/timestamp/nanoseconds.rs | 61 | ||||
| -rw-r--r-- | vendor/time/src/serde/visitor.rs | 355 |
10 files changed, 1426 insertions, 0 deletions
diff --git a/vendor/time/src/serde/iso8601.rs b/vendor/time/src/serde/iso8601.rs new file mode 100644 index 00000000..f08cf511 --- /dev/null +++ b/vendor/time/src/serde/iso8601.rs @@ -0,0 +1,76 @@ +//! Use the well-known [ISO 8601 format] when serializing and deserializing an [`OffsetDateTime`]. +//! +//! Use this module in combination with serde's [`#[with]`][with] attribute. +//! +//! [ISO 8601 format]: https://www.iso.org/iso-8601-date-and-time-format.html +//! [with]: https://serde.rs/field-attrs.html#with + +#[cfg(feature = "parsing")] +use core::marker::PhantomData; + +#[cfg(feature = "formatting")] +use serde::ser::Error as _; +#[cfg(feature = "parsing")] +use serde::Deserializer; +#[cfg(feature = "formatting")] +use serde::{Serialize, Serializer}; + +#[cfg(feature = "parsing")] +use super::Visitor; +use crate::format_description::well_known::iso8601::{Config, EncodedConfig}; +use crate::format_description::well_known::Iso8601; +use crate::OffsetDateTime; + +/// The configuration of ISO 8601 used for serde implementations. +pub(crate) const SERDE_CONFIG: EncodedConfig = + Config::DEFAULT.set_year_is_six_digits(true).encode(); + +/// Serialize an [`OffsetDateTime`] using the well-known ISO 8601 format. +#[cfg(feature = "formatting")] +pub fn serialize<S: Serializer>( + datetime: &OffsetDateTime, + serializer: S, +) -> Result<S::Ok, S::Error> { + datetime + .format(&Iso8601::<SERDE_CONFIG>) + .map_err(S::Error::custom)? + .serialize(serializer) +} + +/// Deserialize an [`OffsetDateTime`] from its ISO 8601 representation. +#[cfg(feature = "parsing")] +pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result<OffsetDateTime, D::Error> { + deserializer.deserialize_str(Visitor::<Iso8601<SERDE_CONFIG>>(PhantomData)) +} + +/// Use the well-known ISO 8601 format when serializing and deserializing an +/// [`Option<OffsetDateTime>`]. +/// +/// Use this module in combination with serde's [`#[with]`][with] attribute. +/// +/// [ISO 8601 format]: https://www.iso.org/iso-8601-date-and-time-format.html +/// [with]: https://serde.rs/field-attrs.html#with +pub mod option { + use super::*; + + /// Serialize an [`Option<OffsetDateTime>`] using the well-known ISO 8601 format. + #[cfg(feature = "formatting")] + pub fn serialize<S: Serializer>( + option: &Option<OffsetDateTime>, + serializer: S, + ) -> Result<S::Ok, S::Error> { + option + .map(|odt| odt.format(&Iso8601::<SERDE_CONFIG>)) + .transpose() + .map_err(S::Error::custom)? + .serialize(serializer) + } + + /// Deserialize an [`Option<OffsetDateTime>`] from its ISO 8601 representation. + #[cfg(feature = "parsing")] + pub fn deserialize<'a, D: Deserializer<'a>>( + deserializer: D, + ) -> Result<Option<OffsetDateTime>, D::Error> { + deserializer.deserialize_option(Visitor::<Option<Iso8601<SERDE_CONFIG>>>(PhantomData)) + } +} diff --git a/vendor/time/src/serde/mod.rs b/vendor/time/src/serde/mod.rs new file mode 100644 index 00000000..72b43721 --- /dev/null +++ b/vendor/time/src/serde/mod.rs @@ -0,0 +1,535 @@ +//! Differential formats for serde. +// This also includes the serde implementations for all types. This doesn't need to be externally +// documented, though. + +// Types with guaranteed stable serde representations. Strings are avoided to allow for optimal +// representations in various binary forms. + +/// Consume the next item in a sequence. +macro_rules! item { + ($seq:expr, $name:literal) => { + $seq.next_element()? + .ok_or_else(|| <A::Error as serde::de::Error>::custom(concat!("expected ", $name))) + }; +} + +#[cfg(any(feature = "formatting", feature = "parsing"))] +pub mod iso8601; +#[cfg(any(feature = "formatting", feature = "parsing"))] +pub mod rfc2822; +#[cfg(any(feature = "formatting", feature = "parsing"))] +pub mod rfc3339; +pub mod timestamp; +mod visitor; + +#[cfg(feature = "serde-human-readable")] +use alloc::string::ToString; +use core::marker::PhantomData; + +#[cfg(feature = "serde-human-readable")] +use serde::ser::Error as _; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; +/// Generate a custom serializer and deserializer from a format string or an existing format. +/// +/// The syntax accepted by this macro is the same as [`format_description::parse()`], which can +/// be found in [the book](https://time-rs.github.io/book/api/format-description.html). +/// +/// # Usage +/// +/// Invoked as `serde::format_description!(mod_name, Date, FORMAT)` where `FORMAT` is either a +/// `"<format string>"` or something that implements +#[cfg_attr( + all(feature = "formatting", feature = "parsing"), + doc = "[`Formattable`](crate::formatting::Formattable) and \ + [`Parsable`](crate::parsing::Parsable)." +)] +#[cfg_attr( + all(feature = "formatting", not(feature = "parsing")), + doc = "[`Formattable`](crate::formatting::Formattable)." +)] +#[cfg_attr( + all(not(feature = "formatting"), feature = "parsing"), + doc = "[`Parsable`](crate::parsing::Parsable)." +)] +/// This puts a module named `mod_name` in the current scope that can be used to format `Date` +/// structs. A submodule (`mod_name::option`) is also generated for `Option<Date>`. Both +/// modules are only visible in the current scope by default. To increase visibility, you can +/// specify `pub`, `pub(crate)`, or similar before the module name: +/// `serde::format_description!(pub mod_name, Date, FORMAT)`. +/// +/// The returned `Option` will contain a deserialized value if present and `None` if the field +/// is present but the value is `null` (or the equivalent in other formats). To return `None` +/// when the field is not present, you should use `#[serde(default)]` on the field. +/// +/// # Examples +/// +/// Using a format string: +/// +/// ```rust,no_run +/// # use time::OffsetDateTime; +#[cfg_attr( + all(feature = "formatting", feature = "parsing"), + doc = "use ::serde::{Serialize, Deserialize};" +)] +#[cfg_attr( + all(feature = "formatting", not(feature = "parsing")), + doc = "use ::serde::Serialize;" +)] +#[cfg_attr( + all(not(feature = "formatting"), feature = "parsing"), + doc = "use ::serde::Deserialize;" +)] +/// use time::serde; +/// +/// // Makes a module `mod my_format { ... }`. +/// serde::format_description!(my_format, OffsetDateTime, "hour=[hour], minute=[minute]"); +/// +/// # #[allow(dead_code)] +#[cfg_attr( + all(feature = "formatting", feature = "parsing"), + doc = "#[derive(Serialize, Deserialize)]" +)] +#[cfg_attr( + all(feature = "formatting", not(feature = "parsing")), + doc = "#[derive(Serialize)]" +)] +#[cfg_attr( + all(not(feature = "formatting"), feature = "parsing"), + doc = "#[derive(Deserialize)]" +)] +/// struct SerializesWithCustom { +/// #[serde(with = "my_format")] +/// dt: OffsetDateTime, +/// #[serde(with = "my_format::option")] +/// maybe_dt: Option<OffsetDateTime>, +/// } +/// ``` +/// +/// Define the format separately to be used in multiple places: +/// ```rust,no_run +/// # use time::OffsetDateTime; +#[cfg_attr( + all(feature = "formatting", feature = "parsing"), + doc = "use ::serde::{Serialize, Deserialize};" +)] +#[cfg_attr( + all(feature = "formatting", not(feature = "parsing")), + doc = "use ::serde::Serialize;" +)] +#[cfg_attr( + all(not(feature = "formatting"), feature = "parsing"), + doc = "use ::serde::Deserialize;" +)] +/// use time::serde; +/// use time::format_description::BorrowedFormatItem; +/// +/// const DATE_TIME_FORMAT: &[BorrowedFormatItem<'_>] = time::macros::format_description!( +/// "hour=[hour], minute=[minute]" +/// ); +/// +/// // Makes a module `mod my_format { ... }`. +/// serde::format_description!(my_format, OffsetDateTime, DATE_TIME_FORMAT); +/// +/// # #[allow(dead_code)] +#[cfg_attr( + all(feature = "formatting", feature = "parsing"), + doc = "#[derive(Serialize, Deserialize)]" +)] +#[cfg_attr( + all(feature = "formatting", not(feature = "parsing")), + doc = "#[derive(Serialize)]" +)] +#[cfg_attr( + all(not(feature = "formatting"), feature = "parsing"), + doc = "#[derive(Deserialize)]" +)] +/// struct SerializesWithCustom { +/// #[serde(with = "my_format")] +/// dt: OffsetDateTime, +/// #[serde(with = "my_format::option")] +/// maybe_dt: Option<OffsetDateTime>, +/// } +/// +/// fn main() { +/// # #[allow(unused_variables)] +/// let str_ts = OffsetDateTime::now_utc().format(DATE_TIME_FORMAT).unwrap(); +/// } +/// ``` +/// +/// Customize the configuration of ISO 8601 formatting/parsing: +/// ```rust,no_run +/// # use time::OffsetDateTime; +#[cfg_attr( + all(feature = "formatting", feature = "parsing"), + doc = "use ::serde::{Serialize, Deserialize};" +)] +#[cfg_attr( + all(feature = "formatting", not(feature = "parsing")), + doc = "use ::serde::Serialize;" +)] +#[cfg_attr( + all(not(feature = "formatting"), feature = "parsing"), + doc = "use ::serde::Deserialize;" +)] +/// use time::serde; +/// use time::format_description::well_known::{iso8601, Iso8601}; +/// +/// # #[allow(dead_code)] +/// const CONFIG: iso8601::EncodedConfig = iso8601::Config::DEFAULT +/// .set_year_is_six_digits(false) +/// .encode(); +/// # #[allow(dead_code)] +/// const FORMAT: Iso8601<CONFIG> = Iso8601::<CONFIG>; +/// +/// // Makes a module `mod my_format { ... }`. +/// serde::format_description!(my_format, OffsetDateTime, FORMAT); +/// +/// # #[allow(dead_code)] +#[cfg_attr( + all(feature = "formatting", feature = "parsing"), + doc = "#[derive(Serialize, Deserialize)]" +)] +#[cfg_attr( + all(feature = "formatting", not(feature = "parsing")), + doc = "#[derive(Serialize)]" +)] +#[cfg_attr( + all(not(feature = "formatting"), feature = "parsing"), + doc = "#[derive(Deserialize)]" +)] +/// struct SerializesWithCustom { +/// #[serde(with = "my_format")] +/// dt: OffsetDateTime, +/// #[serde(with = "my_format::option")] +/// maybe_dt: Option<OffsetDateTime>, +/// } +/// # fn main() {} +/// ``` +/// +/// [`format_description::parse()`]: crate::format_description::parse() +#[cfg(all(feature = "macros", any(feature = "formatting", feature = "parsing")))] +pub use time_macros::serde_format_description as format_description; + +use self::visitor::Visitor; +#[cfg(feature = "parsing")] +use crate::format_description::{modifier, BorrowedFormatItem, Component}; +use crate::{ + Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcDateTime, UtcOffset, Weekday, +}; + +/// The format used when serializing and deserializing a human-readable `Date`. +#[cfg(feature = "parsing")] +const DATE_FORMAT: &[BorrowedFormatItem<'_>] = &[ + BorrowedFormatItem::Component(Component::Year(modifier::Year::default())), + BorrowedFormatItem::Literal(b"-"), + BorrowedFormatItem::Component(Component::Month(modifier::Month::default())), + BorrowedFormatItem::Literal(b"-"), + BorrowedFormatItem::Component(Component::Day(modifier::Day::default())), +]; + +impl Serialize for Date { + fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { + #[cfg(feature = "serde-human-readable")] + if serializer.is_human_readable() { + let Ok(s) = self.format(&DATE_FORMAT) else { + return Err(S::Error::custom("failed formatting `Date`")); + }; + return serializer.serialize_str(&s); + } + + (self.year(), self.ordinal()).serialize(serializer) + } +} + +impl<'a> Deserialize<'a> for Date { + fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> { + if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() { + deserializer.deserialize_any(Visitor::<Self>(PhantomData)) + } else { + deserializer.deserialize_tuple(2, Visitor::<Self>(PhantomData)) + } + } +} + +impl Serialize for Duration { + fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { + #[cfg(feature = "serde-human-readable")] + if serializer.is_human_readable() { + return serializer.collect_str(&format_args!( + "{}{}.{:>09}", + if self.is_negative() { "-" } else { "" }, + self.whole_seconds().unsigned_abs(), + self.subsec_nanoseconds().abs(), + )); + } + + (self.whole_seconds(), self.subsec_nanoseconds()).serialize(serializer) + } +} + +impl<'a> Deserialize<'a> for Duration { + fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> { + if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() { + deserializer.deserialize_any(Visitor::<Self>(PhantomData)) + } else { + deserializer.deserialize_tuple(2, Visitor::<Self>(PhantomData)) + } + } +} + +/// The format used when serializing and deserializing a human-readable `OffsetDateTime`. +#[cfg(feature = "parsing")] +const OFFSET_DATE_TIME_FORMAT: &[BorrowedFormatItem<'_>] = &[ + BorrowedFormatItem::Compound(DATE_FORMAT), + BorrowedFormatItem::Literal(b" "), + BorrowedFormatItem::Compound(TIME_FORMAT), + BorrowedFormatItem::Literal(b" "), + BorrowedFormatItem::Compound(UTC_OFFSET_FORMAT), +]; + +impl Serialize for OffsetDateTime { + fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { + #[cfg(feature = "serde-human-readable")] + if serializer.is_human_readable() { + let Ok(s) = self.format(&OFFSET_DATE_TIME_FORMAT) else { + return Err(S::Error::custom("failed formatting `OffsetDateTime`")); + }; + return serializer.serialize_str(&s); + } + + ( + self.year(), + self.ordinal(), + self.hour(), + self.minute(), + self.second(), + self.nanosecond(), + self.offset().whole_hours(), + self.offset().minutes_past_hour(), + self.offset().seconds_past_minute(), + ) + .serialize(serializer) + } +} + +impl<'a> Deserialize<'a> for OffsetDateTime { + fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> { + if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() { + deserializer.deserialize_any(Visitor::<Self>(PhantomData)) + } else { + deserializer.deserialize_tuple(9, Visitor::<Self>(PhantomData)) + } + } +} + +/// The format used when serializing and deserializing a human-readable `PrimitiveDateTime`. +#[cfg(feature = "parsing")] +const PRIMITIVE_DATE_TIME_FORMAT: &[BorrowedFormatItem<'_>] = &[ + BorrowedFormatItem::Compound(DATE_FORMAT), + BorrowedFormatItem::Literal(b" "), + BorrowedFormatItem::Compound(TIME_FORMAT), +]; + +impl Serialize for PrimitiveDateTime { + fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { + #[cfg(feature = "serde-human-readable")] + if serializer.is_human_readable() { + let Ok(s) = self.format(&PRIMITIVE_DATE_TIME_FORMAT) else { + return Err(S::Error::custom("failed formatting `PrimitiveDateTime`")); + }; + return serializer.serialize_str(&s); + } + + ( + self.year(), + self.ordinal(), + self.hour(), + self.minute(), + self.second(), + self.nanosecond(), + ) + .serialize(serializer) + } +} + +impl<'a> Deserialize<'a> for PrimitiveDateTime { + fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> { + if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() { + deserializer.deserialize_any(Visitor::<Self>(PhantomData)) + } else { + deserializer.deserialize_tuple(6, Visitor::<Self>(PhantomData)) + } + } +} + +/// The format used when serializing and deserializing a human-readable `UtcDateTime`. +#[cfg(feature = "parsing")] +const UTC_DATE_TIME_FORMAT: &[BorrowedFormatItem<'_>] = PRIMITIVE_DATE_TIME_FORMAT; + +impl Serialize for UtcDateTime { + fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { + #[cfg(feature = "serde-human-readable")] + if serializer.is_human_readable() { + let Ok(s) = self.format(&PRIMITIVE_DATE_TIME_FORMAT) else { + return Err(S::Error::custom("failed formatting `UtcDateTime`")); + }; + return serializer.serialize_str(&s); + } + + ( + self.year(), + self.ordinal(), + self.hour(), + self.minute(), + self.second(), + self.nanosecond(), + ) + .serialize(serializer) + } +} + +impl<'a> Deserialize<'a> for UtcDateTime { + fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> { + if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() { + deserializer.deserialize_any(Visitor::<Self>(PhantomData)) + } else { + deserializer.deserialize_tuple(6, Visitor::<Self>(PhantomData)) + } + } +} + +/// The format used when serializing and deserializing a human-readable `Time`. +#[cfg(feature = "parsing")] +const TIME_FORMAT: &[BorrowedFormatItem<'_>] = &[ + BorrowedFormatItem::Component(Component::Hour(modifier::Hour::default())), + BorrowedFormatItem::Literal(b":"), + BorrowedFormatItem::Component(Component::Minute(modifier::Minute::default())), + BorrowedFormatItem::Literal(b":"), + BorrowedFormatItem::Component(Component::Second(modifier::Second::default())), + BorrowedFormatItem::Literal(b"."), + BorrowedFormatItem::Component(Component::Subsecond(modifier::Subsecond::default())), +]; + +impl Serialize for Time { + fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { + #[cfg(feature = "serde-human-readable")] + if serializer.is_human_readable() { + let Ok(s) = self.format(&TIME_FORMAT) else { + return Err(S::Error::custom("failed formatting `Time`")); + }; + return serializer.serialize_str(&s); + } + + (self.hour(), self.minute(), self.second(), self.nanosecond()).serialize(serializer) + } +} + +impl<'a> Deserialize<'a> for Time { + fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> { + if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() { + deserializer.deserialize_any(Visitor::<Self>(PhantomData)) + } else { + deserializer.deserialize_tuple(4, Visitor::<Self>(PhantomData)) + } + } +} + +// FIXME: turn these constants into `const { ... }` blocks once we can depend on Rust 1.79. +#[cfg(feature = "parsing")] +const UTC_OFFSET_HOUR: modifier::OffsetHour = { + let mut m = modifier::OffsetHour::default(); + m.sign_is_mandatory = true; + m +}; +#[cfg(feature = "parsing")] +const UTC_OFFSET_MINUTE: modifier::OffsetMinute = modifier::OffsetMinute::default(); +#[cfg(feature = "parsing")] +const UTC_OFFSET_SECOND: modifier::OffsetSecond = modifier::OffsetSecond::default(); +/// The format used when serializing and deserializing a human-readable `UtcOffset`. +#[cfg(feature = "parsing")] +const UTC_OFFSET_FORMAT: &[BorrowedFormatItem<'_>] = &[ + BorrowedFormatItem::Component(Component::OffsetHour(UTC_OFFSET_HOUR)), + BorrowedFormatItem::Optional(&BorrowedFormatItem::Compound(&[ + BorrowedFormatItem::Literal(b":"), + BorrowedFormatItem::Component(Component::OffsetMinute(UTC_OFFSET_MINUTE)), + BorrowedFormatItem::Optional(&BorrowedFormatItem::Compound(&[ + BorrowedFormatItem::Literal(b":"), + BorrowedFormatItem::Component(Component::OffsetSecond(UTC_OFFSET_SECOND)), + ])), + ])), +]; + +impl Serialize for UtcOffset { + fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { + #[cfg(feature = "serde-human-readable")] + if serializer.is_human_readable() { + let Ok(s) = self.format(&UTC_OFFSET_FORMAT) else { + return Err(S::Error::custom("failed formatting `UtcOffset`")); + }; + return serializer.serialize_str(&s); + } + + ( + self.whole_hours(), + self.minutes_past_hour(), + self.seconds_past_minute(), + ) + .serialize(serializer) + } +} + +impl<'a> Deserialize<'a> for UtcOffset { + fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> { + if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() { + deserializer.deserialize_any(Visitor::<Self>(PhantomData)) + } else { + deserializer.deserialize_tuple(3, Visitor::<Self>(PhantomData)) + } + } +} + +impl Serialize for Weekday { + fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { + #[cfg(feature = "serde-human-readable")] + if serializer.is_human_readable() { + #[cfg(not(feature = "std"))] + use alloc::string::ToString; + return self.to_string().serialize(serializer); + } + + self.number_from_monday().serialize(serializer) + } +} + +impl<'a> Deserialize<'a> for Weekday { + fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> { + if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() { + deserializer.deserialize_any(Visitor::<Self>(PhantomData)) + } else { + deserializer.deserialize_u8(Visitor::<Self>(PhantomData)) + } + } +} + +impl Serialize for Month { + fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { + #[cfg(feature = "serde-human-readable")] + if serializer.is_human_readable() { + #[cfg(not(feature = "std"))] + use alloc::string::String; + return self.to_string().serialize(serializer); + } + + u8::from(*self).serialize(serializer) + } +} + +impl<'a> Deserialize<'a> for Month { + fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> { + if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() { + deserializer.deserialize_any(Visitor::<Self>(PhantomData)) + } else { + deserializer.deserialize_u8(Visitor::<Self>(PhantomData)) + } + } +} diff --git a/vendor/time/src/serde/rfc2822.rs b/vendor/time/src/serde/rfc2822.rs new file mode 100644 index 00000000..c6716d58 --- /dev/null +++ b/vendor/time/src/serde/rfc2822.rs @@ -0,0 +1,71 @@ +//! Use the well-known [RFC2822 format] when serializing and deserializing an [`OffsetDateTime`]. +//! +//! Use this module in combination with serde's [`#[with]`][with] attribute. +//! +//! [RFC2822 format]: https://tools.ietf.org/html/rfc2822#section-3.3 +//! [with]: https://serde.rs/field-attrs.html#with + +#[cfg(feature = "parsing")] +use core::marker::PhantomData; + +#[cfg(feature = "formatting")] +use serde::ser::Error as _; +#[cfg(feature = "parsing")] +use serde::Deserializer; +#[cfg(feature = "formatting")] +use serde::{Serialize, Serializer}; + +#[cfg(feature = "parsing")] +use super::Visitor; +use crate::format_description::well_known::Rfc2822; +use crate::OffsetDateTime; + +/// Serialize an [`OffsetDateTime`] using the well-known RFC2822 format. +#[cfg(feature = "formatting")] +pub fn serialize<S: Serializer>( + datetime: &OffsetDateTime, + serializer: S, +) -> Result<S::Ok, S::Error> { + datetime + .format(&Rfc2822) + .map_err(S::Error::custom)? + .serialize(serializer) +} + +/// Deserialize an [`OffsetDateTime`] from its RFC2822 representation. +#[cfg(feature = "parsing")] +pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result<OffsetDateTime, D::Error> { + deserializer.deserialize_str(Visitor::<Rfc2822>(PhantomData)) +} + +/// Use the well-known [RFC2822 format] when serializing and deserializing an +/// [`Option<OffsetDateTime>`]. +/// +/// Use this module in combination with serde's [`#[with]`][with] attribute. +/// +/// [RFC2822 format]: https://tools.ietf.org/html/rfc2822#section-3.3 +/// [with]: https://serde.rs/field-attrs.html#with +pub mod option { + use super::*; + + /// Serialize an [`Option<OffsetDateTime>`] using the well-known RFC2822 format. + #[cfg(feature = "formatting")] + pub fn serialize<S: Serializer>( + option: &Option<OffsetDateTime>, + serializer: S, + ) -> Result<S::Ok, S::Error> { + option + .map(|odt| odt.format(&Rfc2822)) + .transpose() + .map_err(S::Error::custom)? + .serialize(serializer) + } + + /// Deserialize an [`Option<OffsetDateTime>`] from its RFC2822 representation. + #[cfg(feature = "parsing")] + pub fn deserialize<'a, D: Deserializer<'a>>( + deserializer: D, + ) -> Result<Option<OffsetDateTime>, D::Error> { + deserializer.deserialize_option(Visitor::<Option<Rfc2822>>(PhantomData)) + } +} diff --git a/vendor/time/src/serde/rfc3339.rs b/vendor/time/src/serde/rfc3339.rs new file mode 100644 index 00000000..4cccd635 --- /dev/null +++ b/vendor/time/src/serde/rfc3339.rs @@ -0,0 +1,71 @@ +//! Use the well-known [RFC3339 format] when serializing and deserializing an [`OffsetDateTime`]. +//! +//! Use this module in combination with serde's [`#[with]`][with] attribute. +//! +//! [RFC3339 format]: https://tools.ietf.org/html/rfc3339#section-5.6 +//! [with]: https://serde.rs/field-attrs.html#with + +#[cfg(feature = "parsing")] +use core::marker::PhantomData; + +#[cfg(feature = "formatting")] +use serde::ser::Error as _; +#[cfg(feature = "parsing")] +use serde::Deserializer; +#[cfg(feature = "formatting")] +use serde::{Serialize, Serializer}; + +#[cfg(feature = "parsing")] +use super::Visitor; +use crate::format_description::well_known::Rfc3339; +use crate::OffsetDateTime; + +/// Serialize an [`OffsetDateTime`] using the well-known RFC3339 format. +#[cfg(feature = "formatting")] +pub fn serialize<S: Serializer>( + datetime: &OffsetDateTime, + serializer: S, +) -> Result<S::Ok, S::Error> { + datetime + .format(&Rfc3339) + .map_err(S::Error::custom)? + .serialize(serializer) +} + +/// Deserialize an [`OffsetDateTime`] from its RFC3339 representation. +#[cfg(feature = "parsing")] +pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result<OffsetDateTime, D::Error> { + deserializer.deserialize_str(Visitor::<Rfc3339>(PhantomData)) +} + +/// Use the well-known [RFC3339 format] when serializing and deserializing an +/// [`Option<OffsetDateTime>`]. +/// +/// Use this module in combination with serde's [`#[with]`][with] attribute. +/// +/// [RFC3339 format]: https://tools.ietf.org/html/rfc3339#section-5.6 +/// [with]: https://serde.rs/field-attrs.html#with +pub mod option { + use super::*; + + /// Serialize an [`Option<OffsetDateTime>`] using the well-known RFC3339 format. + #[cfg(feature = "formatting")] + pub fn serialize<S: Serializer>( + option: &Option<OffsetDateTime>, + serializer: S, + ) -> Result<S::Ok, S::Error> { + option + .map(|odt| odt.format(&Rfc3339)) + .transpose() + .map_err(S::Error::custom)? + .serialize(serializer) + } + + /// Deserialize an [`Option<OffsetDateTime>`] from its RFC3339 representation. + #[cfg(feature = "parsing")] + pub fn deserialize<'a, D: Deserializer<'a>>( + deserializer: D, + ) -> Result<Option<OffsetDateTime>, D::Error> { + deserializer.deserialize_option(Visitor::<Option<Rfc3339>>(PhantomData)) + } +} diff --git a/vendor/time/src/serde/timestamp/microseconds.rs b/vendor/time/src/serde/timestamp/microseconds.rs new file mode 100644 index 00000000..65c603ec --- /dev/null +++ b/vendor/time/src/serde/timestamp/microseconds.rs @@ -0,0 +1,63 @@ +//! Treat an [`OffsetDateTime`] as a [Unix timestamp] with microseconds for +//! the purposes of serde. +//! +//! Use this module in combination with serde's [`#[with]`][with] attribute. +//! +//! When deserializing, the offset is assumed to be UTC. +//! +//! [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time +//! [with]: https://serde.rs/field-attrs.html#with + +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + +use crate::OffsetDateTime; + +/// Serialize an `OffsetDateTime` as its Unix timestamp with microseconds +pub fn serialize<S: Serializer>( + datetime: &OffsetDateTime, + serializer: S, +) -> Result<S::Ok, S::Error> { + let timestamp = datetime.unix_timestamp_nanos() / 1_000; + timestamp.serialize(serializer) +} + +/// Deserialize an `OffsetDateTime` from its Unix timestamp with microseconds +pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result<OffsetDateTime, D::Error> { + let value: i128 = <_>::deserialize(deserializer)?; + OffsetDateTime::from_unix_timestamp_nanos(value * 1_000) + .map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) +} + +/// Treat an `Option<OffsetDateTime>` as a [Unix timestamp] with microseconds +/// for the purposes of serde. +/// +/// Use this module in combination with serde's [`#[with]`][with] attribute. +/// +/// When deserializing, the offset is assumed to be UTC. +/// +/// [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time +/// [with]: https://serde.rs/field-attrs.html#with +pub mod option { + #[allow(clippy::wildcard_imports)] + use super::*; + + /// Serialize an `Option<OffsetDateTime>` as its Unix timestamp with microseconds + pub fn serialize<S: Serializer>( + option: &Option<OffsetDateTime>, + serializer: S, + ) -> Result<S::Ok, S::Error> { + option + .map(|timestamp| timestamp.unix_timestamp_nanos() / 1_000) + .serialize(serializer) + } + + /// Deserialize an `Option<OffsetDateTime>` from its Unix timestamp with microseconds + pub fn deserialize<'a, D: Deserializer<'a>>( + deserializer: D, + ) -> Result<Option<OffsetDateTime>, D::Error> { + Option::deserialize(deserializer)? + .map(|value: i128| OffsetDateTime::from_unix_timestamp_nanos(value * 1_000)) + .transpose() + .map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) + } +} diff --git a/vendor/time/src/serde/timestamp/milliseconds.rs b/vendor/time/src/serde/timestamp/milliseconds.rs new file mode 100644 index 00000000..e571b6c9 --- /dev/null +++ b/vendor/time/src/serde/timestamp/milliseconds.rs @@ -0,0 +1,63 @@ +//! Treat an [`OffsetDateTime`] as a [Unix timestamp] with milliseconds for +//! the purposes of serde. +//! +//! Use this module in combination with serde's [`#[with]`][with] attribute. +//! +//! When deserializing, the offset is assumed to be UTC. +//! +//! [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time +//! [with]: https://serde.rs/field-attrs.html#with + +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + +use crate::OffsetDateTime; + +/// Serialize an `OffsetDateTime` as its Unix timestamp with milliseconds +pub fn serialize<S: Serializer>( + datetime: &OffsetDateTime, + serializer: S, +) -> Result<S::Ok, S::Error> { + let timestamp = datetime.unix_timestamp_nanos() / 1_000_000; + timestamp.serialize(serializer) +} + +/// Deserialize an `OffsetDateTime` from its Unix timestamp with milliseconds +pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result<OffsetDateTime, D::Error> { + let value: i128 = <_>::deserialize(deserializer)?; + OffsetDateTime::from_unix_timestamp_nanos(value * 1_000_000) + .map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) +} + +/// Treat an `Option<OffsetDateTime>` as a [Unix timestamp] with milliseconds +/// for the purposes of serde. +/// +/// Use this module in combination with serde's [`#[with]`][with] attribute. +/// +/// When deserializing, the offset is assumed to be UTC. +/// +/// [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time +/// [with]: https://serde.rs/field-attrs.html#with +pub mod option { + #[allow(clippy::wildcard_imports)] + use super::*; + + /// Serialize an `Option<OffsetDateTime>` as its Unix timestamp with milliseconds + pub fn serialize<S: Serializer>( + option: &Option<OffsetDateTime>, + serializer: S, + ) -> Result<S::Ok, S::Error> { + option + .map(|timestamp| timestamp.unix_timestamp_nanos() / 1_000_000) + .serialize(serializer) + } + + /// Deserialize an `Option<OffsetDateTime>` from its Unix timestamp with milliseconds + pub fn deserialize<'a, D: Deserializer<'a>>( + deserializer: D, + ) -> Result<Option<OffsetDateTime>, D::Error> { + Option::deserialize(deserializer)? + .map(|value: i128| OffsetDateTime::from_unix_timestamp_nanos(value * 1_000_000)) + .transpose() + .map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) + } +} diff --git a/vendor/time/src/serde/timestamp/milliseconds_i64.rs b/vendor/time/src/serde/timestamp/milliseconds_i64.rs new file mode 100644 index 00000000..59f44644 --- /dev/null +++ b/vendor/time/src/serde/timestamp/milliseconds_i64.rs @@ -0,0 +1,66 @@ +//! Treat an [`OffsetDateTime`] as a [Unix timestamp] with milliseconds for +//! the purposes of serde. +//! +//! Use this module in combination with serde's [`#[with]`][with] attribute. +//! +//! When deserializing, the offset is assumed to be UTC. +//! +//! [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time +//! [with]: https://serde.rs/field-attrs.html#with + +use num_conv::prelude::*; +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + +use crate::OffsetDateTime; + +/// Serialize an `OffsetDateTime` as its Unix timestamp with milliseconds +pub fn serialize<S: Serializer>( + datetime: &OffsetDateTime, + serializer: S, +) -> Result<S::Ok, S::Error> { + let timestamp = (datetime.unix_timestamp_nanos() / 1_000_000).truncate::<i64>(); + timestamp.serialize(serializer) +} + +/// Deserialize an `OffsetDateTime` from its Unix timestamp with milliseconds +pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result<OffsetDateTime, D::Error> { + let value: i64 = <_>::deserialize(deserializer)?; + OffsetDateTime::from_unix_timestamp_nanos(value.extend::<i128>() * 1_000_000) + .map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) +} + +/// Treat an `Option<OffsetDateTime>` as a [Unix timestamp] with milliseconds +/// for the purposes of serde. +/// +/// Use this module in combination with serde's [`#[with]`][with] attribute. +/// +/// When deserializing, the offset is assumed to be UTC. +/// +/// [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time +/// [with]: https://serde.rs/field-attrs.html#with +pub mod option { + #[allow(clippy::wildcard_imports)] + use super::*; + + /// Serialize an `Option<OffsetDateTime>` as its Unix timestamp with milliseconds + pub fn serialize<S: Serializer>( + option: &Option<OffsetDateTime>, + serializer: S, + ) -> Result<S::Ok, S::Error> { + option + .map(|timestamp| (timestamp.unix_timestamp_nanos() / 1_000_000).truncate::<i64>()) + .serialize(serializer) + } + + /// Deserialize an `Option<OffsetDateTime>` from its Unix timestamp with milliseconds + pub fn deserialize<'a, D: Deserializer<'a>>( + deserializer: D, + ) -> Result<Option<OffsetDateTime>, D::Error> { + Option::deserialize(deserializer)? + .map(|value: i64| { + OffsetDateTime::from_unix_timestamp_nanos(value.extend::<i128>() * 1_000_000) + }) + .transpose() + .map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) + } +} diff --git a/vendor/time/src/serde/timestamp/mod.rs b/vendor/time/src/serde/timestamp/mod.rs new file mode 100644 index 00000000..d27836b6 --- /dev/null +++ b/vendor/time/src/serde/timestamp/mod.rs @@ -0,0 +1,65 @@ +//! Treat an [`OffsetDateTime`] as a [Unix timestamp] for the purposes of serde. +//! +//! Use this module in combination with serde's [`#[with]`][with] attribute. +//! +//! When deserializing, the offset is assumed to be UTC. +//! +//! [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time +//! [with]: https://serde.rs/field-attrs.html#with + +pub mod microseconds; +pub mod milliseconds; +pub mod milliseconds_i64; +pub mod nanoseconds; + +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + +use crate::OffsetDateTime; + +/// Serialize an `OffsetDateTime` as its Unix timestamp +pub fn serialize<S: Serializer>( + datetime: &OffsetDateTime, + serializer: S, +) -> Result<S::Ok, S::Error> { + datetime.unix_timestamp().serialize(serializer) +} + +/// Deserialize an `OffsetDateTime` from its Unix timestamp +pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result<OffsetDateTime, D::Error> { + OffsetDateTime::from_unix_timestamp(<_>::deserialize(deserializer)?) + .map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) +} + +/// Treat an `Option<OffsetDateTime>` as a [Unix timestamp] for the purposes of +/// serde. +/// +/// Use this module in combination with serde's [`#[with]`][with] attribute. +/// +/// When deserializing, the offset is assumed to be UTC. +/// +/// [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time +/// [with]: https://serde.rs/field-attrs.html#with +pub mod option { + #[allow(clippy::wildcard_imports)] + use super::*; + + /// Serialize an `Option<OffsetDateTime>` as its Unix timestamp + pub fn serialize<S: Serializer>( + option: &Option<OffsetDateTime>, + serializer: S, + ) -> Result<S::Ok, S::Error> { + option + .map(OffsetDateTime::unix_timestamp) + .serialize(serializer) + } + + /// Deserialize an `Option<OffsetDateTime>` from its Unix timestamp + pub fn deserialize<'a, D: Deserializer<'a>>( + deserializer: D, + ) -> Result<Option<OffsetDateTime>, D::Error> { + Option::deserialize(deserializer)? + .map(OffsetDateTime::from_unix_timestamp) + .transpose() + .map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) + } +} diff --git a/vendor/time/src/serde/timestamp/nanoseconds.rs b/vendor/time/src/serde/timestamp/nanoseconds.rs new file mode 100644 index 00000000..c71d1e7c --- /dev/null +++ b/vendor/time/src/serde/timestamp/nanoseconds.rs @@ -0,0 +1,61 @@ +//! Treat an [`OffsetDateTime`] as a [Unix timestamp] with nanoseconds for +//! the purposes of serde. +//! +//! Use this module in combination with serde's [`#[with]`][with] attribute. +//! +//! When deserializing, the offset is assumed to be UTC. +//! +//! [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time +//! [with]: https://serde.rs/field-attrs.html#with + +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + +use crate::OffsetDateTime; + +/// Serialize an `OffsetDateTime` as its Unix timestamp with nanoseconds +pub fn serialize<S: Serializer>( + datetime: &OffsetDateTime, + serializer: S, +) -> Result<S::Ok, S::Error> { + datetime.unix_timestamp_nanos().serialize(serializer) +} + +/// Deserialize an `OffsetDateTime` from its Unix timestamp with nanoseconds +pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result<OffsetDateTime, D::Error> { + OffsetDateTime::from_unix_timestamp_nanos(<_>::deserialize(deserializer)?) + .map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) +} + +/// Treat an `Option<OffsetDateTime>` as a [Unix timestamp] with nanoseconds +/// for the purposes of serde. +/// +/// Use this module in combination with serde's [`#[with]`][with] attribute. +/// +/// When deserializing, the offset is assumed to be UTC. +/// +/// [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time +/// [with]: https://serde.rs/field-attrs.html#with +pub mod option { + #[allow(clippy::wildcard_imports)] + use super::*; + + /// Serialize an `Option<OffsetDateTime>` as its Unix timestamp with nanoseconds + pub fn serialize<S: Serializer>( + option: &Option<OffsetDateTime>, + serializer: S, + ) -> Result<S::Ok, S::Error> { + option + .map(OffsetDateTime::unix_timestamp_nanos) + .serialize(serializer) + } + + /// Deserialize an `Option<OffsetDateTime>` from its Unix timestamp with nanoseconds + pub fn deserialize<'a, D: Deserializer<'a>>( + deserializer: D, + ) -> Result<Option<OffsetDateTime>, D::Error> { + Option::deserialize(deserializer)? + .map(OffsetDateTime::from_unix_timestamp_nanos) + .transpose() + .map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) + } +} diff --git a/vendor/time/src/serde/visitor.rs b/vendor/time/src/serde/visitor.rs new file mode 100644 index 00000000..b7034020 --- /dev/null +++ b/vendor/time/src/serde/visitor.rs @@ -0,0 +1,355 @@ +//! Serde visitor for various types. + +use core::fmt; +use core::marker::PhantomData; + +use serde::de; +#[cfg(feature = "parsing")] +use serde::Deserializer; + +#[cfg(feature = "parsing")] +use super::{ + DATE_FORMAT, OFFSET_DATE_TIME_FORMAT, PRIMITIVE_DATE_TIME_FORMAT, TIME_FORMAT, + UTC_DATE_TIME_FORMAT, UTC_OFFSET_FORMAT, +}; +use crate::error::ComponentRange; +#[cfg(feature = "parsing")] +use crate::format_description::well_known::*; +use crate::{ + Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcDateTime, UtcOffset, Weekday, +}; + +/// A serde visitor for various types. +pub(super) struct Visitor<T: ?Sized>(pub(super) PhantomData<T>); + +impl<'a> de::Visitor<'a> for Visitor<Date> { + type Value = Date; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a `Date`") + } + + #[cfg(feature = "parsing")] + fn visit_str<E: de::Error>(self, value: &str) -> Result<Date, E> { + Date::parse(value, &DATE_FORMAT).map_err(E::custom) + } + + fn visit_seq<A: de::SeqAccess<'a>>(self, mut seq: A) -> Result<Date, A::Error> { + let year = item!(seq, "year")?; + let ordinal = item!(seq, "day of year")?; + Date::from_ordinal_date(year, ordinal).map_err(ComponentRange::into_de_error) + } +} + +impl<'a> de::Visitor<'a> for Visitor<Duration> { + type Value = Duration; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a `Duration`") + } + + fn visit_str<E: de::Error>(self, value: &str) -> Result<Duration, E> { + let (seconds, nanoseconds) = value.split_once('.').ok_or_else(|| { + de::Error::invalid_value(de::Unexpected::Str(value), &"a decimal point") + })?; + + let seconds = seconds + .parse() + .map_err(|_| de::Error::invalid_value(de::Unexpected::Str(seconds), &"seconds"))?; + let mut nanoseconds = nanoseconds.parse().map_err(|_| { + de::Error::invalid_value(de::Unexpected::Str(nanoseconds), &"nanoseconds") + })?; + + if seconds < 0 + // make sure sign does not disappear when seconds == 0 + || (seconds == 0 && value.starts_with("-")) + { + nanoseconds *= -1; + } + + Ok(Duration::new(seconds, nanoseconds)) + } + + fn visit_seq<A: de::SeqAccess<'a>>(self, mut seq: A) -> Result<Duration, A::Error> { + let seconds = item!(seq, "seconds")?; + let nanoseconds = item!(seq, "nanoseconds")?; + Ok(Duration::new(seconds, nanoseconds)) + } +} + +impl<'a> de::Visitor<'a> for Visitor<OffsetDateTime> { + type Value = OffsetDateTime; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("an `OffsetDateTime`") + } + + #[cfg(feature = "parsing")] + fn visit_str<E: de::Error>(self, value: &str) -> Result<OffsetDateTime, E> { + OffsetDateTime::parse(value, &OFFSET_DATE_TIME_FORMAT).map_err(E::custom) + } + + fn visit_seq<A: de::SeqAccess<'a>>(self, mut seq: A) -> Result<OffsetDateTime, A::Error> { + let year = item!(seq, "year")?; + let ordinal = item!(seq, "day of year")?; + let hour = item!(seq, "hour")?; + let minute = item!(seq, "minute")?; + let second = item!(seq, "second")?; + let nanosecond = item!(seq, "nanosecond")?; + let offset_hours = item!(seq, "offset hours")?; + let offset_minutes = item!(seq, "offset minutes")?; + let offset_seconds = item!(seq, "offset seconds")?; + + Date::from_ordinal_date(year, ordinal) + .and_then(|date| date.with_hms_nano(hour, minute, second, nanosecond)) + .and_then(|datetime| { + UtcOffset::from_hms(offset_hours, offset_minutes, offset_seconds) + .map(|offset| datetime.assume_offset(offset)) + }) + .map_err(ComponentRange::into_de_error) + } +} + +impl<'a> de::Visitor<'a> for Visitor<PrimitiveDateTime> { + type Value = PrimitiveDateTime; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a `PrimitiveDateTime`") + } + + #[cfg(feature = "parsing")] + fn visit_str<E: de::Error>(self, value: &str) -> Result<PrimitiveDateTime, E> { + PrimitiveDateTime::parse(value, &PRIMITIVE_DATE_TIME_FORMAT).map_err(E::custom) + } + + fn visit_seq<A: de::SeqAccess<'a>>(self, mut seq: A) -> Result<PrimitiveDateTime, A::Error> { + let year = item!(seq, "year")?; + let ordinal = item!(seq, "day of year")?; + let hour = item!(seq, "hour")?; + let minute = item!(seq, "minute")?; + let second = item!(seq, "second")?; + let nanosecond = item!(seq, "nanosecond")?; + + Date::from_ordinal_date(year, ordinal) + .and_then(|date| date.with_hms_nano(hour, minute, second, nanosecond)) + .map_err(ComponentRange::into_de_error) + } +} + +impl<'a> de::Visitor<'a> for Visitor<UtcDateTime> { + type Value = UtcDateTime; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a `PrimitiveDateTime`") + } + + #[cfg(feature = "parsing")] + fn visit_str<E: de::Error>(self, value: &str) -> Result<UtcDateTime, E> { + UtcDateTime::parse(value, &UTC_DATE_TIME_FORMAT).map_err(E::custom) + } + + fn visit_seq<A: de::SeqAccess<'a>>(self, mut seq: A) -> Result<UtcDateTime, A::Error> { + let year = item!(seq, "year")?; + let ordinal = item!(seq, "day of year")?; + let hour = item!(seq, "hour")?; + let minute = item!(seq, "minute")?; + let second = item!(seq, "second")?; + let nanosecond = item!(seq, "nanosecond")?; + + Date::from_ordinal_date(year, ordinal) + .and_then(|date| date.with_hms_nano(hour, minute, second, nanosecond)) + .map(UtcDateTime::from_primitive) + .map_err(ComponentRange::into_de_error) + } +} + +impl<'a> de::Visitor<'a> for Visitor<Time> { + type Value = Time; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a `Time`") + } + + #[cfg(feature = "parsing")] + fn visit_str<E: de::Error>(self, value: &str) -> Result<Time, E> { + Time::parse(value, &TIME_FORMAT).map_err(E::custom) + } + + fn visit_seq<A: de::SeqAccess<'a>>(self, mut seq: A) -> Result<Time, A::Error> { + let hour = item!(seq, "hour")?; + let minute = item!(seq, "minute")?; + let second = item!(seq, "second")?; + let nanosecond = item!(seq, "nanosecond")?; + + Time::from_hms_nano(hour, minute, second, nanosecond).map_err(ComponentRange::into_de_error) + } +} + +impl<'a> de::Visitor<'a> for Visitor<UtcOffset> { + type Value = UtcOffset; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a `UtcOffset`") + } + + #[cfg(feature = "parsing")] + fn visit_str<E: de::Error>(self, value: &str) -> Result<UtcOffset, E> { + UtcOffset::parse(value, &UTC_OFFSET_FORMAT).map_err(E::custom) + } + + fn visit_seq<A: de::SeqAccess<'a>>(self, mut seq: A) -> Result<UtcOffset, A::Error> { + let hours = item!(seq, "offset hours")?; + let mut minutes = 0; + let mut seconds = 0; + + if let Ok(Some(min)) = seq.next_element() { + minutes = min; + if let Ok(Some(sec)) = seq.next_element() { + seconds = sec; + } + }; + + UtcOffset::from_hms(hours, minutes, seconds).map_err(ComponentRange::into_de_error) + } +} + +impl de::Visitor<'_> for Visitor<Weekday> { + type Value = Weekday; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a `Weekday`") + } + + fn visit_str<E: de::Error>(self, value: &str) -> Result<Weekday, E> { + match value { + "Monday" => Ok(Weekday::Monday), + "Tuesday" => Ok(Weekday::Tuesday), + "Wednesday" => Ok(Weekday::Wednesday), + "Thursday" => Ok(Weekday::Thursday), + "Friday" => Ok(Weekday::Friday), + "Saturday" => Ok(Weekday::Saturday), + "Sunday" => Ok(Weekday::Sunday), + _ => Err(E::invalid_value(de::Unexpected::Str(value), &"a `Weekday`")), + } + } + + fn visit_u64<E: de::Error>(self, value: u64) -> Result<Weekday, E> { + match value { + 1 => Ok(Weekday::Monday), + 2 => Ok(Weekday::Tuesday), + 3 => Ok(Weekday::Wednesday), + 4 => Ok(Weekday::Thursday), + 5 => Ok(Weekday::Friday), + 6 => Ok(Weekday::Saturday), + 7 => Ok(Weekday::Sunday), + _ => Err(E::invalid_value( + de::Unexpected::Unsigned(value), + &"a value in the range 1..=7", + )), + } + } +} + +impl de::Visitor<'_> for Visitor<Month> { + type Value = Month; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a `Month`") + } + + fn visit_str<E: de::Error>(self, value: &str) -> Result<Month, E> { + match value { + "January" => Ok(Month::January), + "February" => Ok(Month::February), + "March" => Ok(Month::March), + "April" => Ok(Month::April), + "May" => Ok(Month::May), + "June" => Ok(Month::June), + "July" => Ok(Month::July), + "August" => Ok(Month::August), + "September" => Ok(Month::September), + "October" => Ok(Month::October), + "November" => Ok(Month::November), + "December" => Ok(Month::December), + _ => Err(E::invalid_value(de::Unexpected::Str(value), &"a `Month`")), + } + } + + fn visit_u64<E: de::Error>(self, value: u64) -> Result<Month, E> { + match value { + 1 => Ok(Month::January), + 2 => Ok(Month::February), + 3 => Ok(Month::March), + 4 => Ok(Month::April), + 5 => Ok(Month::May), + 6 => Ok(Month::June), + 7 => Ok(Month::July), + 8 => Ok(Month::August), + 9 => Ok(Month::September), + 10 => Ok(Month::October), + 11 => Ok(Month::November), + 12 => Ok(Month::December), + _ => Err(E::invalid_value( + de::Unexpected::Unsigned(value), + &"a value in the range 1..=12", + )), + } + } +} + +/// Implement a visitor for a well-known format. +macro_rules! well_known { + ($article:literal, $name:literal, $($ty:tt)+) => { + #[cfg(feature = "parsing")] + impl de::Visitor<'_> for Visitor<$($ty)+> { + type Value = OffsetDateTime; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(concat!($article, " ", $name, "-formatted `OffsetDateTime`")) + } + + fn visit_str<E: de::Error>(self, value: &str) -> Result<OffsetDateTime, E> { + OffsetDateTime::parse(value, &$($ty)+).map_err(E::custom) + } + } + + #[cfg(feature = "parsing")] + impl<'a> de::Visitor<'a> for Visitor<Option<$($ty)+>> { + type Value = Option<OffsetDateTime>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(concat!( + $article, + " ", + $name, + "-formatted `Option<OffsetDateTime>`" + )) + } + + fn visit_some<D: Deserializer<'a>>( + self, + deserializer: D, + ) -> Result<Option<OffsetDateTime>, D::Error> { + deserializer + .deserialize_any(Visitor::<$($ty)+>(PhantomData)) + .map(Some) + } + + fn visit_none<E: de::Error>(self) -> Result<Option<OffsetDateTime>, E> { + Ok(None) + } + + fn visit_unit<E: de::Error>(self) -> Result<Self::Value, E> { + Ok(None) + } + } + }; +} + +well_known!("an", "RFC2822", Rfc2822); +well_known!("an", "RFC3339", Rfc3339); +well_known!( + "an", + "ISO 8601", + Iso8601::<{ super::iso8601::SERDE_CONFIG }> +); |
