Skip to content

Commit

Permalink
Use more correct feature gates
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Dec 23, 2021
1 parent eef9baf commit f6baa3f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
//! serializer, not the user. If this feature is not enabled or if the serializer requests a
//! non-human-readable format, a format optimized for binary representation will be used.
//!
//! Libraries should never enable this feature, as the decision of what format to use should be up
//! to the user.
//!
//! - `serde-well-known` (_implicitly enables `serde/alloc`, `formatting`, and `parsing`_)
//!
//! Enables support for serializing and deserializing well-known formats using serde's
Expand Down
12 changes: 6 additions & 6 deletions src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ use serde::ser::Error as _;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use self::visitor::Visitor;
#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "parsing")]
use crate::format_description::{modifier, Component, FormatItem};
use crate::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset, Weekday};

// region: Date
/// The format used when serializing and deserializing a human-readable `Date`.
#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "parsing")]
const DATE_FORMAT: &[FormatItem<'_>] = &[
FormatItem::Component(Component::Year(modifier::Year::default())),
FormatItem::Literal(b"-"),
Expand Down Expand Up @@ -88,7 +88,7 @@ impl<'a> Deserialize<'a> for Duration {

// region: OffsetDateTime
/// The format used when serializing and deserializing a human-readable `OffsetDateTime`.
#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "parsing")]
const OFFSET_DATE_TIME_FORMAT: &[FormatItem<'_>] = &[
FormatItem::Compound(DATE_FORMAT),
FormatItem::Literal(b" "),
Expand Down Expand Up @@ -131,7 +131,7 @@ impl<'a> Deserialize<'a> for OffsetDateTime {

// region: PrimitiveDateTime
/// The format used when serializing and deserializing a human-readable `PrimitiveDateTime`.
#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "parsing")]
const PRIMITIVE_DATE_TIME_FORMAT: &[FormatItem<'_>] = &[
FormatItem::Compound(DATE_FORMAT),
FormatItem::Literal(b" "),
Expand Down Expand Up @@ -169,7 +169,7 @@ impl<'a> Deserialize<'a> for PrimitiveDateTime {

// region: Time
/// The format used when serializing and deserializing a human-readable `Time`.
#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "parsing")]
const TIME_FORMAT: &[FormatItem<'_>] = &[
FormatItem::Component(Component::Hour(<modifier::Hour>::default())),
FormatItem::Literal(b":"),
Expand Down Expand Up @@ -203,7 +203,7 @@ impl<'a> Deserialize<'a> for Time {

// region: UtcOffset
/// The format used when serializing and deserializing a human-readable `UtcOffset`.
#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "parsing")]
const UTC_OFFSET_FORMAT: &[FormatItem<'_>] = &[
FormatItem::Component(Component::OffsetHour(modifier::OffsetHour::default())),
FormatItem::Literal(b":"),
Expand Down
19 changes: 8 additions & 11 deletions src/serde/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use core::fmt;
use core::marker::PhantomData;

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

#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "parsing")]
use super::{
DATE_FORMAT, OFFSET_DATE_TIME_FORMAT, PRIMITIVE_DATE_TIME_FORMAT, TIME_FORMAT,
UTC_OFFSET_FORMAT,
};
use crate::error::ComponentRange;
#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "serde-well-known")]
use crate::format_description::well_known;
use crate::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset, Weekday};

Expand All @@ -27,7 +27,7 @@ impl<'a> de::Visitor<'a> for Visitor<Date> {
formatter.write_str("a `Date`")
}

#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "parsing")]
fn visit_str<E: de::Error>(self, value: &str) -> Result<Date, E> {
Date::parse(value, &DATE_FORMAT).map_err(E::custom)
}
Expand All @@ -46,7 +46,6 @@ impl<'a> de::Visitor<'a> for Visitor<Duration> {
formatter.write_str("a `Duration`")
}

#[cfg(feature = "serde-human-readable")]
fn visit_str<E: de::Error>(self, value: &str) -> Result<Duration, E> {
let (seconds, nanoseconds) = value.split_once('.').ok_or_else(|| {
de::Error::invalid_value(de::Unexpected::Str(value), &"a decimal point")
Expand Down Expand Up @@ -80,7 +79,7 @@ impl<'a> de::Visitor<'a> for Visitor<OffsetDateTime> {
formatter.write_str("an `OffsetDateTime`")
}

#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "parsing")]
fn visit_str<E: de::Error>(self, value: &str) -> Result<OffsetDateTime, E> {
OffsetDateTime::parse(value, &OFFSET_DATE_TIME_FORMAT).map_err(E::custom)
}
Expand Down Expand Up @@ -113,7 +112,7 @@ impl<'a> de::Visitor<'a> for Visitor<PrimitiveDateTime> {
formatter.write_str("a `PrimitiveDateTime`")
}

#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "parsing")]
fn visit_str<E: de::Error>(self, value: &str) -> Result<PrimitiveDateTime, E> {
PrimitiveDateTime::parse(value, &PRIMITIVE_DATE_TIME_FORMAT).map_err(E::custom)
}
Expand All @@ -139,7 +138,7 @@ impl<'a> de::Visitor<'a> for Visitor<Time> {
formatter.write_str("a `Time`")
}

#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "parsing")]
fn visit_str<E: de::Error>(self, value: &str) -> Result<Time, E> {
Time::parse(value, &TIME_FORMAT).map_err(E::custom)
}
Expand All @@ -161,7 +160,7 @@ impl<'a> de::Visitor<'a> for Visitor<UtcOffset> {
formatter.write_str("a `UtcOffset`")
}

#[cfg(feature = "serde-human-readable")]
#[cfg(feature = "parsing")]
fn visit_str<E: de::Error>(self, value: &str) -> Result<UtcOffset, E> {
UtcOffset::parse(value, &UTC_OFFSET_FORMAT).map_err(E::custom)
}
Expand All @@ -182,7 +181,6 @@ impl<'a> de::Visitor<'a> for Visitor<Weekday> {
formatter.write_str("a `Weekday`")
}

#[cfg(feature = "serde-human-readable")]
fn visit_str<E: de::Error>(self, value: &str) -> Result<Weekday, E> {
match value {
"Monday" => Ok(Weekday::Monday),
Expand Down Expand Up @@ -220,7 +218,6 @@ impl<'a> de::Visitor<'a> for Visitor<Month> {
formatter.write_str("a `Month`")
}

#[cfg(feature = "serde-human-readable")]
fn visit_str<E: de::Error>(self, value: &str) -> Result<Month, E> {
match value {
"January" => Ok(Month::January),
Expand Down

0 comments on commit f6baa3f

Please sign in to comment.