diff --git a/src/datetime/serde.rs b/src/datetime/serde.rs index e9f5d7e3..0d6deba4 100644 --- a/src/datetime/serde.rs +++ b/src/datetime/serde.rs @@ -3,7 +3,6 @@ use serde::{de, ser}; use super::DateTime; use crate::format::{write_rfc3339, SecondsFormat}; -use crate::naive::datetime::serde::serde_from; #[cfg(feature = "clock")] use crate::offset::Local; use crate::offset::{FixedOffset, Offset, TimeZone, Utc}; @@ -148,10 +147,10 @@ pub mod ts_nanoseconds { use core::fmt; use serde::{de, ser}; - use crate::offset::TimeZone; - use crate::{DateTime, Utc}; + use crate::serde::serde_from; + use crate::{DateTime, TimeZone, Utc}; - use super::{serde_from, NanoSecondsTimestampVisitor}; + use super::NanoSecondsTimestampVisitor; /// Serialize a UTC datetime into an integer number of nanoseconds since the epoch /// @@ -448,9 +447,10 @@ pub mod ts_microseconds { use core::fmt; use serde::{de, ser}; - use super::{serde_from, MicroSecondsTimestampVisitor}; - use crate::offset::TimeZone; - use crate::{DateTime, Utc}; + use crate::serde::serde_from; + use crate::{DateTime, TimeZone, Utc}; + + use super::MicroSecondsTimestampVisitor; /// Serialize a UTC datetime into an integer number of microseconds since the epoch /// @@ -726,9 +726,10 @@ pub mod ts_milliseconds { use core::fmt; use serde::{de, ser}; - use super::{serde_from, MilliSecondsTimestampVisitor}; - use crate::offset::TimeZone; - use crate::{DateTime, Utc}; + use crate::serde::serde_from; + use crate::{DateTime, TimeZone, Utc}; + + use super::MilliSecondsTimestampVisitor; /// Serialize a UTC datetime into an integer number of milliseconds since the epoch /// @@ -1005,9 +1006,11 @@ pub mod ts_seconds { use core::fmt; use serde::{de, ser}; - use super::{serde_from, SecondsTimestampVisitor}; + use crate::serde::serde_from; use crate::{DateTime, LocalResult, TimeZone, Utc}; + use super::SecondsTimestampVisitor; + /// Serialize a UTC datetime into an integer number of seconds since the epoch /// /// Intended for use with `serde`s `serialize_with` attribute. diff --git a/src/lib.rs b/src/lib.rs index 1cc1a365..5366c560 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -629,7 +629,59 @@ pub use naive::__BenchYearFlags; /// [2]: https://serde.rs/field-attrs.html#with #[cfg(feature = "serde")] pub mod serde { + use crate::offset::LocalResult; + use core::fmt; + use serde::de; + pub use super::datetime::serde::*; + + // lik? function to convert a LocalResult into a serde-ish Result + pub(crate) fn serde_from(me: LocalResult, ts: &V) -> Result + where + E: de::Error, + V: fmt::Display, + T: fmt::Display, + { + match me { + LocalResult::None => Err(E::custom(ne_timestamp(ts))), + LocalResult::Ambiguous(min, max) => { + Err(E::custom(SerdeError::Ambiguous { timestamp: ts, min, max })) + } + LocalResult::Single(val) => Ok(val), + } + } + + pub(crate) enum SerdeError { + NonExistent { timestamp: V }, + Ambiguous { timestamp: V, min: D, max: D }, + } + + /// Construct a [`SerdeError::NonExistent`] + pub(crate) fn ne_timestamp(ts: T) -> SerdeError { + SerdeError::NonExistent:: { timestamp: ts } + } + + impl fmt::Debug for SerdeError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "ChronoSerdeError({})", self) + } + } + + // impl core::error::Error for SerdeError {} + impl fmt::Display for SerdeError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + SerdeError::NonExistent { timestamp } => { + write!(f, "value is not a legal timestamp: {}", timestamp) + } + SerdeError::Ambiguous { timestamp, min, max } => write!( + f, + "value is an ambiguous timestamp: {}, could be either of {}, {}", + timestamp, min, max + ), + } + } + } } /// Zero-copy serialization/deserialization with rkyv. diff --git a/src/naive/datetime/serde.rs b/src/naive/datetime/serde.rs index 16088e36..0eb562ea 100644 --- a/src/naive/datetime/serde.rs +++ b/src/naive/datetime/serde.rs @@ -2,7 +2,6 @@ use core::fmt; use serde::{de, ser}; use super::NaiveDateTime; -use crate::offset::LocalResult; /// Serialize a `NaiveDateTime` as an RFC 3339 string /// @@ -83,7 +82,7 @@ pub mod ts_nanoseconds { use core::fmt; use serde::{de, ser}; - use super::ne_timestamp; + use crate::serde::ne_timestamp; use crate::NaiveDateTime; /// Serialize a datetime into an integer number of nanoseconds since the epoch @@ -372,7 +371,7 @@ pub mod ts_microseconds { use core::fmt; use serde::{de, ser}; - use super::ne_timestamp; + use crate::serde::ne_timestamp; use crate::NaiveDateTime; /// Serialize a datetime into an integer number of microseconds since the epoch @@ -636,7 +635,7 @@ pub mod ts_milliseconds { use core::fmt; use serde::{de, ser}; - use super::ne_timestamp; + use crate::serde::ne_timestamp; use crate::NaiveDateTime; /// Serialize a datetime into an integer number of milliseconds since the epoch @@ -896,7 +895,7 @@ pub mod ts_seconds { use core::fmt; use serde::{de, ser}; - use super::ne_timestamp; + use crate::serde::ne_timestamp; use crate::NaiveDateTime; /// Serialize a datetime into an integer number of seconds since the epoch @@ -1109,54 +1108,6 @@ pub mod ts_seconds_option { } } -// lik? function to convert a LocalResult into a serde-ish Result -pub(crate) fn serde_from(me: LocalResult, ts: &V) -> Result -where - E: de::Error, - V: fmt::Display, - T: fmt::Display, -{ - match me { - LocalResult::None => Err(E::custom(ne_timestamp(ts))), - LocalResult::Ambiguous(min, max) => { - Err(E::custom(SerdeError::Ambiguous { timestamp: ts, min, max })) - } - LocalResult::Single(val) => Ok(val), - } -} - -enum SerdeError { - NonExistent { timestamp: V }, - Ambiguous { timestamp: V, min: D, max: D }, -} - -/// Construct a [`SerdeError::NonExistent`] -fn ne_timestamp(ts: T) -> SerdeError { - SerdeError::NonExistent:: { timestamp: ts } -} - -impl fmt::Debug for SerdeError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "ChronoSerdeError({})", self) - } -} - -// impl core::error::Error for SerdeError {} -impl fmt::Display for SerdeError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - SerdeError::NonExistent { timestamp } => { - write!(f, "value is not a legal timestamp: {}", timestamp) - } - SerdeError::Ambiguous { timestamp, min, max } => write!( - f, - "value is an ambiguous timestamp: {}, could be either of {}, {}", - timestamp, min, max - ), - } - } -} - #[cfg(test)] mod tests { use crate::naive::datetime::{test_decodable_json, test_encodable_json};