Skip to content

Commit

Permalink
Optionally implement Arbitrary for Naive{Date,Time,DateTime}
Browse files Browse the repository at this point in the history
  • Loading branch information
asayers committed Aug 13, 2022
1 parent 4514276 commit 8e7d0d1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,12 @@ breakage if you use `no-default-features`.
There were/are numerous minor versions before 1.0 due to the language changes.
Versions with only mechanical changes will be omitted from the following list.

## Unreleased

* Implement `arbitrary::Arbitrary` for `NaiveDate`, `NaiveTime`, and
`NaiveDateTime`. You need to enable the "arbitrary" feature to get these
impls.

## 0.4.22

* Allow wasmbindgen to be optional on `wasm32-unknown-unknown` target [(#771)](https://github.com/chronotope/chrono/pull/771)
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -41,6 +41,7 @@ pure-rust-locales = { version = "0.5.2", optional = true }
criterion = { version = "0.3", optional = true }
rkyv = {version = "0.7", optional = true}
iana-time-zone = { version = "0.1.44", optional = true, features = ["fallback"] }
arbitrary = { version = "1.0.0", features = ["derive"], optional = true }

[target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies]
wasm-bindgen = { version = "0.2", optional = true }
Expand Down
9 changes: 9 additions & 0 deletions src/naive/date.rs
Expand Up @@ -176,6 +176,15 @@ pub const MIN_DATE: NaiveDate = NaiveDate::MIN;
#[deprecated(since = "0.4.20", note = "Use NaiveDate::MAX instead")]
pub const MAX_DATE: NaiveDate = NaiveDate::MAX;

#[cfg(feature = "arbitrary")]
impl arbitrary::Arbitrary<'_> for NaiveDate {
fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<NaiveDate> {
let year = u.int_in_range(MIN_YEAR..=MAX_YEAR)?;
let ord = u.int_in_range(1..=366)?;
NaiveDate::from_yo_opt(year, ord).ok_or(arbitrary::Error::IncorrectFormat)
}
}

// as it is hard to verify year flags in `NaiveDate::MIN` and `NaiveDate::MAX`,
// we use a separate run-time test.
#[test]
Expand Down
1 change: 1 addition & 0 deletions src/naive/datetime/mod.rs
Expand Up @@ -73,6 +73,7 @@ pub const MAX_DATETIME: NaiveDateTime = NaiveDateTime::MAX;
/// ```
#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)]
#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct NaiveDateTime {
date: NaiveDate,
time: NaiveTime,
Expand Down
9 changes: 9 additions & 0 deletions src/naive/time/mod.rs
Expand Up @@ -194,6 +194,15 @@ pub struct NaiveTime {
frac: u32,
}

#[cfg(feature = "arbitrary")]
impl arbitrary::Arbitrary<'_> for NaiveTime {
fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<NaiveTime> {
let secs = u.int_in_range(0..=86_400)?;
let frac = u.int_in_range(0..=2_000_000_000)?;
Ok(NaiveTime { secs, frac })
}
}

impl NaiveTime {
/// Makes a new `NaiveTime` from hour, minute and second.
///
Expand Down

0 comments on commit 8e7d0d1

Please sign in to comment.