Slightly improve serde documentation

This commit is contained in:
Paul Dicker 2024-03-15 11:37:47 +01:00 committed by Paul Dicker
parent c21b359c88
commit 04eb6d346f
4 changed files with 40 additions and 32 deletions

View File

@ -23,10 +23,12 @@ pub struct MicroSecondsTimestampVisitor;
#[derive(Debug)] #[derive(Debug)]
pub struct MilliSecondsTimestampVisitor; pub struct MilliSecondsTimestampVisitor;
/// Serialize into an ISO 8601 formatted string. /// Serialize to an RFC 3339 formatted string
/// ///
/// See [the `serde` module](./serde/index.html) for alternate /// As an extension to RFC 3339 this can serialize `DateTime`s outside the range of 0-9999 years
/// serializations. /// using an ISO 8601 syntax (which prepends an `-` or `+`).
///
/// See [the `serde` module](crate::serde) for alternate serializations.
impl<Tz: TimeZone> ser::Serialize for DateTime<Tz> { impl<Tz: TimeZone> ser::Serialize for DateTime<Tz> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
@ -54,7 +56,7 @@ impl<'de> de::Visitor<'de> for DateTimeVisitor {
type Value = DateTime<FixedOffset>; type Value = DateTime<FixedOffset>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a formatted date and time string or a unix timestamp") formatter.write_str("an RFC 3339 formatted date and time string")
} }
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
@ -65,13 +67,12 @@ impl<'de> de::Visitor<'de> for DateTimeVisitor {
} }
} }
/// Deserialize a value that optionally includes a timezone offset in its /// Deserialize an RFC 3339 formatted string into a `DateTime<FixedOffset>`
/// string representation
/// ///
/// The value to be deserialized must be an rfc3339 string. /// As an extension to RFC 3339 this can deserialize to `DateTime`s outside the range of 0-9999
/// years using an ISO 8601 syntax (which prepends an `-` or `+`).
/// ///
/// See [the `serde` module](./serde/index.html) for alternate /// See [the `serde` module](crate::serde) for alternate deserialization formats.
/// deserialization formats.
impl<'de> de::Deserialize<'de> for DateTime<FixedOffset> { impl<'de> de::Deserialize<'de> for DateTime<FixedOffset> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
@ -81,12 +82,14 @@ impl<'de> de::Deserialize<'de> for DateTime<FixedOffset> {
} }
} }
/// Deserialize into a UTC value /// Deserialize an RFC 3339 formatted string into a `DateTime<Utc>`
/// ///
/// The value to be deserialized must be an rfc3339 string. /// If the value contains an offset from UTC that is not zero, the value will be converted to UTC.
/// ///
/// See [the `serde` module](./serde/index.html) for alternate /// As an extension to RFC 3339 this can deserialize to `DateTime`s outside the range of 0-9999
/// deserialization formats. /// years using an ISO 8601 syntax (which prepends an `-` or `+`).
///
/// See [the `serde` module](crate::serde) for alternate deserialization formats.
impl<'de> de::Deserialize<'de> for DateTime<Utc> { impl<'de> de::Deserialize<'de> for DateTime<Utc> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
@ -96,13 +99,15 @@ impl<'de> de::Deserialize<'de> for DateTime<Utc> {
} }
} }
/// Deserialize a value that includes no timezone in its string /// Deserialize an RFC 3339 formatted string into a `DateTime<Local>`
/// representation
/// ///
/// The value to be deserialized must be an rfc3339 string. /// The value will remain the same instant in UTC, but the offset will be recalculated to match
/// that of the `Local` platform time zone.
/// ///
/// See [the `serde` module](./serde/index.html) for alternate /// As an extension to RFC 3339 this can deserialize to `DateTime`s outside the range of 0-9999
/// serialization formats. /// years using an ISO 8601 syntax (which prepends an `-` or `+`).
///
/// See [the `serde` module](crate::serde) for alternate deserialization formats.
#[cfg(feature = "clock")] #[cfg(feature = "clock")]
impl<'de> de::Deserialize<'de> for DateTime<Local> { impl<'de> de::Deserialize<'de> for DateTime<Local> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>

View File

@ -614,15 +614,21 @@ pub use traits::{Datelike, Timelike};
#[doc(hidden)] #[doc(hidden)]
pub use naive::__BenchYearFlags; pub use naive::__BenchYearFlags;
/// Serialization/Deserialization with serde. /// Serialization/Deserialization with serde
/// ///
/// This module provides default implementations for `DateTime` using the [RFC 3339][1] format and various /// The [`DateTime`] type has default implementations for (de)serializing to/from the [RFC 3339]
/// alternatives for use with serde's [`with` annotation][2]. /// format. This module provides alternatives for serializing to timestamps.
///
/// The alternatives are for use with serde's [`with` annotation] combined with the module name.
/// Alternatively the individual `serialize` and `deserialize` functions in each module can be used
/// with serde's [`serialize_with`] and [`deserialize_with`] annotations.
/// ///
/// *Available on crate feature 'serde' only.* /// *Available on crate feature 'serde' only.*
/// ///
/// [1]: https://tools.ietf.org/html/rfc3339 /// [RFC 3339]: https://tools.ietf.org/html/rfc3339
/// [2]: https://serde.rs/field-attrs.html#with /// [`with` annotation]: https://serde.rs/field-attrs.html#with
/// [`serialize_with`]: https://serde.rs/field-attrs.html#serialize_with
/// [`deserialize_with`]: https://serde.rs/field-attrs.html#deserialize_with
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
pub mod serde { pub mod serde {
use core::fmt; use core::fmt;

View File

@ -3,10 +3,9 @@ use serde::{de, ser};
use super::NaiveDateTime; use super::NaiveDateTime;
/// Serialize a `NaiveDateTime` as an RFC 3339 string /// Serialize a `NaiveDateTime` as an ISO 8601 string
/// ///
/// See [the `serde` module](./serde/index.html) for alternate /// See [the `naive::serde` module](crate::naive::serde) for alternate serialization formats.
/// serialization formats.
impl ser::Serialize for NaiveDateTime { impl ser::Serialize for NaiveDateTime {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where

View File

@ -141,14 +141,12 @@ impl Days {
} }
} }
/// Serialization/Deserialization of naive types in alternate formats /// Serialization/Deserialization of `NaiveDateTime` in alternate formats
/// ///
/// The various modules in here are intended to be used with serde's [`with` /// The various modules in here are intended to be used with serde's [`with` annotation] to
/// annotation][1] to serialize as something other than the default [RFC /// serialize as something other than the default ISO 8601 format.
/// 3339][2] format.
/// ///
/// [1]: https://serde.rs/attributes.html#field-attributes /// [`with` annotation]: https://serde.rs/field-attrs.html#with
/// [2]: https://tools.ietf.org/html/rfc3339
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
pub mod serde { pub mod serde {
pub use super::datetime::serde::*; pub use super::datetime::serde::*;