From a09f9ba2a8a3219cddd02ca20f4698e3c3d37d3d Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Fri, 20 Sep 2019 10:50:28 -0400 Subject: [PATCH] Test against serde with no features in both std and no-std --- Cargo.toml | 8 +++----- ci/travis.sh | 5 ++++- src/datetime.rs | 29 ++--------------------------- src/lib.rs | 35 +++++++++++++++++++++++++++++++++++ src/naive/date.rs | 4 ++-- src/naive/datetime.rs | 28 ++++++++++------------------ src/naive/time.rs | 4 +--- 7 files changed, 57 insertions(+), 56 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5135032fe0..3e2e229bb2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,8 +27,6 @@ name = "chrono" default = ["clock", "std"] alloc = [] std = [] -serde_alloc = ["serde/alloc"] -serde_std = ["serde/std"] clock = ["time", "std"] wasmbind = ["wasm-bindgen", "js-sys"] @@ -44,9 +42,9 @@ wasm-bindgen = { version = "0.2", optional = true } js-sys = { version = "0.3", optional = true } # contains FFI bindings for the JS Date API [dev-dependencies] -serde_json = { version = "1" } -serde_derive = { version = "1" } -bincode = { version = "0.8.0" } +serde_json = { version = "1", default-features = false } +serde_derive = { version = "1", default-features = false } +bincode = { version = "1.1.0" } num-iter = { version = "0.1.35", default-features = false } doc-comment = "0.3" diff --git a/ci/travis.sh b/ci/travis.sh index 8b5e067bce..c757b711ec 100755 --- a/ci/travis.sh +++ b/ci/travis.sh @@ -121,6 +121,9 @@ build_and_test_nonwasm() { channel build -v --no-default-features --features std,serde,rustc-serialize TZ=Asia/Katmandu channel test -v --no-default-features --features std,serde,rustc-serialize --lib + channel build -v --no-default-features --features 'serde' + TZ=UTC0 channel test -v --no-default-features --features 'serde' --lib + channel build -v --no-default-features --features 'alloc serde' TZ=UTC0 channel test -v --no-default-features --features 'alloc serde' --lib } @@ -151,7 +154,7 @@ build_core_test() { channel_run rustup target add thumbv6m-none-eabi --toolchain "$CHANNEL" ( cd ci/core-test - channel build -v --features alloc --target thumbv6m-none-eabi + channel build -v --target thumbv6m-none-eabi ) } diff --git a/src/datetime.rs b/src/datetime.rs index 092da60af1..aa61fe4913 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -923,37 +923,12 @@ pub mod rustc_serialize { #[cfg(feature = "serde")] pub mod serde { use core::fmt; - // #[cfg(any(test, feature = "alloc"))] - // use alloc::format; use super::DateTime; #[cfg(feature="clock")] use offset::Local; use offset::{LocalResult, TimeZone, Utc, FixedOffset}; use serdelib::{ser, de}; - - enum SerdeError { - NonExistent { timestamp: V }, - Ambiguous { timestamp: V, min: D, max: D } - } - - impl fmt::Debug for SerdeError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "ChronoSerdeError({})", self) - } - } - - // impl core::error::Error for SerdeError {} - impl fmt::Display for SerdeError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - &SerdeError::NonExistent { ref timestamp } => write!( - f, "value is not a legal timestamp: {}", timestamp), - &SerdeError::Ambiguous { ref timestamp, ref min, ref max } => write!( - f, "value is an ambiguous timestamp: {}, could be either of {}, {}", - timestamp, min, max), - } - } - } + use {SerdeError, ne_timestamp}; // try!-like function to convert a LocalResult into a serde-ish Result fn serde_from(me: LocalResult, ts: &V) -> Result @@ -964,7 +939,7 @@ pub mod serde { { match me { LocalResult::None => Err(E::custom( - SerdeError::NonExistent::<_, u8> { timestamp: ts })), + ne_timestamp(ts))), LocalResult::Ambiguous(min, max) => Err(E::custom( SerdeError::Ambiguous { timestamp: ts, min: min, max: max })), LocalResult::Single(val) => Ok(val) diff --git a/src/lib.rs b/src/lib.rs index ad6710c418..9b684dda98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -520,6 +520,41 @@ pub mod serde { pub use super::datetime::serde::*; } +// Until rust 1.18 there is no "pub(crate)" so to share this we need it in the root + +#[cfg(feature = "serde")] +enum SerdeError { + NonExistent { timestamp: V }, + Ambiguous { timestamp: V, min: D, max: D }, +} + +/// Construct a [`SerdeError::NonExistent`] +#[cfg(feature = "serde")] +fn ne_timestamp(timestamp: T) -> SerdeError { + SerdeError::NonExistent:: { timestamp } +} + +#[cfg(feature = "serde")] +impl fmt::Debug for SerdeError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "ChronoSerdeError({})", self) + } +} + +// impl core::error::Error for SerdeError {} +#[cfg(feature = "serde")] +impl fmt::Display for SerdeError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + &SerdeError::NonExistent { ref timestamp } => write!( + f, "value is not a legal timestamp: {}", timestamp), + &SerdeError::Ambiguous { ref timestamp, ref min, ref max } => write!( + f, "value is an ambiguous timestamp: {}, could be either of {}, {}", + timestamp, min, max), + } + } +} + /// The day of week. /// /// The order of the days of week depends on the context. diff --git a/src/naive/date.rs b/src/naive/date.rs index cbd1350cd4..981e9f4bf1 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -1642,14 +1642,14 @@ mod serde { fn visit_str(self, value: &str) -> Result where E: de::Error { - value.parse().map_err(|err| E::custom(format!("{}", err))) + value.parse().map_err(E::custom) } #[cfg(not(any(feature = "std", test)))] fn visit_str(self, value: &str) -> Result where E: de::Error { - value.parse().map_err(|err| E::custom(err)) + value.parse().map_err(E::custom) } } diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index 60e2a77e4d..2049782203 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -1668,8 +1668,6 @@ pub mod rustc_serialize { #[cfg(feature = "serde")] pub mod serde { use core::fmt; - #[cfg(feature = "alloc")] - use alloc::format; use super::{NaiveDateTime}; use serdelib::{ser, de}; @@ -1708,7 +1706,7 @@ pub mod serde { fn visit_str(self, value: &str) -> Result where E: de::Error { - value.parse().map_err(|err| E::custom(format!("{}", err))) + value.parse().map_err(E::custom) } } @@ -1757,11 +1755,9 @@ pub mod serde { /// ``` pub mod ts_nanoseconds { use core::fmt; - #[cfg(not(any(feature = "std", test)))] - use alloc::format; use serdelib::{ser, de}; - use NaiveDateTime; + use {NaiveDateTime, ne_timestamp}; /// Serialize a UTC datetime into an integer number of nanoseconds since the epoch /// @@ -1854,7 +1850,7 @@ pub mod serde { { NaiveDateTime::from_timestamp_opt(value / 1_000_000_000, (value % 1_000_000_000) as u32) - .ok_or_else(|| E::custom(format!("value is not a legal timestamp: {}", value))) + .ok_or_else(|| E::custom(ne_timestamp(value))) } fn visit_u64(self, value: u64) -> Result @@ -1862,7 +1858,7 @@ pub mod serde { { NaiveDateTime::from_timestamp_opt(value as i64 / 1_000_000_000, (value as i64 % 1_000_000_000) as u32) - .ok_or_else(|| E::custom(format!("value is not a legal timestamp: {}", value))) + .ok_or_else(|| E::custom(ne_timestamp(value))) } } } @@ -1904,11 +1900,9 @@ pub mod serde { /// ``` pub mod ts_milliseconds { use core::fmt; - #[cfg(not(any(feature = "std", test)))] - use alloc::format; use serdelib::{ser, de}; - use NaiveDateTime; + use {NaiveDateTime, ne_timestamp}; /// Serialize a UTC datetime into an integer number of milliseconds since the epoch /// @@ -2001,7 +1995,7 @@ pub mod serde { { NaiveDateTime::from_timestamp_opt(value / 1000, ((value % 1000) * 1_000_000) as u32) - .ok_or_else(|| E::custom(format!("value is not a legal timestamp: {}", value))) + .ok_or_else(|| E::custom(ne_timestamp(value))) } fn visit_u64(self, value: u64) -> Result @@ -2009,7 +2003,7 @@ pub mod serde { { NaiveDateTime::from_timestamp_opt((value / 1000) as i64, ((value % 1000) * 1_000_000) as u32) - .ok_or_else(|| E::custom(format!("value is not a legal timestamp: {}", value))) + .ok_or_else(|| E::custom(ne_timestamp(value))) } } } @@ -2051,11 +2045,9 @@ pub mod serde { /// ``` pub mod ts_seconds { use core::fmt; - #[cfg(not(any(feature = "std", test)))] - use alloc::format; use serdelib::{ser, de}; - use NaiveDateTime; + use {NaiveDateTime, ne_timestamp}; /// Serialize a UTC datetime into an integer number of seconds since the epoch /// @@ -2147,14 +2139,14 @@ pub mod serde { where E: de::Error { NaiveDateTime::from_timestamp_opt(value, 0) - .ok_or_else(|| E::custom(format!("value is not a legal timestamp: {}", value))) + .ok_or_else(|| E::custom(ne_timestamp(value))) } fn visit_u64(self, value: u64) -> Result where E: de::Error { NaiveDateTime::from_timestamp_opt(value as i64, 0) - .ok_or_else(|| E::custom(format!("value is not a legal timestamp: {}", value))) + .ok_or_else(|| E::custom(ne_timestamp(value))) } } } diff --git a/src/naive/time.rs b/src/naive/time.rs index 45b2643a92..c0272c8fe9 100644 --- a/src/naive/time.rs +++ b/src/naive/time.rs @@ -1416,8 +1416,6 @@ mod rustc_serialize { #[cfg(feature = "serde")] mod serde { use core::fmt; - #[cfg(feature = "alloc")] - use alloc::format; use super::NaiveTime; use serdelib::{ser, de}; @@ -1445,7 +1443,7 @@ mod serde { fn visit_str(self, value: &str) -> Result where E: de::Error { - value.parse().map_err(|err| E::custom(format!("{}", err))) + value.parse().map_err(E::custom) } }