//! 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( datetime: &OffsetDateTime, serializer: S, ) -> Result { 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 { deserializer.deserialize_str(Visitor::(PhantomData)) } /// Use the well-known [RFC3339 format] when serializing and deserializing an /// [`Option`]. /// /// 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 { #[allow(clippy::wildcard_imports)] use super::*; /// Serialize an [`Option`] using the well-known RFC3339 format. #[cfg(feature = "formatting")] pub fn serialize( option: &Option, serializer: S, ) -> Result { option .map(|odt| odt.format(&Rfc3339)) .transpose() .map_err(S::Error::custom)? .serialize(serializer) } /// Deserialize an [`Option`] from its RFC3339 representation. #[cfg(feature = "parsing")] pub fn deserialize<'a, D: Deserializer<'a>>( deserializer: D, ) -> Result, D::Error> { deserializer.deserialize_option(Visitor::>(PhantomData)) } }