From 537df73bdd733cc1d8b8b71d5bd52b844c1cfc5a Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Thu, 11 May 2023 21:55:45 +0200 Subject: [PATCH 1/3] Fix a doc warning --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 90661c2..f3b61e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,7 +62,7 @@ impl From for ParseDurationError { /// * "tomorrow" /// * use "ago" for the past /// -/// [num] can be a positive or negative integer. +/// `[num]` can be a positive or negative integer. /// [unit] can be one of the following: "fortnight", "week", "day", "hour", /// "minute", "min", "second", "sec" and their plural forms. /// From 7a3d0bafcaca53b073d35e087752609a39b33dba Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Thu, 11 May 2023 22:14:25 +0200 Subject: [PATCH 2/3] Reverse the two functions --- Cargo.lock | 2 +- src/lib.rs | 56 +++++++++++++++++++++++++++--------------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 53ea4dd..ebed510 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,7 +13,7 @@ dependencies = [ [[package]] name = "humantime_to_duration" -version = "0.1.4" +version = "0.2.0" dependencies = [ "regex", "time", diff --git a/src/lib.rs b/src/lib.rs index f3b61e9..9f1b963 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,6 +89,30 @@ 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) +} + +/// Parses a duration string and returns a `Duration` instance, with the duration +/// calculated from the specified date. +/// +/// # Arguments +/// +/// * `date` - A `Date` instance representing the base date for the calculation +/// * `s` - A string slice representing the relative time. +/// +/// # Examples +/// +/// ``` +/// use time::{Date, Duration, OffsetDateTime}; +/// use humantime_to_duration::{from_str_at_date, ParseDurationError}; +/// let today = OffsetDateTime::now_utc().date(); +/// 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 { let time_pattern: Regex = Regex::new( r"(?x) (?:(?P[-+]?\d*)\s*)? @@ -160,35 +184,11 @@ pub fn from_str(s: &str) -> Result { if captures_processed == 0 { Err(ParseDurationError::InvalidInput) } else { - Ok(total_duration) - } -} - -/// Parses a duration string and returns a `Duration` instance, with the duration -/// calculated from the specified date. -/// -/// # Arguments -/// -/// * `date` - A `Date` instance representing the base date for the calculation -/// * `s` - A string slice representing the relative time. -/// -/// # Examples -/// -/// ``` -/// use time::{Date, Duration, OffsetDateTime}; -/// use humantime_to_duration::{from_str_at_date, ParseDurationError}; -/// let today = OffsetDateTime::now_utc().date(); -/// 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 { - let time_now = OffsetDateTime::now_utc().date(); - let date_duration = date - time_now; + let time_now = OffsetDateTime::now_utc().date(); + let date_duration = date - time_now; - Ok(from_str(s)? + date_duration) + Ok(total_duration + date_duration) + } } #[cfg(test)] From daec32d5c45d8e975b826ed3cef79c97d0f4ff81 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Thu, 11 May 2023 22:15:29 +0200 Subject: [PATCH 3/3] describe the error --- src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 9f1b963..e826cea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,6 +100,11 @@ pub fn from_str(s: &str) -> Result { /// * `date` - A `Date` instance representing the base date for the calculation /// * `s` - A string slice representing the relative time. /// +/// # Errors +/// +/// This function will return `Err(ParseDurationError::InvalidInput)` if the input string +/// cannot be parsed as a relative time. +/// /// # Examples /// /// ```