Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow spaces (and other things) as separators when parsing RFC3339 #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,28 @@ single-use-lifetimes = "warn"
trivial-casts = "warn"
trivial-numeric-casts = "warn"
unreachable-pub = "warn"
unstable-name-collisions = { level = "warn", priority = 1 } # overrides #![deny(future_incompatible)]
unused = "warn"
unused = { level = "warn", priority = -1 }
unused-import-braces = "warn"
unused-lifetimes = "warn"
unused-qualifications = "warn"
# unused-results = "warn"
variant-size-differences = "warn"

unstable-name-collisions = { level = "allow", priority = 1 } # overrides #![deny(future_incompatible)], temporary while `.cast_{un}signed()` is unstable

[workspace.lints.clippy]
alloc-instead-of-core = "deny"
std-instead-of-core = "deny"
undocumented-unsafe-blocks = "deny"

all = "warn"
missing-docs-in-private-items = "warn"
all = { level = "warn", priority = -1 }
dbg-macro = "warn"
decimal-literal-representation = "warn"
explicit-auto-deref = "warn"
get-unwrap = "warn"
manual-let-else = "warn"
missing-docs-in-private-items = "warn"
missing-enforced-import-renames = "warn"
nursery = "warn"
nursery = { level = "warn", priority = -1 }
obfuscated-if-else = "warn"
print-stdout = "warn"
semicolon-outside-block = "warn"
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ macro_rules! bench_rand {
iter_batched_ref!(
ben,
|| StepRng::new(0, 1),
[|rng| rng.gen::<$type>()]
[|rng| rng.r#gen::<$type>()]
);
})*
}
Expand Down
1 change: 0 additions & 1 deletion tests/duration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core::u64;
use std::cmp::Ordering;
use std::cmp::Ordering::{Equal, Greater, Less};
use std::time::Duration as StdDuration;
Expand Down
2 changes: 1 addition & 1 deletion tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn run_with_all_features() -> Result<(), Box<dyn std::error::Error>> {
impl std::error::Error for Error {}

let status = std::process::Command::new("cargo")
.args(&["test", "--all-features"])
.args(["test", "--all-features"])
.status()?;

return if status.success() {
Expand Down
18 changes: 14 additions & 4 deletions tests/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,16 @@ fn rfc_3339() -> time::Result<()> {
offset!(-00:01),
);

// Any separator is allowed by RFC 3339, not just `T`.
assert_eq!(
OffsetDateTime::parse("2021-01-02 03:04:05Z", &Rfc3339)?,
datetime!(2021-01-02 03:04:05 UTC),
);
assert_eq!(
OffsetDateTime::parse("2021-01-02$03:04:05Z", &Rfc3339)?,
datetime!(2021-01-02 03:04:05 UTC),
);

Ok(())
}

Expand Down Expand Up @@ -354,8 +364,8 @@ fn rfc_3339_err() {
invalid_component!("day")
));
assert!(matches!(
PrimitiveDateTime::parse("2021-01-01x", &Rfc3339),
invalid_literal!()
PrimitiveDateTime::parse("2021-01-01", &Rfc3339),
invalid_component!("separator")
));
assert!(matches!(
PrimitiveDateTime::parse("2021-01-01T0", &Rfc3339),
Expand Down Expand Up @@ -448,8 +458,8 @@ fn rfc_3339_err() {
invalid_component!("day")
));
assert!(matches!(
OffsetDateTime::parse("2021-01-01x", &Rfc3339),
invalid_literal!()
OffsetDateTime::parse("2021-01-01", &Rfc3339),
invalid_component!("separator")
));
assert!(matches!(
OffsetDateTime::parse("2021-01-01T0", &Rfc3339),
Expand Down
16 changes: 8 additions & 8 deletions tests/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ fn support() {
let mut rng = rand::rngs::mock::StepRng::new(0, 656_175_560);

for _ in 0..7 {
let _ = rng.gen::<Weekday>();
let _ = rng.r#gen::<Weekday>();
}
for _ in 0..12 {
let _ = rng.gen::<Month>();
let _ = rng.r#gen::<Month>();
}
let _ = rng.gen::<Time>();
let _ = rng.gen::<Date>();
let _ = rng.gen::<UtcOffset>();
let _ = rng.gen::<PrimitiveDateTime>();
let _ = rng.gen::<OffsetDateTime>();
let _ = rng.gen::<Duration>();
let _ = rng.r#gen::<Time>();
let _ = rng.r#gen::<Date>();
let _ = rng.r#gen::<UtcOffset>();
let _ = rng.r#gen::<PrimitiveDateTime>();
let _ = rng.r#gen::<OffsetDateTime>();
let _ = rng.r#gen::<Duration>();
}
8 changes: 4 additions & 4 deletions time/src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ impl Duration {
/// ```rust
/// # use time::{Duration, ext::NumericalDuration};
/// assert_eq!(Duration::seconds_f64(0.5), 0.5.seconds());
/// assert_eq!(Duration::seconds_f64(-0.5), -0.5.seconds());
/// assert_eq!(Duration::seconds_f64(-0.5), (-0.5).seconds());
/// ```
pub fn seconds_f64(seconds: f64) -> Self {
try_from_secs!(
Expand Down Expand Up @@ -564,7 +564,7 @@ impl Duration {
/// ```rust
/// # use time::{Duration, ext::NumericalDuration};
/// assert_eq!(Duration::saturating_seconds_f64(0.5), 0.5.seconds());
/// assert_eq!(Duration::saturating_seconds_f64(-0.5), -0.5.seconds());
/// assert_eq!(Duration::saturating_seconds_f64(-0.5), (-0.5).seconds());
/// assert_eq!(
/// Duration::saturating_seconds_f64(f64::NAN),
/// Duration::new(0, 0),
Expand Down Expand Up @@ -637,7 +637,7 @@ impl Duration {
/// ```rust
/// # use time::{Duration, ext::NumericalDuration};
/// assert_eq!(Duration::checked_seconds_f64(0.5), Some(0.5.seconds()));
/// assert_eq!(Duration::checked_seconds_f64(-0.5), Some(-0.5.seconds()));
/// assert_eq!(Duration::checked_seconds_f64(-0.5), Some((-0.5).seconds()));
/// assert_eq!(Duration::checked_seconds_f64(f64::NAN), None);
/// assert_eq!(Duration::checked_seconds_f64(f64::NEG_INFINITY), None);
/// assert_eq!(Duration::checked_seconds_f64(f64::INFINITY), None);
Expand All @@ -664,7 +664,7 @@ impl Duration {
/// ```rust
/// # use time::{Duration, ext::NumericalDuration};
/// assert_eq!(Duration::checked_seconds_f32(0.5), Some(0.5.seconds()));
/// assert_eq!(Duration::checked_seconds_f32(-0.5), Some(-0.5.seconds()));
/// assert_eq!(Duration::checked_seconds_f32(-0.5), Some((-0.5).seconds()));
/// assert_eq!(Duration::checked_seconds_f32(f32::NAN), None);
/// assert_eq!(Duration::checked_seconds_f32(f32::NEG_INFINITY), None);
/// assert_eq!(Duration::checked_seconds_f32(f32::INFINITY), None);
Expand Down
1 change: 1 addition & 0 deletions time/src/error/component_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@ impl ComponentRange {
}

#[cfg(feature = "std")]
#[allow(clippy::std_instead_of_core)]
impl std::error::Error for ComponentRange {}
1 change: 1 addition & 0 deletions time/src/error/conversion_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl fmt::Display for ConversionRange {
}

#[cfg(feature = "std")]
#[allow(clippy::std_instead_of_core)]
impl std::error::Error for ConversionRange {}

impl From<ConversionRange> for crate::Error {
Expand Down
1 change: 1 addition & 0 deletions time/src/error/different_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ impl fmt::Display for DifferentVariant {
}

#[cfg(feature = "std")]
#[allow(clippy::std_instead_of_core)]
impl std::error::Error for DifferentVariant {}

impl From<DifferentVariant> for crate::Error {
Expand Down
1 change: 1 addition & 0 deletions time/src/error/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl TryFrom<Format> for io::Error {
}

#[cfg(feature = "std")]
#[allow(clippy::std_instead_of_core)]
impl std::error::Error for Format {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self {
Expand Down
1 change: 1 addition & 0 deletions time/src/error/indeterminate_offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ impl fmt::Display for IndeterminateOffset {
}

#[cfg(feature = "std")]
#[allow(clippy::std_instead_of_core)]
impl std::error::Error for IndeterminateOffset {}

impl From<IndeterminateOffset> for crate::Error {
Expand Down
1 change: 1 addition & 0 deletions time/src/error/invalid_format_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,5 @@ impl fmt::Display for InvalidFormatDescription {
}

#[cfg(feature = "std")]
#[allow(clippy::std_instead_of_core)]
impl std::error::Error for InvalidFormatDescription {}
1 change: 1 addition & 0 deletions time/src/error/invalid_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ impl fmt::Display for InvalidVariant {
}

#[cfg(feature = "std")]
#[allow(clippy::std_instead_of_core)]
impl std::error::Error for InvalidVariant {}

impl From<InvalidVariant> for crate::Error {
Expand Down
1 change: 1 addition & 0 deletions time/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl fmt::Display for Error {
}

#[cfg(feature = "std")]
#[allow(clippy::std_instead_of_core)]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Expand Down
1 change: 1 addition & 0 deletions time/src/error/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl fmt::Display for Parse {
}

#[cfg(feature = "std")]
#[allow(clippy::std_instead_of_core)]
impl std::error::Error for Parse {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Expand Down
1 change: 1 addition & 0 deletions time/src/error/parse_from_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl fmt::Display for ParseFromDescription {
}

#[cfg(feature = "std")]
#[allow(clippy::std_instead_of_core)]
impl std::error::Error for ParseFromDescription {}

impl From<ParseFromDescription> for crate::Error {
Expand Down
1 change: 1 addition & 0 deletions time/src/error/try_from_parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl TryFrom<TryFromParsed> for error::ComponentRange {
}

#[cfg(feature = "std")]
#[allow(clippy::std_instead_of_core)]
impl std::error::Error for TryFromParsed {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Expand Down
2 changes: 1 addition & 1 deletion time/src/parsing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<'a, T> ParsedItem<'a, T> {
/// Filter the value with the provided function. If the function returns `false`, the value
/// is discarded and `None` is returned. Otherwise, the value is preserved and `Some(self)` is
/// returned.
pub(crate) fn filter(self, f: impl FnOnce(&T) -> bool) -> Option<ParsedItem<'a, T>> {
pub(crate) fn filter(self, f: impl FnOnce(&T) -> bool) -> Option<Self> {
f(&self.1).then_some(self)
}
}
Expand Down
30 changes: 24 additions & 6 deletions time/src/parsing/parsable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,18 @@ impl sealed::Sealed for Rfc3339 {
let input = exactly_n_digits::<2, _>(input)
.and_then(|item| item.consume_value(|value| parsed.set_day(value)))
.ok_or(InvalidComponent("day"))?;
let input = ascii_char_ignore_case::<b'T'>(input)
.ok_or(InvalidLiteral)?
.into_inner();

// RFC3339 allows any separator, not just `T`, not just `space`.
// cf. Section 5.6: Internet Date/Time Format:
// NOTE: ISO 8601 defines date and time separated by "T".
// Applications using this syntax may choose, for the sake of
// readability, to specify a full-date and full-time separated by
// (say) a space character.
// Specifically, rusqlite uses space separators.
let input = input
.get(1..)
.ok_or_else(|| InvalidComponent("separator"))?;

let input = exactly_n_digits::<2, _>(input)
.and_then(|item| item.consume_value(|value| parsed.set_hour_24(value)))
.ok_or(InvalidComponent("hour"))?;
Expand Down Expand Up @@ -618,9 +627,18 @@ impl sealed::Sealed for Rfc3339 {
let input = dash(input).ok_or(InvalidLiteral)?.into_inner();
let ParsedItem(input, day) =
exactly_n_digits::<2, _>(input).ok_or(InvalidComponent("day"))?;
let input = ascii_char_ignore_case::<b'T'>(input)
.ok_or(InvalidLiteral)?
.into_inner();

// RFC3339 allows any separator, not just `T`, not just `space`.
// cf. Section 5.6: Internet Date/Time Format:
// NOTE: ISO 8601 defines date and time separated by "T".
// Applications using this syntax may choose, for the sake of
// readability, to specify a full-date and full-time separated by
// (say) a space character.
// Specifically, rusqlite uses space separators.
let input = input
.get(1..)
.ok_or_else(|| InvalidComponent("separator"))?;

let ParsedItem(input, hour) =
exactly_n_digits::<2, _>(input).ok_or(InvalidComponent("hour"))?;
let input = colon(input).ok_or(InvalidLiteral)?.into_inner();
Expand Down
6 changes: 3 additions & 3 deletions time/src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcO

impl Distribution<Time> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Time {
Time::from_hms_nanos_ranged(rng.gen(), rng.gen(), rng.gen(), rng.gen())
Time::from_hms_nanos_ranged(rng.r#gen(), rng.r#gen(), rng.r#gen(), rng.r#gen())
}
}

Expand All @@ -21,7 +21,7 @@ impl Distribution<Date> for Standard {

impl Distribution<UtcOffset> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> UtcOffset {
UtcOffset::from_hms_ranged(rng.gen(), rng.gen(), rng.gen())
UtcOffset::from_hms_ranged(rng.r#gen(), rng.r#gen(), rng.r#gen())
}
}

Expand All @@ -40,7 +40,7 @@ impl Distribution<OffsetDateTime> for Standard {

impl Distribution<Duration> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Duration {
Duration::new_ranged(rng.gen(), rng.gen())
Duration::new_ranged(rng.r#gen(), rng.r#gen())
}
}

Expand Down
2 changes: 2 additions & 0 deletions time/src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,11 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// use time::serde;
/// use time::format_description::well_known::{iso8601, Iso8601};
///
/// # #[allow(dead_code)]
/// const CONFIG: iso8601::EncodedConfig = iso8601::Config::DEFAULT
/// .set_year_is_six_digits(false)
/// .encode();
/// # #[allow(dead_code)]
/// const FORMAT: Iso8601<CONFIG> = Iso8601::<CONFIG>;
///
/// // Makes a module `mod my_format { ... }`.
Expand Down