Move serde_from and SerdeError to crate::serde inline module

This commit is contained in:
Paul Dicker 2024-02-27 14:01:49 +01:00 committed by Paul Dicker
parent 85c70ffad9
commit 0b68d19a3e
3 changed files with 70 additions and 64 deletions

View File

@ -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.

View File

@ -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<T, E, V>(me: LocalResult<T>, ts: &V) -> Result<T, E>
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<V: fmt::Display, D: fmt::Display> {
NonExistent { timestamp: V },
Ambiguous { timestamp: V, min: D, max: D },
}
/// Construct a [`SerdeError::NonExistent`]
pub(crate) fn ne_timestamp<T: fmt::Display>(ts: T) -> SerdeError<T, u8> {
SerdeError::NonExistent::<T, u8> { timestamp: ts }
}
impl<V: fmt::Display, D: fmt::Display> fmt::Debug for SerdeError<V, D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ChronoSerdeError({})", self)
}
}
// impl<V: fmt::Display, D: fmt::Debug> core::error::Error for SerdeError<V, D> {}
impl<V: fmt::Display, D: fmt::Display> fmt::Display for SerdeError<V, D> {
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.

View File

@ -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<T, E, V>(me: LocalResult<T>, ts: &V) -> Result<T, E>
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<V: fmt::Display, D: fmt::Display> {
NonExistent { timestamp: V },
Ambiguous { timestamp: V, min: D, max: D },
}
/// Construct a [`SerdeError::NonExistent`]
fn ne_timestamp<T: fmt::Display>(ts: T) -> SerdeError<T, u8> {
SerdeError::NonExistent::<T, u8> { timestamp: ts }
}
impl<V: fmt::Display, D: fmt::Display> fmt::Debug for SerdeError<V, D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ChronoSerdeError({})", self)
}
}
// impl<V: fmt::Display, D: fmt::Debug> core::error::Error for SerdeError<V, D> {}
impl<V: fmt::Display, D: fmt::Display> fmt::Display for SerdeError<V, D> {
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};