From 30496269d5a6b1d6356ea6f402c526fddbd26fae Mon Sep 17 00:00:00 2001 From: Pro Date: Fri, 9 Feb 2024 20:17:10 +0100 Subject: Bump env_logger Some tests were failing with: env_logger::init should not be called after logger initialized: SetLoggerError(()) --- Cargo.toml | 2 +- src/timetype.rs | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 44fbb87..72c53d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ thiserror = "1" filters = { version = "0.3", optional = true } [dev-dependencies] -env_logger = "0.4" +env_logger = "0.11" log = "0.4" [features] diff --git a/src/timetype.rs b/src/timetype.rs index 818ad4b..e559b3c 100644 --- a/src/timetype.rs +++ b/src/timetype.rs @@ -2054,8 +2054,6 @@ mod moment_plus_amount_tests { } => { #[test] fn $name() { - let _ = env_logger::init(); - let base = TT::moment($base); debug!("Using base = {:?}", base); debug!(" + {:?}", $amount); -- cgit v1.2.3 From e4443d26abd7831cf5deae840aeb5aff5cb1a575 Mon Sep 17 00:00:00 2001 From: Pro Date: Fri, 9 Feb 2024 20:20:22 +0100 Subject: Bump easy deps --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 72c53d2..38a1ec5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ nom = "3" iso8601 = "0.2" thiserror = "1" -filters = { version = "0.3", optional = true } +filters = { version = "0.4", optional = true } [dev-dependencies] env_logger = "0.11" -- cgit v1.2.3 From fbf408b649322c7587dbda34656774a458c2ad46 Mon Sep 17 00:00:00 2001 From: Pro Date: Fri, 9 Feb 2024 20:20:25 +0100 Subject: Cleanup --- src/indicator.rs | 26 +- src/iter.rs | 27 +- src/parser/iterator.rs | 91 +++---- src/parser/mod.rs | 2 +- src/parser/timetype.rs | 96 +++---- src/timetype.rs | 687 ++++++++++++++++++++++++------------------------- src/util.rs | 92 +++---- 7 files changed, 505 insertions(+), 516 deletions(-) diff --git a/src/indicator.rs b/src/indicator.rs index fa9bf63..39bd371 100644 --- a/src/indicator.rs +++ b/src/indicator.rs @@ -21,16 +21,16 @@ pub enum Day { Sunday, } -impl Into<::chrono::Weekday> for Day { - fn into(self) -> ::chrono::Weekday { - match self { - Day::Monday => ::chrono::Weekday::Mon, - Day::Tuesday => ::chrono::Weekday::Tue, - Day::Wednesday => ::chrono::Weekday::Wed, - Day::Thursday => ::chrono::Weekday::Thu, - Day::Friday => ::chrono::Weekday::Fri, - Day::Saturday => ::chrono::Weekday::Sat, - Day::Sunday => ::chrono::Weekday::Sun, +impl From for chrono::Weekday { + fn from(val: Day) -> Self { + match val { + Day::Monday => chrono::Weekday::Mon, + Day::Tuesday => chrono::Weekday::Tue, + Day::Wednesday => chrono::Weekday::Wed, + Day::Thursday => chrono::Weekday::Thu, + Day::Friday => chrono::Weekday::Fri, + Day::Saturday => chrono::Weekday::Sat, + Day::Sunday => chrono::Weekday::Sun, } } } @@ -51,9 +51,9 @@ pub enum Month { December, } -impl Into for Month { - fn into(self) -> u32 { - match self { +impl From for u32 { + fn from(val: Month) -> Self { + match val { Month::January => 1, Month::February => 2, Month::March => 3, diff --git a/src/iter.rs b/src/iter.rs index d776037..ee32d6d 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -123,7 +123,7 @@ impl Iterator for FilterIter } pub trait EveryFilter : Iterator> + Sized { - fn every(self, M) -> FilterIter; + fn every(self, matcher: M) -> FilterIter; } impl EveryFilter for I @@ -171,7 +171,7 @@ impl Iterator for WithoutIter } pub trait WithoutFilter : Iterator> + Sized { - fn without(self, M) -> WithoutIter; + fn without(self, matcher: M) -> WithoutIter; } impl WithoutFilter for I @@ -221,7 +221,7 @@ impl Iterator for UntilIter } pub trait Until : Iterator> + Sized { - fn until(self, NaiveDateTime) -> UntilIter; + fn until(self, ending: NaiveDateTime) -> UntilIter; } impl Until for I @@ -247,7 +247,7 @@ impl TimesIter fn new(i: I, times: i64) -> TimesIter { TimesIter { inner: i, - times: times, + times, count: 0, } } @@ -269,7 +269,7 @@ impl Iterator for TimesIter } pub trait Times : Iterator> + Sized { - fn times(self, i64) -> TimesIter; + fn times(self, times: i64) -> TimesIter; } impl Times for I @@ -422,7 +422,7 @@ pub mod extensions { use chrono::NaiveDate as ND; fn ymd_hms(y: i32, m: u32, d: u32, h: u32, mi: u32, s: u32) -> TT { - TT::moment(ND::from_ymd(y, m, d).and_hms(h, mi, s)) + TT::moment(ND::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, mi, s).unwrap()) } #[test] @@ -569,12 +569,11 @@ mod test_until { #[test] fn test_until() { - let yesterday = (TimeType::today() - TimeType::days(1)) + let yesterday = *(TimeType::today() - TimeType::days(1)) .calculate() .unwrap() .get_moment() - .unwrap() - .clone(); + .unwrap(); let v = TimeType::today() .daily(1) @@ -587,12 +586,11 @@ mod test_until { #[test] fn test_until_1() { - let end = (TimeType::today() + TimeType::days(1)) + let end = *(TimeType::today() + TimeType::days(1)) .calculate() .unwrap() .get_moment() - .unwrap() - .clone(); + .unwrap(); let v = TimeType::today() .daily(1) @@ -605,12 +603,11 @@ mod test_until { #[test] fn test_until_2() { - let end = (TimeType::today() + TimeType::days(2)) + let end = *(TimeType::today() + TimeType::days(2)) .calculate() .unwrap() .get_moment() - .unwrap() - .clone(); + .unwrap(); let v = TimeType::today() .hourly(1) diff --git a/src/parser/iterator.rs b/src/parser/iterator.rs index 7d3007c..fc4b6f9 100644 --- a/src/parser/iterator.rs +++ b/src/parser/iterator.rs @@ -148,6 +148,7 @@ impl ::std::iter::Iterator for UserIterator #[cfg(test)] mod tests { + use std::iter::Iterator; use nom::IResult; use super::*; @@ -169,7 +170,7 @@ mod tests { #[test] fn test_iterator_1() { let res = iterator(&b"2017-01-01 hourly"[..]); - assert!(res.is_done(), format!("Not done: {:?}", res)); + assert!(res.is_done(), "Not done: {:?}", res); let (_, i) = res.unwrap(); println!("{:#?}", i); @@ -182,18 +183,18 @@ mod tests { assert!(n.is_ok(), "Not ok: {:#?}", n); let tt = n.unwrap(); assert_eq!(tt.get_moment().unwrap().year() , 2017); - assert_eq!(tt.get_moment().unwrap().month() , 01); - assert_eq!(tt.get_moment().unwrap().day() , 01); + assert_eq!(tt.get_moment().unwrap().month() , 1); + assert_eq!(tt.get_moment().unwrap().day() , 1); assert_eq!(tt.get_moment().unwrap().hour() , hour); - assert_eq!(tt.get_moment().unwrap().minute(), 00); - assert_eq!(tt.get_moment().unwrap().second(), 00); + assert_eq!(tt.get_moment().unwrap().minute(), 0); + assert_eq!(tt.get_moment().unwrap().second(), 0); } } #[test] fn test_iterator_2() { let res = iterator(&b"2017-01-01 every 2mins"[..]); - assert!(res.is_done(), format!("Not done: {:?}", res)); + assert!(res.is_done(), "Not done: {:?}", res); let (_, i) = res.unwrap(); println!("{:#?}", i); @@ -201,23 +202,23 @@ mod tests { assert!(ui.is_ok(), "Not okay: {:#?}", ui); let mut ui = ui.unwrap(); - for min in (0..60).into_iter().filter(|n| n % 2 == 0) { + for min in (0..60).filter(|n| n % 2 == 0) { let n = ui.next().unwrap(); assert!(n.is_ok(), "Not ok: {:#?}", n); let tt = n.unwrap(); assert_eq!(tt.get_moment().unwrap().year() , 2017); - assert_eq!(tt.get_moment().unwrap().month() , 01); - assert_eq!(tt.get_moment().unwrap().day() , 01); - assert_eq!(tt.get_moment().unwrap().hour() , 00); + assert_eq!(tt.get_moment().unwrap().month() , 1); + assert_eq!(tt.get_moment().unwrap().day() , 1); + assert_eq!(tt.get_moment().unwrap().hour() , 0); assert_eq!(tt.get_moment().unwrap().minute(), min); - assert_eq!(tt.get_moment().unwrap().second(), 00); + assert_eq!(tt.get_moment().unwrap().second(), 0); } } #[test] fn test_iterator_3() { let res = iterator(&b"2017-01-01 daily"[..]); - assert!(res.is_done(), format!("Not done: {:?}", res)); + assert!(res.is_done(), "Not done: {:?}", res); let (_, i) = res.unwrap(); println!("{:#?}", i); @@ -230,18 +231,18 @@ mod tests { assert!(n.is_ok(), "Not ok: {:#?}", n); let tt = n.unwrap(); assert_eq!(tt.get_moment().unwrap().year() , 2017); - assert_eq!(tt.get_moment().unwrap().month() , 01); + assert_eq!(tt.get_moment().unwrap().month() , 1); assert_eq!(tt.get_moment().unwrap().day() , day); - assert_eq!(tt.get_moment().unwrap().hour() , 00); - assert_eq!(tt.get_moment().unwrap().minute(), 00); - assert_eq!(tt.get_moment().unwrap().second(), 00); + assert_eq!(tt.get_moment().unwrap().hour() , 0); + assert_eq!(tt.get_moment().unwrap().minute(), 0); + assert_eq!(tt.get_moment().unwrap().second(), 0); } } #[test] fn test_iterator_4() { let res = iterator(&b"2017-01-01 weekly"[..]); - assert!(res.is_done(), format!("Not done: {:?}", res)); + assert!(res.is_done(), "Not done: {:?}", res); let (_, i) = res.unwrap(); println!("{:#?}", i); @@ -254,18 +255,18 @@ mod tests { assert!(n.is_ok(), "Not ok: {:#?}", n); let tt = n.unwrap(); assert_eq!(tt.get_moment().unwrap().year() , 2017); - assert_eq!(tt.get_moment().unwrap().month() , 01); - assert_eq!(tt.get_moment().unwrap().day() , 01 + (week * 7)); - assert_eq!(tt.get_moment().unwrap().hour() , 00); - assert_eq!(tt.get_moment().unwrap().minute(), 00); - assert_eq!(tt.get_moment().unwrap().second(), 00); + assert_eq!(tt.get_moment().unwrap().month() , 1); + assert_eq!(tt.get_moment().unwrap().day() , 1 + (week * 7)); + assert_eq!(tt.get_moment().unwrap().hour() , 0); + assert_eq!(tt.get_moment().unwrap().minute(), 0); + assert_eq!(tt.get_moment().unwrap().second(), 0); } } #[test] fn test_until_spec_1() { let res = until_spec(&b"until 2017-01-01T05:00:00"[..]); - assert!(res.is_done(), format!("Not done: {:?}", res)); + assert!(res.is_done(), "Not done: {:?}", res); let (_, i) = res.unwrap(); println!("{:#?}", i); } @@ -273,7 +274,7 @@ mod tests { #[test] fn test_until_iterator_1() { let res = iterator(&b"2017-01-01 hourly until 2017-01-01T05:00:00"[..]); - assert!(res.is_done(), format!("Not done: {:?}", res)); + assert!(res.is_done(), "Not done: {:?}", res); let (_, i) = res.unwrap(); println!("{:#?}", i); @@ -292,11 +293,11 @@ mod tests { assert!(n.is_ok(), "Not ok: {:#?}", n); let tt = n.unwrap(); assert_eq!(tt.get_moment().unwrap().year() , 2017); - assert_eq!(tt.get_moment().unwrap().month() , 01); - assert_eq!(tt.get_moment().unwrap().day() , 01); + assert_eq!(tt.get_moment().unwrap().month() , 1); + assert_eq!(tt.get_moment().unwrap().day() , 1); assert_eq!(tt.get_moment().unwrap().hour() , hour); - assert_eq!(tt.get_moment().unwrap().minute(), 00); - assert_eq!(tt.get_moment().unwrap().second(), 00); + assert_eq!(tt.get_moment().unwrap().minute(), 0); + assert_eq!(tt.get_moment().unwrap().second(), 0); } } } @@ -304,7 +305,7 @@ mod tests { #[test] fn test_until_iterator_2() { let res = iterator(&b"2017-01-01 every 2mins until 2017-01-01T00:10:00"[..]); - assert!(res.is_done(), format!("Not done: {:?}", res)); + assert!(res.is_done(), "Not done: {:?}", res); let (_, i) = res.unwrap(); println!("{:#?}", i); @@ -312,7 +313,7 @@ mod tests { assert!(ui.is_ok(), "Not okay: {:#?}", ui); let mut ui = ui.unwrap(); - for min in (0..60).into_iter().filter(|n| n % 2 == 0) { + for min in (0..60).filter(|n| n % 2 == 0) { if min > 9 { let n = ui.next(); assert!(n.is_none(), "Is Some, should be None: {:?}", n); @@ -322,11 +323,11 @@ mod tests { assert!(n.is_ok(), "Not ok: {:#?}", n); let tt = n.unwrap(); assert_eq!(tt.get_moment().unwrap().year() , 2017); - assert_eq!(tt.get_moment().unwrap().month() , 01); - assert_eq!(tt.get_moment().unwrap().day() , 01); - assert_eq!(tt.get_moment().unwrap().hour() , 00); + assert_eq!(tt.get_moment().unwrap().month() , 1); + assert_eq!(tt.get_moment().unwrap().day() , 1); + assert_eq!(tt.get_moment().unwrap().hour() , 0); assert_eq!(tt.get_moment().unwrap().minute(), min); - assert_eq!(tt.get_moment().unwrap().second(), 00); + assert_eq!(tt.get_moment().unwrap().second(), 0); } } } @@ -334,7 +335,7 @@ mod tests { #[test] fn test_until_iterator_3() { let res = iterator(&b"2017-01-01 daily until 2017-01-05"[..]); - assert!(res.is_done(), format!("Not done: {:?}", res)); + assert!(res.is_done(), "Not done: {:?}", res); let (_, i) = res.unwrap(); println!("{:#?}", i); @@ -352,11 +353,11 @@ mod tests { assert!(n.is_ok(), "Not ok: {:#?}", n); let tt = n.unwrap(); assert_eq!(tt.get_moment().unwrap().year() , 2017); - assert_eq!(tt.get_moment().unwrap().month() , 01); + assert_eq!(tt.get_moment().unwrap().month() , 1); assert_eq!(tt.get_moment().unwrap().day() , day); - assert_eq!(tt.get_moment().unwrap().hour() , 00); - assert_eq!(tt.get_moment().unwrap().minute(), 00); - assert_eq!(tt.get_moment().unwrap().second(), 00); + assert_eq!(tt.get_moment().unwrap().hour() , 0); + assert_eq!(tt.get_moment().unwrap().minute(), 0); + assert_eq!(tt.get_moment().unwrap().second(), 0); } } } @@ -364,7 +365,7 @@ mod tests { #[test] fn test_until_iterator_4() { let res = iterator(&b"2017-01-01 weekly until 2017-01-14"[..]); - assert!(res.is_done(), format!("Not done: {:?}", res)); + assert!(res.is_done(), "Not done: {:?}", res); let (_, i) = res.unwrap(); println!("{:#?}", i); @@ -382,11 +383,11 @@ mod tests { assert!(n.is_ok(), "Not ok: {:#?}", n); let tt = n.unwrap(); assert_eq!(tt.get_moment().unwrap().year() , 2017); - assert_eq!(tt.get_moment().unwrap().month() , 01); - assert_eq!(tt.get_moment().unwrap().day() , 01 + (week * 7)); - assert_eq!(tt.get_moment().unwrap().hour() , 00); - assert_eq!(tt.get_moment().unwrap().minute(), 00); - assert_eq!(tt.get_moment().unwrap().second(), 00); + assert_eq!(tt.get_moment().unwrap().month() , 1); + assert_eq!(tt.get_moment().unwrap().day() , 1 + (week * 7)); + assert_eq!(tt.get_moment().unwrap().hour() , 0); + assert_eq!(tt.get_moment().unwrap().minute(), 0); + assert_eq!(tt.get_moment().unwrap().second(), 0); } } } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 88d0020..1338dc5 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -70,7 +70,7 @@ pub fn parse(s: &str) -> Result { match do_parse(s.as_bytes()) { IResult::Done(_, Ok(o)) => Ok(o), IResult::Done(_, Err(e)) => Err(e), - IResult::Error(e) => Err(e).map_err(From::from), + IResult::Error(e) => Err(From::from(e)), IResult::Incomplete(Needed::Unknown) => Err(Error::UnknownParserError), IResult::Incomplete(Needed::Size(_)) => Err(Error::UnknownParserError), diff --git a/src/parser/timetype.rs b/src/parser/timetype.rs index 40ae3f7..7106d77 100644 --- a/src/parser/timetype.rs +++ b/src/parser/timetype.rs @@ -70,9 +70,9 @@ pub enum UnitAlias { Yearly, } -impl Into for UnitAlias { - fn into(self) -> Unit { - match self { +impl From for Unit { + fn from(val: UnitAlias) -> Self { + match val { UnitAlias::Secondly => Unit::Second, UnitAlias::Minutely => Unit::Minute, UnitAlias::Hourly => Unit::Hour, @@ -138,7 +138,7 @@ named!(pub amount_expr, do_parse!( amount:amount_parser >> opt!(sp) >> o: opt!(complete!(amount_expr_next)) >> - (AmountExpr { amount: amount, next: o, }) + (AmountExpr { amount, next: o, }) )); #[derive(Debug, PartialEq, Eq)] @@ -153,8 +153,8 @@ impl IntoTimeType for AmountExpr { if let Some((op, other_amonut_expr)) = self.next { match op { - Operator::Plus => amount = amount + (*other_amonut_expr).into_timetype()?, - Operator::Minus => amount = amount - (*other_amonut_expr).into_timetype()?, + Operator::Plus => amount += (*other_amonut_expr).into_timetype()?, + Operator::Minus => amount -= (*other_amonut_expr).into_timetype()?, } } @@ -179,8 +179,8 @@ pub enum ExactDate { Today, Yesterday, Tomorrow, - Iso8601Date(::iso8601::Date), - Iso8601DateTime(::iso8601::DateTime) + Iso8601Date(iso8601::Date), + Iso8601DateTime(iso8601::DateTime) } impl IntoTimeType for ExactDate { @@ -191,14 +191,14 @@ impl IntoTimeType for ExactDate { ExactDate::Tomorrow => Ok(timetype::TimeType::today() + timetype::TimeType::days(1)), ExactDate::Iso8601Date(date) => { match date { - ::iso8601::Date::YMD { year, month, day } => NaiveDate::from_ymd_opt(year, month, day) + iso8601::Date::YMD { year, month, day } => NaiveDate::from_ymd_opt(year, month, day) + .and_then(|ndt| ndt.and_hms_opt(0, 0, 0)) .ok_or(Error::OutOfBounds(year, month, day, 0, 0, 0)) - .map(|ndt| ndt.and_hms(0, 0, 0)) .map(timetype::TimeType::moment), - ::iso8601::Date::Week { year, ww, d } => NaiveDate::from_ymd_opt(year, 1, 1) + iso8601::Date::Week { year, ww, d } => NaiveDate::from_ymd_opt(year, 1, 1) + .and_then(|ndt| ndt.and_hms_opt(0, 0, 0)) .ok_or(Error::OutOfBounds(year, 1, 1, 0, 0, 0)) - .map(|ndt| ndt.and_hms(0, 0, 0)) .map(timetype::TimeType::moment) .map(|m| { m @@ -206,25 +206,25 @@ impl IntoTimeType for ExactDate { + timetype::TimeType::days(d as i64) }), - ::iso8601::Date::Ordinal { year, ddd } => NaiveDate::from_ymd_opt(year, 1, 1) + iso8601::Date::Ordinal { year, ddd } => NaiveDate::from_ymd_opt(year, 1, 1) + .and_then(|ndt| ndt.and_hms_opt(0, 0, 0)) .ok_or(Error::OutOfBounds(year, 1, 1, 0, 0, 0)) - .map(|ndt| ndt.and_hms(0, 0, 0)) .map(timetype::TimeType::moment) .map(|m| m + timetype::TimeType::days(ddd as i64)), } }, - ExactDate::Iso8601DateTime(::iso8601::DateTime { date, time }) => { + ExactDate::Iso8601DateTime(iso8601::DateTime { date, time }) => { let (hour, minute, second) = (time.hour, time.minute, time.second); match date { - ::iso8601::Date::YMD { year, month, day } => NaiveDate::from_ymd_opt(year, month, day) + iso8601::Date::YMD { year, month, day } => NaiveDate::from_ymd_opt(year, month, day) .and_then(|ndt| ndt.and_hms_opt(hour, minute, second)) .ok_or(Error::OutOfBounds(year, month, day, hour, minute, second)) .map(timetype::TimeType::moment), - ::iso8601::Date::Week { year, ww, d } => NaiveDate::from_ymd_opt(year, 1, 1) + iso8601::Date::Week { year, ww, d } => NaiveDate::from_ymd_opt(year, 1, 1) + .and_then(|ndt| ndt.and_hms_opt(0, 0, 0)) .ok_or(Error::OutOfBounds(year, 1, 1, 0, 0, 0)) - .map(|ndt| ndt.and_hms(0, 0, 0)) .map(timetype::TimeType::moment) .map(|m| { m @@ -235,9 +235,9 @@ impl IntoTimeType for ExactDate { + timetype::TimeType::seconds(second as i64) }), - ::iso8601::Date::Ordinal { year, ddd } => NaiveDate::from_ymd_opt(year, 1, 1) + iso8601::Date::Ordinal { year, ddd } => NaiveDate::from_ymd_opt(year, 1, 1) + .and_then(|ndt| ndt.and_hms_opt(0, 0, 0)) .ok_or(Error::OutOfBounds(year, 1, 1, 0, 0, 0)) - .map(|ndt| ndt.and_hms(0, 0, 0)) .map(timetype::TimeType::moment) .map(|m| { m @@ -422,7 +422,7 @@ mod tests { assert!(res.is_done()); match res.unwrap().1 { - ExactDate::Iso8601DateTime(_) => assert!(false), + ExactDate::Iso8601DateTime(_) => panic!("Unexpected enum variant"), ExactDate::Iso8601Date(d) => { match d { Date::YMD { year, month, day } => { @@ -430,12 +430,12 @@ mod tests { assert_eq!(month, 1); assert_eq!(day, 1) }, - _ => assert!(false), + _ => panic!("Unexpected enum variant"), } }, - ExactDate::Tomorrow => assert!(false), - ExactDate::Yesterday => assert!(false), - ExactDate::Today => assert!(false), + ExactDate::Tomorrow => panic!("Unexpected enum variant"), + ExactDate::Yesterday => panic!("Unexpected enum variant"), + ExactDate::Today => panic!("Unexpected enum variant"), }; } @@ -453,32 +453,32 @@ mod tests { assert_eq!(month, 1); assert_eq!(day, 1) }, - _ => assert!(false), + _ => panic!("Unexpected enum variant"), } assert_eq!(obj.time.hour, 22); assert_eq!(obj.time.minute, 0); assert_eq!(obj.time.second, 11); }, - ExactDate::Iso8601Date(_) => assert!(false), - ExactDate::Tomorrow => assert!(false), - ExactDate::Yesterday => assert!(false), - ExactDate::Today => assert!(false), + ExactDate::Iso8601Date(_) => panic!("Unexpected enum variant"), + ExactDate::Tomorrow => panic!("Unexpected enum variant"), + ExactDate::Yesterday => panic!("Unexpected enum variant"), + ExactDate::Today => panic!("Unexpected enum variant"), }; } #[test] fn test_simple_date_1() { let res = exact_date_parser(&b"today"[..]); - assert!(res.is_done(), format!("Not done: {:?}", res)); + assert!(res.is_done(), "Not done: {:?}", res); let res = date(&b"today"[..]); - assert!(res.is_done(), format!("Not done: {:?}", res)); + assert!(res.is_done(), "Not done: {:?}", res); } #[test] fn test_simple_date_2() { let res = date(&b"2017-01-01"[..]); - assert!(res.is_done(), format!("Not done: {:?}", res)); + assert!(res.is_done(), "Not done: {:?}", res); let (_, o) = res.unwrap(); println!("{:#?}", o); @@ -491,17 +491,17 @@ mod tests { println!("{:#?}", calc_res); assert_eq!(calc_res.get_moment().unwrap().year() , 2017); - assert_eq!(calc_res.get_moment().unwrap().month() , 01); - assert_eq!(calc_res.get_moment().unwrap().day() , 01); - assert_eq!(calc_res.get_moment().unwrap().hour() , 00); - assert_eq!(calc_res.get_moment().unwrap().minute(), 00); - assert_eq!(calc_res.get_moment().unwrap().second(), 00); + assert_eq!(calc_res.get_moment().unwrap().month() , 1); + assert_eq!(calc_res.get_moment().unwrap().day() , 1); + assert_eq!(calc_res.get_moment().unwrap().hour() , 0); + assert_eq!(calc_res.get_moment().unwrap().minute(), 0); + assert_eq!(calc_res.get_moment().unwrap().second(), 0); } #[test] fn test_simple_date_3() { let res = date(&b"2017-01-01T01:02:03"[..]); - assert!(res.is_done(), format!("Not done: {:?}", res)); + assert!(res.is_done(), "Not done: {:?}", res); let (_, o) = res.unwrap(); println!("{:#?}", o); @@ -514,11 +514,11 @@ mod tests { println!("{:#?}", calc_res); assert_eq!(calc_res.get_moment().unwrap().year() , 2017); - assert_eq!(calc_res.get_moment().unwrap().month() , 01); - assert_eq!(calc_res.get_moment().unwrap().day() , 01); - assert_eq!(calc_res.get_moment().unwrap().hour() , 01); - assert_eq!(calc_res.get_moment().unwrap().minute(), 02); - assert_eq!(calc_res.get_moment().unwrap().second(), 03); + assert_eq!(calc_res.get_moment().unwrap().month() , 1); + assert_eq!(calc_res.get_moment().unwrap().day() , 1); + assert_eq!(calc_res.get_moment().unwrap().hour() , 1); + assert_eq!(calc_res.get_moment().unwrap().minute(), 2); + assert_eq!(calc_res.get_moment().unwrap().second(), 3); } #[test] @@ -588,11 +588,11 @@ mod tests { println!("{:#?}", calc_res); assert_eq!(calc_res.get_moment().unwrap().year() , 2017); - assert_eq!(calc_res.get_moment().unwrap().month() , 01); - assert_eq!(calc_res.get_moment().unwrap().day() , 01); - assert_eq!(calc_res.get_moment().unwrap().hour() , 00); + assert_eq!(calc_res.get_moment().unwrap().month() , 1); + assert_eq!(calc_res.get_moment().unwrap().day() , 1); + assert_eq!(calc_res.get_moment().unwrap().hour() , 0); assert_eq!(calc_res.get_moment().unwrap().minute(), 17); - assert_eq!(calc_res.get_moment().unwrap().second(), 00); + assert_eq!(calc_res.get_moment().unwrap().second(), 0); } #[test] diff --git a/src/timetype.rs b/src/timetype.rs index e559b3c..9f67289 100644 --- a/src/timetype.rs +++ b/src/timetype.rs @@ -98,36 +98,24 @@ impl TimeType { } pub fn is_a_amount(&self) -> bool { - match *self { - TimeType::Seconds(_) | + matches!(self, TimeType::Seconds(_) | TimeType::Minutes(_) | TimeType::Hours(_) | TimeType::Days(_) | TimeType::Months(_) | - TimeType::Years(_) => true, - _ => false, - } + TimeType::Years(_)) } pub fn is_moment(&self) -> bool { - match *self { - TimeType::Moment(_) => true, - _ => false, - } + matches!(self, TimeType::Moment(_)) } pub fn is_addition(&self) -> bool { - match *self { - TimeType::Addition(_, _) => true, - _ => false, - } + matches!(self, TimeType::Addition(_, _)) } pub fn is_subtraction(&self) -> bool { - match *self { - TimeType::Subtraction(_, _) => true, - _ => false, - } + matches!(self, TimeType::Subtraction(_, _)) } pub fn seconds(i: i64) -> TimeType { @@ -381,8 +369,8 @@ impl TimeType { } pub fn get_moment(&self) -> Option<&NaiveDateTime> { - match *self { - TimeType::Moment(ref m) => Some(&m), + match self { + TimeType::Moment(m) => Some(m), _ => None, } } @@ -482,9 +470,9 @@ fn end_of_year(tt: TimeType) -> Result { els @ TT::Addition(_, _) | els @ TT::Subtraction(_, _) => Err(Error::CannotCalculateEndOfYearOn(els)), TT::Moment(m) => NaiveDate::from_ymd_opt(m.year(), 12, 31) - .map(|nd| nd.and_hms(0, 0, 0)) + .and_then(|nd| nd.and_hms_opt(0, 0, 0)) .map(TT::moment) - .ok_or(Error::OutOfBounds(m.year() as i32, 12, 31, 0, 0, 0)) + .ok_or(Error::OutOfBounds(m.year(), 12, 31, 0, 0, 0)) .map_err(Error::from), TT::EndOfYear(e) => do_calculate(*e), @@ -514,9 +502,9 @@ fn end_of_month(tt: TimeType) -> Result { TT::Moment(m) => { let last_day = get_num_of_days_in_month(m.year() as i64, m.month() as i64) as u32; NaiveDate::from_ymd_opt(m.year(), m.month(), last_day) - .map(|nd| nd.and_hms(0, 0, 0)) + .and_then(|nd| nd.and_hms_opt(0, 0, 0)) .map(TT::moment) - .ok_or(Error::OutOfBounds(m.year() as i32, m.month() as u32, last_day, 0, 0, 0)) + .ok_or(Error::OutOfBounds(m.year(), m.month(), last_day, 0, 0, 0)) .map_err(Error::from) }, TT::EndOfYear(e) => do_calculate(*e), @@ -544,9 +532,9 @@ fn end_of_day(tt: TimeType) -> Result { els @ TT::Addition(_, _) | els @ TT::Subtraction(_, _) => Err(Error::CannotCalculateEndOfMonthOn(els)), TT::Moment(m) => NaiveDate::from_ymd_opt(m.year(), m.month(), m.day()) - .map(|nd| nd.and_hms(23, 59, 59)) + .and_then(|nd| nd.and_hms_opt(23, 59, 59)) .map(TT::moment) - .ok_or(Error::OutOfBounds(m.year() as i32, m.month() as u32, m.day() as u32, 23, 59, 59)) + .ok_or(Error::OutOfBounds(m.year(), m.month(), m.day(), 23, 59, 59)) .map_err(Error::from), TT::EndOfYear(e) => do_calculate(*e), TT::EndOfMonth(e) => do_calculate(*e), @@ -575,7 +563,7 @@ fn end_of_hour(tt: TimeType) -> Result { TT::Moment(m) => NaiveDate::from_ymd_opt(m.year(), m.month(), m.day()) .and_then(|nd| nd.and_hms_opt(m.hour(), 59, 59)) .map(TT::moment) - .ok_or(Error::OutOfBounds(m.year() as i32, m.month() as u32, m.day() as u32, m.hour() as u32, 59, 59)) + .ok_or(Error::OutOfBounds(m.year(), m.month(), m.day(), m.hour(), 59, 59)) .map_err(Error::from), TT::EndOfYear(e) => do_calculate(*e), TT::EndOfMonth(e) => do_calculate(*e), @@ -604,7 +592,7 @@ fn end_of_minute(tt: TimeType) -> Result { TT::Moment(m) => NaiveDate::from_ymd_opt(m.year(), m.month(), m.day()) .and_then(|nd| nd.and_hms_opt(m.hour(), m.minute(), 59)) .map(TT::moment) - .ok_or(Error::OutOfBounds(m.year() as i32, m.month() as u32, m.day() as u32, m.hour() as u32, m.minute() as u32, 59 as u32)) + .ok_or(Error::OutOfBounds(m.year(), m.month(), m.day(), m.hour(), m.minute(), 59)) .map_err(Error::from), TT::EndOfYear(e) => do_calculate(*e), TT::EndOfMonth(e) => do_calculate(*e), @@ -614,6 +602,7 @@ fn end_of_minute(tt: TimeType) -> Result { } } +#[allow(clippy::boxed_local)] fn add(a: Box, b: Box) -> Result { use timetype::TimeType as TT; @@ -897,6 +886,7 @@ fn add_to_moment(mom: NaiveDateTime, tt: TimeType) -> Result { } } +#[allow(clippy::boxed_local)] fn sub(a: Box, b: Box) -> Result { use timetype::TimeType as TT; @@ -1196,7 +1186,7 @@ mod tests { assert_eq!(0, a.get_seconds()); assert_eq!(1, b.get_seconds()); } - _ => assert!(false, "Addition failed, returned non-Addition type"), + _ => panic!("Addition failed, returned non-Addition type"), } } @@ -1216,10 +1206,10 @@ mod tests { assert_eq!(1, b.get_seconds()); assert_eq!(2, c.get_seconds()); }, - _ => assert!(false, "Addition failed, returned non-Addition type"), + _ => panic!("Addition failed, returned non-Addition type"), } } - _ => assert!(false, "Addition failed, returned non-Addition type"), + _ => panic!("Addition failed, returned non-Addition type"), } } @@ -1235,7 +1225,7 @@ mod tests { assert_eq!(5, a.get_seconds()); assert_eq!(3, b.get_seconds()); } - _ => assert!(false, "Subtraction failed, returned non-Subtraction type"), + _ => panic!("Subtraction failed, returned non-Subtraction type"), } } @@ -1255,10 +1245,10 @@ mod tests { assert_eq!(2, b.get_seconds()); assert_eq!(1, c.get_seconds()); }, - _ => assert!(false, "Subtraction failed"), + _ => panic!("Subtraction failed"), } } - _ => assert!(false, "Subtraction failed, returned non-Subtraction type"), + _ => panic!("Subtraction failed, returned non-Subtraction type"), } } @@ -1328,7 +1318,7 @@ mod tests { assert_eq!(0, a.get_minutes()); assert_eq!(1, b.get_minutes()); } - _ => assert!(false, "Addition failed, returned non-Addition type"), + _ => panic!("Addition failed, returned non-Addition type"), } } @@ -1348,10 +1338,10 @@ mod tests { assert_eq!(1, b.get_minutes()); assert_eq!(2, c.get_minutes()); }, - _ => assert!(false, "Addition failed, returned non-Addition type"), + _ => panic!("Addition failed, returned non-Addition type"), } } - _ => assert!(false, "Addition failed, returned non-Addition type"), + _ => panic!("Addition failed, returned non-Addition type"), } } @@ -1367,7 +1357,7 @@ mod tests { assert_eq!(5, a.get_minutes()); assert_eq!(3, b.get_minutes()); } - _ => assert!(false, "Subtraction failed, returned non-Subtraction type"), + _ => panic!("Subtraction failed, returned non-Subtraction type"), } } @@ -1387,10 +1377,10 @@ mod tests { assert_eq!(2, b.get_minutes()); assert_eq!(1, c.get_minutes()); }, - _ => assert!(false, "Subtraction failed, returned non-Subtraction type"), + _ => panic!("Subtraction failed, returned non-Subtraction type"), } } - _ => assert!(false, "Subtraction failed, returned non-Subtraction type"), + _ => panic!("Subtraction failed, returned non-Subtraction type"), } } @@ -1460,7 +1450,7 @@ mod tests { assert_eq!(0, a.get_days()); assert_eq!(1, b.get_days()); } - _ => assert!(false, "Addition failed, returned non-Addition type"), + _ => panic!("Addition failed, returned non-Addition type"), } } @@ -1480,10 +1470,10 @@ mod tests { assert_eq!(1, b.get_days()); assert_eq!(2, c.get_days()); }, - _ => assert!(false, "Addition failed, wrong type"), + _ => panic!("Addition failed, wrong type"), } } - _ => assert!(false, "Addition failed, returned non-Addition type"), + _ => panic!("Addition failed, returned non-Addition type"), } } @@ -1499,7 +1489,7 @@ mod tests { assert_eq!(5, a.get_days()); assert_eq!(3, b.get_days()); } - _ => assert!(false, "Subtraction failed, returned non-Subtraction type"), + _ => panic!("Subtraction failed, returned non-Subtraction type"), } } @@ -1519,10 +1509,10 @@ mod tests { assert_eq!(2, b.get_days()); assert_eq!(1, c.get_days()); }, - _ => assert!(false, "Subtraction failed, wrong type"), + _ => panic!("Subtraction failed, wrong type"), } } - _ => assert!(false, "Subtraction failed, returned non-Subtraction type"), + _ => panic!("Subtraction failed, returned non-Subtraction type"), } } @@ -1592,7 +1582,7 @@ mod tests { assert_eq!(0, a.get_months()); assert_eq!(1, b.get_months()); } - _ => assert!(false, "Addition failed, returned non-Addition type"), + _ => panic!("Addition failed, returned non-Addition type"), } } @@ -1612,10 +1602,10 @@ mod tests { assert_eq!(1, b.get_months()); assert_eq!(2, c.get_months()); }, - _ => assert!(false, "Addition failed, wrong type"), + _ => panic!("Addition failed, wrong type"), } } - _ => assert!(false, "Addition failed, returned non-Addition type"), + _ => panic!("Addition failed, returned non-Addition type"), } } @@ -1631,7 +1621,7 @@ mod tests { assert_eq!(5, a.get_months()); assert_eq!(3, b.get_months()); } - _ => assert!(false, "Subtraction failed, returned non-Subtraction type"), + _ => panic!("Subtraction failed, returned non-Subtraction type"), } } @@ -1651,10 +1641,10 @@ mod tests { assert_eq!(2, b.get_months()); assert_eq!(1, c.get_months()); }, - _ => assert!(false, "Subtraction failed, wrong type"), + _ => panic!("Subtraction failed, wrong type"), } } - _ => assert!(false, "Subtraction failed, returned non-Subtraction type"), + _ => panic!("Subtraction failed, returned non-Subtraction type"), } } @@ -1724,7 +1714,7 @@ mod tests { assert_eq!(0, a.get_years()); assert_eq!(1, b.get_years()); } - _ => assert!(false, "Addition failed, returned non-Addition type"), + _ => panic!("Addition failed, returned non-Addition type"), } } @@ -1744,10 +1734,10 @@ mod tests { assert_eq!(1, b.get_years()); assert_eq!(2, c.get_years()); }, - _ => assert!(false, "Addition failed, wrong type"), + _ => panic!("Addition failed, wrong type"), } } - _ => assert!(false, "Addition failed, returned non-Addition type"), + _ => panic!("Addition failed, returned non-Addition type"), } } @@ -1763,7 +1753,7 @@ mod tests { assert_eq!(5, a.get_years()); assert_eq!(3, b.get_years()); } - _ => assert!(false, "Subtraction failed, returned non-Subtraction type"), + _ => panic!("Subtraction failed, returned non-Subtraction type"), } } @@ -1783,10 +1773,10 @@ mod tests { assert_eq!(2, b.get_years()); assert_eq!(1, c.get_years()); }, - _ => assert!(false, "Subtraction failed, wrong type"), + _ => panic!("Subtraction failed, wrong type"), } } - _ => assert!(false, "Subtraction failed, returned non-Subtraction type"), + _ => panic!("Subtraction failed, returned non-Subtraction type"), } } @@ -1890,33 +1880,27 @@ mod tests { #[test] fn test_add_moment_to_seconds() { let a = TT::seconds(3); - let b = TT::moment(NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11)); + let b = TT::moment(NaiveDate::from_ymd_opt(2016, 7, 8).expect("Static time").and_hms_opt(9, 10, 11).expect("Static time")); let res = (a + b).calculate(); assert!(res.is_err()); let res = res.unwrap_err(); - assert!(match res { - Error::CannotAdd(..) => true, - _ => false, - }); + assert!(matches!(res, Error::CannotAdd(..))); } #[test] fn test_subtract_moment_from_seconds() { let a = TT::seconds(3); - let b = TT::moment(NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11)); + let b = TT::moment(NaiveDate::from_ymd_opt(2016, 7, 8).expect("Static time").and_hms_opt(9, 10, 11).expect("Static time")); let res = (a - b).calculate(); assert!(res.is_err()); let res = res.unwrap_err(); - assert!(match res { - Error::CannotSub(..) => true, - _ => false, - }); + assert!(matches!(res, Error::CannotSub(..))); } } @@ -2040,7 +2024,6 @@ mod timetype_value_tests { #[cfg(test)] mod moment_plus_amount_tests { - use env_logger; use super::TimeType as TT; use chrono::NaiveDate; @@ -2057,6 +2040,7 @@ mod moment_plus_amount_tests { let base = TT::moment($base); debug!("Using base = {:?}", base); debug!(" + {:?}", $amount); + #[allow(clippy::redundant_closure_call)] let result = $op(base, $amount).calculate(); debug!(" -> = {:?}", result); assert!(result.is_ok(), "Operation failed: {:?}", result); @@ -2108,250 +2092,252 @@ mod moment_plus_amount_tests { generate_test_moment_plus_amount! { name = test_moment_plus_zero_seconds; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::seconds(0); - expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_seconds; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::seconds(1); - expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 1); + expected = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 1).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_too_much_seconds; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::seconds(62); - expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 1, 2); + expected = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 1, 2).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_minutes; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::minutes(2); - expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 2, 0); + expected = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 2, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_too_much_minutes; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::minutes(65); - expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(1, 5, 0); + expected = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(1, 5, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_minutes_in_seconds; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::seconds(62); - expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 1, 2); + expected = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 1, 2).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_months; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::months(14); - expected = NaiveDate::from_ymd(2001, 3, 1).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2001, 3, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_years; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::years(62); - expected = NaiveDate::from_ymd(2062, 1, 1).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2062, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_year; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::years(1) + TT::months(1); - expected = NaiveDate::from_ymd(2001, 2, 1).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2001, 2, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_month; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); // As we calculate 1 month + 1 day first, we end up adding 31 days to the base amount = TT::months(1) + TT::days(1); // and therefor this results in the date 2000-02-01 // This is not that inuitive, of course. - expected = NaiveDate::from_ymd(2000, 2, 1).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2000, 2, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_day; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::days(1) + TT::hours(1); - expected = NaiveDate::from_ymd(2000, 1, 2).and_hms(1, 0, 0); + expected = NaiveDate::from_ymd_opt(2000, 1, 2).expect("Static time").and_hms_opt(1, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_hour; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::hours(1) + TT::minutes(1); - expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(1, 1, 0); + expected = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(1, 1, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_minute; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::minutes(1) + TT::seconds(1); - expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 1, 1); + expected = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 1, 1).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_invalid_months; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::months(13); - expected = NaiveDate::from_ymd(2001, 2, 1).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2001, 2, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_invalid_days; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::days(31); - expected = NaiveDate::from_ymd(2000, 2, 1).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2000, 2, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_invalid_hours; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::hours(25); - expected = NaiveDate::from_ymd(2000, 1, 2).and_hms(1, 0, 0); + expected = NaiveDate::from_ymd_opt(2000, 1, 2).expect("Static time").and_hms_opt(1, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_invalid_minutes; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::minutes(61); - expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(1, 1, 0); + expected = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(1, 1, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_invalid_seconds; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::seconds(61); - expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 1, 1); + expected = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 1, 1).expect("Static time"); } generate_test_moment_minus_amount! { name = test_moment_minus_nothing; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::seconds(0); - expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_minus_amount! { name = test_moment_minus_seconds; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::seconds(1); - expected = NaiveDate::from_ymd(1999, 12, 31).and_hms(23, 59, 59); + expected = NaiveDate::from_ymd_opt(1999, 12, 31).expect("Static time").and_hms_opt(23, 59, 59).expect("Static time"); } generate_test_moment_minus_amount! { name = test_moment_minus_months; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::months(12); - expected = NaiveDate::from_ymd(1999, 1, 1).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(1999, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_minute_in_seconds; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::seconds(130); - expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 2, 10); + expected = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 2, 10).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_hour_in_minutes; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::minutes(130); - expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(2, 10, 00); + expected = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(2, 10, 00).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_day_in_hours_1; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::hours(50); - expected = NaiveDate::from_ymd(2000, 1, 3).and_hms(2, 0, 0); + expected = NaiveDate::from_ymd_opt(2000, 1, 3).expect("Static time").and_hms_opt(2, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_day_in_hours_2; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::hours(170); - expected = NaiveDate::from_ymd(2000, 1, 8).and_hms(2, 0, 0); + expected = NaiveDate::from_ymd_opt(2000, 1, 8).expect("Static time").and_hms_opt(2, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_month_in_days_1; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::days(80); - expected = NaiveDate::from_ymd(2000, 3,21).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2000, 3,21).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_month_in_days_2; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::days(120); - expected = NaiveDate::from_ymd(2000, 4,30).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2000, 4,30).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_month_in_days_3; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::days(150); - expected = NaiveDate::from_ymd(2000, 5,30).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2000, 5,30).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_year_in_months_1; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::months(15); - expected = NaiveDate::from_ymd(2001, 4, 1).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2001, 4, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_year_in_months_2; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::months(25); - expected = NaiveDate::from_ymd(2002, 2, 1).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2002, 2, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_year_in_months_3; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::months(78); - expected = NaiveDate::from_ymd(2006, 7, 1).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2006, 7, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_year_in_months_4; - base = NaiveDate::from_ymd(2000,10,31).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000,10,31).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::months(4); - expected = NaiveDate::from_ymd(2001, 3, 1).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2001, 3, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_year_in_months_5; - base = NaiveDate::from_ymd(2000,10,31).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000,10,31).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::months(5); - expected = NaiveDate::from_ymd(2001, 4, 1).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2001, 4, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount! { name = test_moment_plus_more_than_one_year_in_months_6; - base = NaiveDate::from_ymd(2000,10,31).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000,10,31).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::months(4) + TT::months(1); - expected = NaiveDate::from_ymd(2001, 4, 1).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2001, 4, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } } #[cfg(test)] mod test_time_adjustments { + #![allow(clippy::identity_op)] + use super::adjust_times_add; use super::adjust_times_sub; @@ -2533,6 +2519,7 @@ mod test_end_of_year { #[test] fn $name() { let base = TT::moment($base); + #[allow(clippy::redundant_closure_call)] let result = $op(base, $amount).end_of_year().calculate(); assert!(result.is_ok(), "Operation failed: {:?}", result); let result = result.unwrap(); @@ -2583,154 +2570,154 @@ mod test_end_of_year { generate_test_moment_plus_amount_and_end_of_year! { name = test_moment_plus_zero_seconds; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::seconds(0); - expected = NaiveDate::from_ymd(2000, 12, 31).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2000, 12, 31).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount_and_end_of_year! { name = test_moment_plus_seconds; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::seconds(1); - expected = NaiveDate::from_ymd(2000, 12, 31).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2000, 12, 31).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount_and_end_of_year! { name = test_moment_plus_too_much_seconds; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::from_ymd_opt(2000, 1, 1).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); amount = TT::seconds(62); - expected = NaiveDate::from_ymd(2000, 12, 31).and_hms(0, 0, 0); + expected = NaiveDate::from_ymd_opt(2000, 12, 31).expect("Static time").and_hms_opt(0, 0, 0).expect("Static time"); } generate_test_moment_plus_amount_and_end_of_year! { name = test_moment_plus_minutes; - base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + base = NaiveDate::