Skip to content

Commit

Permalink
Loosen features required for well-known formats
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Oct 4, 2022
1 parent 110e17b commit 4af896f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
12 changes: 11 additions & 1 deletion src/serde/iso8601.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
//! [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 _;
use serde::{Deserializer, Serialize, Serializer};
#[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;
Expand All @@ -20,6 +26,7 @@ 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,
Expand All @@ -31,6 +38,7 @@ pub fn serialize<S: 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_any(Visitor::<Iso8601<SERDE_CONFIG>>(PhantomData))
}
Expand All @@ -47,6 +55,7 @@ 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,
Expand All @@ -59,6 +68,7 @@ pub mod option {
}

/// 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> {
Expand Down
8 changes: 4 additions & 4 deletions src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ macro_rules! item {
};
}

#[cfg(feature = "serde-human-readable")]
#[cfg(any(feature = "formatting", feature = "parsing"))]
pub mod iso8601;
#[cfg(feature = "serde-human-readable")]
#[cfg(any(feature = "formatting", feature = "parsing"))]
pub mod rfc2822;
#[cfg(feature = "serde-human-readable")]
#[cfg(any(feature = "formatting", feature = "parsing"))]
pub mod rfc3339;
pub mod timestamp;
mod visitor;
Expand Down Expand Up @@ -66,7 +66,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// ```
///
/// [`format_description::parse()`]: crate::format_description::parse()
#[cfg(all(feature = "macros", feature = "serde-human-readable"))]
#[cfg(all(feature = "macros", any(feature = "formatting", feature = "parsing"),))]
pub use time_macros::serde_format_description as format_description;

use self::visitor::Visitor;
Expand Down
12 changes: 11 additions & 1 deletion src/serde/rfc2822.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@
//! [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 _;
use serde::{Deserializer, Serialize, Serializer};
#[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,
Expand All @@ -26,6 +33,7 @@ pub fn serialize<S: 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))
}
Expand All @@ -42,6 +50,7 @@ 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,
Expand All @@ -54,6 +63,7 @@ pub mod option {
}

/// 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> {
Expand Down
12 changes: 11 additions & 1 deletion src/serde/rfc3339.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@
//! [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 _;
use serde::{Deserializer, Serialize, Serializer};
#[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,
Expand All @@ -26,6 +33,7 @@ pub fn serialize<S: 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))
}
Expand All @@ -42,6 +50,7 @@ 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,
Expand All @@ -54,6 +63,7 @@ pub mod option {
}

/// 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> {
Expand Down
8 changes: 4 additions & 4 deletions src/serde/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::fmt;
use core::marker::PhantomData;

use serde::de;
#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "parsing")]
use serde::Deserializer;

#[cfg(feature = "parsing")]
Expand All @@ -13,7 +13,7 @@ use super::{
UTC_OFFSET_FORMAT,
};
use crate::error::ComponentRange;
#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "parsing")]
use crate::format_description::well_known::*;
use crate::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset, Weekday};

Expand Down Expand Up @@ -261,7 +261,7 @@ impl<'a> de::Visitor<'a> for Visitor<Month> {
/// Implement a visitor for a well-known format.
macro_rules! well_known {
($article:literal, $name:literal, $($ty:tt)+) => {
#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "parsing")]
impl<'a> de::Visitor<'a> for Visitor<$($ty)+> {
type Value = OffsetDateTime;

Expand All @@ -274,7 +274,7 @@ macro_rules! well_known {
}
}

#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "parsing")]
impl<'a> de::Visitor<'a> for Visitor<Option<$($ty)+>> {
type Value = Option<OffsetDateTime>;

Expand Down

0 comments on commit 4af896f

Please sign in to comment.