summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPro <twisted.fall@gmail.com>2024-02-09 20:20:25 +0100
committerPro <twisted.fall@gmail.com>2024-02-09 20:20:25 +0100
commitfbf408b649322c7587dbda34656774a458c2ad46 (patch)
tree7c6d1f713805355abbd982a25bf9e8b826dd11d1
parente4443d26abd7831cf5deae840aeb5aff5cb1a575 (diff)
Cleanup
-rw-r--r--src/indicator.rs26
-rw-r--r--src/iter.rs27
-rw-r--r--src/parser/iterator.rs91
-rw-r--r--src/parser/mod.rs2
-rw-r--r--src/parser/timetype.rs96
-rw-r--r--src/timetype.rs687
-rw-r--r--src/util.rs92
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<Day> 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<u32> for Month {
- fn into(self) -> u32 {
- match self {
+impl From<Month> 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<I, M> Iterator for FilterIter<I, M>
}
pub trait EveryFilter<M: Matcher> : Iterator<Item = Result<TimeType>> + Sized {
- fn every(self, M) -> FilterIter<Self, M>;
+ fn every(self, matcher: M) -> FilterIter<Self, M>;
}
impl<I, M> EveryFilter<M> for I
@@ -171,7 +171,7 @@ impl<I, M> Iterator for WithoutIter<I, M>
}
pub trait WithoutFilter<M: Matcher> : Iterator<Item = Result<TimeType>> + Sized {
- fn without(self, M) -> WithoutIter<Self, M>;
+ fn without(self, matcher: M) -> WithoutIter<Self, M>;
}
impl<I, M> WithoutFilter<M> for I
@@ -221,7 +221,7 @@ impl<I> Iterator for UntilIter<I>
}
pub trait Until : Iterator<Item = Result<TimeType>> + Sized {
- fn until(self, NaiveDateTime) -> UntilIter<Self>;
+ fn until(self, ending: NaiveDateTime) -> UntilIter<Self>;
}
impl<I> Until for I
@@ -247,7 +247,7 @@ impl<I> TimesIter<I>
fn new(i: I, times: i64) -> TimesIter<I> {
TimesIter {
inner: i,
- times: times,
+ times,
count: 0,
}
}
@@ -269,7 +269,7 @@ impl<I> Iterator for TimesIter<I>
}
pub trait Times : Iterator<Item = Result<TimeType>> + Sized {
- fn times(self, i64) -> TimesIter<Self>;
+ fn times(self, times: i64) -> TimesIter<Self>;
}
impl<I> 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<I> ::std::iter::Iterator for UserIterator<I>
#[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<Parsed> {
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<Unit> for UnitAlias {
- fn into(self) -> Unit {
- match self {
+impl From<UnitAlias> 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<AmountExpr>, 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<TimeType> {
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<TimeType> {
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<TimeType> {
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<TimeType> {
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<TimeType> {
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<TimeType> {
}
}
+#[allow(clippy::boxed_local)]
fn add(a: Box<TimeType>, b: Box<TimeType>) -> Result<TimeType> {
use timetype::TimeType as TT;
@@ -897,6 +886,7 @@ fn add_to_moment(mom: NaiveDateTime, tt: TimeType) -> Result<TimeType> {
}
}
+#[allow(clippy::boxed_local)]
fn sub(a: Box<TimeType>, b: Box<TimeType>) -> Result<TimeType> {
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"),
}
}
@@ -