Test against serde with no features in both std and no-std

This commit is contained in:
Brandon W Maister 2019-09-20 10:50:28 -04:00
parent e5bbc94c3b
commit a09f9ba2a8
7 changed files with 57 additions and 56 deletions

View File

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

View File

@ -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
)
}

View File

@ -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>
@ -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)

View File

@ -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.

View File

@ -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)
}
}

View File

@ -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<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)
}
}
@ -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<E>(self, value: u64) -> Result<NaiveDateTime, E>
@ -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<E>(self, value: u64) -> Result<NaiveDateTime, E>
@ -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<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)))
}
}
}

View File

@ -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<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)
}
}