From b439960eb10ba59a98f6e2346baa4bf379f2f81c Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 3 Jun 2023 13:14:00 +0200 Subject: [PATCH 1/3] Port the crate to use chrono instead of time --- Cargo.lock | 313 ++++++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 2 +- src/lib.rs | 42 +++---- tests/simple.rs | 18 ++- 4 files changed, 331 insertions(+), 44 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6516149..bfc0b90 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,20 +11,157 @@ dependencies = [ "memchr", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bumpalo" +version = "3.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1" + +[[package]] +name = "cc" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "chrono" +version = "0.4.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec837a71355b28f6556dbd569b37b3f363091c0bd4b2e735674521b4c5fd9bc5" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "time", + "wasm-bindgen", + "winapi", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" + [[package]] name = "humantime_to_duration" version = "0.2.1" dependencies = [ + "chrono", "regex", - "time", ] +[[package]] +name = "iana-time-zone" +version = "0.1.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0722cd7114b7de04316e7ea5456a0bbb20e4adb46fd27a3697adb812cff0f37c" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "js-sys" +version = "0.3.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f37a4a5928311ac501dee68b3c7613a1037d0edb30c8e5427bd832d55d1b790" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.144" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" + +[[package]] +name = "log" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "518ef76f2f87365916b142844c16d8fefd85039bc5699050210a7778ee1cd1de" + [[package]] name = "memchr" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9670a07f94779e00908f3e686eab508878ebb390ba6e604d3a284c00e8d0487b" + +[[package]] +name = "proc-macro2" +version = "1.0.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6aeca18b86b413c660b781aa319e4e2648a3e6f9eadc9b47e9038e6fe9f3451b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488" +dependencies = [ + "proc-macro2", +] + [[package]] name = "regex" version = "1.8.1" @@ -43,23 +180,177 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a5996294f19bd3aae0453a862ad728f60e6600695733dd5df01da90c54363a3c" [[package]] -name = "serde" -version = "1.0.160" +name = "syn" +version = "2.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c" +checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] [[package]] name = "time" -version = "0.3.20" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi", + "winapi", +] + +[[package]] +name = "unicode-ident" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0" + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + +[[package]] +name = "wasm-bindgen" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bba0e8cb82ba49ff4e229459ff22a191bbe9a1cb3a341610c9c33efc27ddf73" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b04bc93f9d6bdee709f6bd2118f57dd6679cf1176a1af464fca3ab0d66d8fb" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14d6b024f1a526bb0234f52840389927257beb670610081360e5a03c5df9c258" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e128beba882dd1eb6200e1dc92ae6c5dbaa4311aa7bb211ca035779e5efc39f8" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed9d5b4305409d1fc9482fee2d7f9bcbf24b3972bf59817ef757e23982242a93" + +[[package]] +name = "winapi" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd0cbfecb4d19b5ea75bb31ad904eb5b9fa13f21079c3b92017ebdf4999a5890" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ - "serde", - "time-core", + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", ] [[package]] -name = "time-core" -version = "0.1.0" +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" +checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" diff --git a/Cargo.toml b/Cargo.toml index 8f86a5b..774dc2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,4 @@ readme = "README.md" [dependencies] regex = "1.8" -time = "0.3.20" +chrono = "0.4" diff --git a/src/lib.rs b/src/lib.rs index 6c4e2c5..c5c9ba7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,10 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. +use chrono::{Duration, Local, NaiveDate, Utc}; use regex::{Error as RegexError, Regex}; use std::error::Error; use std::fmt::{self, Display}; -use time::{Date, Duration, OffsetDateTime}; #[derive(Debug, PartialEq)] pub enum ParseDurationError { @@ -46,7 +46,7 @@ impl From for ParseDurationError { /// # Examples /// /// ``` -/// use time::Duration; +/// use chrono::Duration; /// let duration = humantime_to_duration::from_str("+3 days"); /// assert_eq!(duration.unwrap(), Duration::days(3)); /// ``` @@ -81,7 +81,7 @@ impl From for ParseDurationError { /// # Examples /// /// ``` -/// use time::Duration; +/// use chrono::Duration; /// use humantime_to_duration::{from_str, ParseDurationError}; /// /// assert_eq!(from_str("1 hour, 30 minutes").unwrap(), Duration::minutes(90)); @@ -89,7 +89,7 @@ impl From for ParseDurationError { /// assert!(matches!(from_str("invalid"), Err(ParseDurationError::InvalidInput))); /// ``` pub fn from_str(s: &str) -> Result { - from_str_at_date(OffsetDateTime::now_utc().date(), s) + from_str_at_date(Utc::today().naive_utc(), s) } /// Parses a duration string and returns a `Duration` instance, with the duration @@ -108,16 +108,16 @@ pub fn from_str(s: &str) -> Result { /// # Examples /// /// ``` -/// use time::{Date, Duration, OffsetDateTime}; +/// use chrono::{Duration, NaiveDate, Utc, Local}; /// use humantime_to_duration::{from_str_at_date, ParseDurationError}; -/// let today = OffsetDateTime::now_utc().date(); +/// let today = Local::now().date().naive_local(); /// let yesterday = today - Duration::days(1); /// assert_eq!( /// from_str_at_date(yesterday, "2 days").unwrap(), /// Duration::days(1) // 1 day from the specified date + 1 day from the input string /// ); /// ``` -pub fn from_str_at_date(date: Date, s: &str) -> Result { +pub fn from_str_at_date(date: NaiveDate, s: &str) -> Result { let time_pattern: Regex = Regex::new( r"(?x) (?:(?P[-+]?\d*)\s*)? @@ -126,7 +126,7 @@ pub fn from_str_at_date(date: Date, s: &str) -> Resultago)?)?", )?; - let mut total_duration = Duration::ZERO; + let mut total_duration = Duration::seconds(0); let mut is_ago = s.contains(" ago"); let mut captures_processed = 0; let mut total_length = 0; @@ -165,15 +165,15 @@ pub fn from_str_at_date(date: Date, s: &str) -> Result Duration::seconds(value), "yesterday" => Duration::days(-1), "tomorrow" => Duration::days(1), - "now" | "today" => Duration::ZERO, + "now" | "today" => Duration::zero(), _ => return Err(ParseDurationError::InvalidInput), }; - - total_duration = match total_duration.checked_add(if is_ago { -duration } else { duration }) - { - Some(duration) => duration, - None => return Err(ParseDurationError::InvalidInput), - }; + let neg_duration = -duration; + total_duration = + match total_duration.checked_add(if is_ago { &neg_duration } else { &duration }) { + Some(duration) => duration, + None => return Err(ParseDurationError::InvalidInput), + }; // Calculate the total length of the matched substring if let Some(m) = capture.get(0) { @@ -189,7 +189,7 @@ pub fn from_str_at_date(date: Date, s: &str) -> Result Date: Sat, 3 Jun 2023 14:38:30 +0200 Subject: [PATCH 2/3] Fix the various warnings --- src/lib.rs | 16 ++++++++-------- tests/simple.rs | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c5c9ba7..a8cbf5d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,7 +89,7 @@ impl From for ParseDurationError { /// assert!(matches!(from_str("invalid"), Err(ParseDurationError::InvalidInput))); /// ``` pub fn from_str(s: &str) -> Result { - from_str_at_date(Utc::today().naive_utc(), s) + from_str_at_date(Utc::now().date_naive(), s) } /// Parses a duration string and returns a `Duration` instance, with the duration @@ -189,7 +189,7 @@ pub fn from_str_at_date(date: NaiveDate, s: &str) -> Result assert!(true), - _ => assert!(false), + _ => panic!(), } let result = from_str("invalid 1"); match result { Err(ParseDurationError::InvalidInput) => assert!(true), - _ => assert!(false), + _ => panic!(), } // Fails for now with a panic /* let result = from_str("777777777777777771m"); @@ -346,8 +346,8 @@ mod tests { #[test] fn test_from_str_at_date() { - let date = NaiveDate::from_ymd(2014, 9, 5); - let now = Local::today().naive_local(); + let date = NaiveDate::from_ymd_opt(2014, 9, 5).unwrap(); + let now = Local::now().date_naive(); let days_diff = (date - now).num_days(); assert_eq!( @@ -363,7 +363,7 @@ mod tests { #[test] fn test_invalid_input_at_date() { - let date = NaiveDate::from_ymd(2014, 9, 5); + let date = NaiveDate::from_ymd_opt(2014, 9, 5).unwrap(); assert!(matches!( from_str_at_date(date, "invalid"), Err(ParseDurationError::InvalidInput) diff --git a/tests/simple.rs b/tests/simple.rs index 2e768f2..366f318 100644 --- a/tests/simple.rs +++ b/tests/simple.rs @@ -1,4 +1,4 @@ -use chrono::{Duration, Local, NaiveDate, Utc}; +use chrono::{Duration, Utc}; use humantime_to_duration::{from_str, from_str_at_date, ParseDurationError}; #[test] @@ -128,7 +128,7 @@ fn test_display_should_fail() { #[test] fn test_from_str_at_date_day() { - let today = Utc::today().naive_utc(); + let today = Utc::now().date_naive(); let yesterday = today - Duration::days(1); assert_eq!( from_str_at_date(yesterday, "2 days").unwrap(), @@ -138,7 +138,7 @@ fn test_from_str_at_date_day() { #[test] fn test_invalid_input_at_date() { - let today = Utc::today().naive_utc(); + let today = Utc::now().date_naive(); let result = from_str_at_date(today, "foobar"); println!("{result:?}"); assert_eq!(result, Err(ParseDurationError::InvalidInput)); From 1d78cc5a3fe3d13472ed06ce462fc9659442d3e9 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 3 Jun 2023 16:31:37 +0200 Subject: [PATCH 3/3] Simplify the check --- src/lib.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a8cbf5d..b53891a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -326,16 +326,10 @@ mod tests { fn test_invalid_input() { let result = from_str("foobar"); println!("{result:?}"); - match result { - Err(ParseDurationError::InvalidInput) => assert!(true), - _ => panic!(), - } + assert_eq!(result, Err(ParseDurationError::InvalidInput)); let result = from_str("invalid 1"); - match result { - Err(ParseDurationError::InvalidInput) => assert!(true), - _ => panic!(), - } + assert_eq!(result, Err(ParseDurationError::InvalidInput)); // Fails for now with a panic /* let result = from_str("777777777777777771m"); match result {