Skip to content

Commit

Permalink
Test against serde with no features in both std and no-std
Browse files Browse the repository at this point in the history
  • Loading branch information
quodlibetor committed Sep 20, 2019
1 parent e5bbc94 commit a09f9ba
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 56 deletions.
8 changes: 3 additions & 5 deletions Cargo.toml
Expand Up @@ -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"]

Expand All @@ -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"

Expand Down
5 changes: 4 additions & 1 deletion ci/travis.sh
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
)
}

Expand Down
29 changes: 2 additions & 27 deletions src/datetime.rs
Expand Up @@ -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<V: fmt::Display, D: fmt::Display> {
NonExistent { timestamp: V },
Ambiguous { timestamp: V, min: D, max: D }
}

impl<V: fmt::Display, D: fmt::Display> fmt::Debug for SerdeError<V, D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ChronoSerdeError({})", self)
}
}

// impl<V: fmt::Display, D: fmt::Debug> core::error::Error for SerdeError<V, D> {}
impl<V: fmt::Display, D: fmt::Display> fmt::Display for SerdeError<V, D> {
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<T, E, V>(me: LocalResult<T>, ts: &V) -> Result<T, E>
Expand All @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions src/lib.rs
Expand Up @@ -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<V: fmt::Display, D: fmt::Display> {
NonExistent { timestamp: V },
Ambiguous { timestamp: V, min: D, max: D },
}

/// Construct a [`SerdeError::NonExistent`]
#[cfg(feature = "serde")]
fn ne_timestamp<T: fmt::Display>(timestamp: T) -> SerdeError<T, u8> {
SerdeError::NonExistent::<T, u8> { timestamp }
}

#[cfg(feature = "serde")]
impl<V: fmt::Display, D: fmt::Display> fmt::Debug for SerdeError<V, D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ChronoSerdeError({})", self)
}
}

// impl<V: fmt::Display, D: fmt::Debug> core::error::Error for SerdeError<V, D> {}
#[cfg(feature = "serde")]
impl<V: fmt::Display, D: fmt::Display> fmt::Display for SerdeError<V, D> {
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.
Expand Down
4 changes: 2 additions & 2 deletions src/naive/date.rs
Expand Up @@ -1642,14 +1642,14 @@ mod serde {
fn visit_str<E>(self, value: &str) -> Result<NaiveDate, E>
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<E>(self, value: &str) -> Result<NaiveDate, E>
where E: de::Error
{
value.parse().map_err(|err| E::custom(err))
value.parse().map_err(E::custom)
}
}

Expand Down
28 changes: 10 additions & 18 deletions src/naive/datetime.rs
Expand Up @@ -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};

Expand Down Expand Up @@ -1708,7 +1706,7 @@ pub mod serde {
fn visit_str<E>(self, value: &str) -> Result<NaiveDateTime, E>
where E: de::Error
{
value.parse().map_err(|err| E::custom(format!("{}", err)))
value.parse().map_err(E::custom)
}
}

Expand Down Expand Up @@ -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
///
Expand Down Expand Up @@ -1854,15 +1850,15 @@ 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<E>(self, value: u64) -> Result<NaiveDateTime, E>
where E: de::Error
{
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)))
}
}
}
Expand Down Expand Up @@ -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
///
Expand Down Expand Up @@ -2001,15 +1995,15 @@ 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<E>(self, value: u64) -> Result<NaiveDateTime, E>
where E: de::Error
{
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)))
}
}
}
Expand Down Expand Up @@ -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
///
Expand Down Expand Up @@ -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<E>(self, value: u64) -> Result<NaiveDateTime, E>
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)))
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/naive/time.rs
Expand Up @@ -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};

Expand Down Expand Up @@ -1445,7 +1443,7 @@ mod serde {
fn visit_str<E>(self, value: &str) -> Result<NaiveTime, E>
where E: de::Error
{
value.parse().map_err(|err| E::custom(format!("{}", err)))
value.parse().map_err(E::custom)
}
}

Expand Down

0 comments on commit a09f9ba

Please sign in to comment.