style(sched): 💄 lint and format

This commit is contained in:
Lucas Colombo 2024-04-07 23:39:03 -03:00
parent 9b0faace04
commit da3dd37eed
Signed by: lucas
GPG Key ID: EF34786CFEFFAE35
12 changed files with 95 additions and 88 deletions

View File

@ -7,3 +7,4 @@ scopes:
- "cli"
- "logger"
- "macros"
- "sched"

View File

@ -3,7 +3,7 @@ mod cron;
#[cfg(feature = "sched-rule-recurrent")]
mod recurrent;
#[cfg(feature = "sched-rule-recurrent")]
pub use self::recurrent::{many, val, range, ranges, ruleset, RecurrenceRuleSet, Rule};
pub use self::recurrent::{many, range, ranges, ruleset, val, RecurrenceRuleSet, Rule};
use chrono::{DateTime, Local};

View File

@ -1,2 +1 @@
// TODO: cron based scheduling

View File

@ -1,8 +1,10 @@
#[cfg(test)]
mod tests;
mod ruleset;
mod rule_unit;
mod ruleset;
pub use ruleset::{RecurrenceRuleSet, builder::ruleset};
pub use rule_unit::{Rule, val, range, many, ranges};
pub use {
rule_unit::{many, range, ranges, val, Rule},
ruleset::{builder::ruleset, RecurrenceRuleSet},
};

View File

@ -37,10 +37,7 @@ impl RecurrenceRuleSet {
/// 🧉 » returns the next match of the rule set from a given `DateTime`
pub fn next_match_from(&self, from: DateTime<Local>) -> Option<DateTime<Local>> {
let next = self._next_match(from);
match next {
Some(date) => Some(date.date()),
None => None,
}
next.map(|date| date.date())
}
/// 🚧 internal
@ -51,12 +48,12 @@ impl RecurrenceRuleSet {
// check year
if let Some(Rule::Val(year)) = self.year {
if year < from.year().into() {
if year < from.year() {
return None;
}
}
let mut next = LoolDate::new(from.clone());
let mut next = LoolDate::new(from);
next.add_second();
loop {
@ -125,4 +122,3 @@ impl RecurrenceRuleSet {
Some(next)
}
}

View File

@ -1,11 +1,10 @@
use chrono::Weekday;
use super::RecurrenceRuleSet;
use crate::sched::rules::Rule;
use {super::RecurrenceRuleSet, crate::sched::rules::Rule};
pub fn ruleset() -> RecurrenceRuleSet {
let ruleset = RecurrenceRuleSet::recurring();
ruleset
RecurrenceRuleSet::recurring()
}
impl RecurrenceRuleSet {

View File

@ -1,3 +1,3 @@
mod recurrence_rules_by_val;
mod recurrence_rules_by_range;
mod recurrence_rules_by_many;
mod recurrence_rules_by_range;
mod recurrence_rules_by_val;

View File

@ -1,10 +1,10 @@
use chrono::{Datelike, Local, TimeZone, Utc};
use chrono::{Datelike, Local, TimeZone};
use crate::sched::rules::{many, range, ruleset};
#[test]
fn every_day_at_12_and_15() {
let date = Local.with_ymd_and_hms(2024, 4, 7, 13, 15, 05).unwrap();
let date = Local.with_ymd_and_hms(2024, 4, 7, 13, 15, 5).unwrap();
let mut rules = ruleset();
rules.hours_rule(many(vec![12, 15])).at_minute(0).at_second(0);
@ -28,12 +28,12 @@ fn every_day_at_12_and_15() {
#[test]
fn each_day_between_9_and_17_at_hour_start() {
// will start next day because it's already > 17:00:00
let date = Local.with_ymd_and_hms(2024, 4, 25, 19, 15, 05).unwrap();
let date = Local.with_ymd_and_hms(2024, 4, 25, 19, 15, 5).unwrap();
let mut rules = ruleset();
rules.hours_rule(range(9, 17, 1)).at_minute(0).at_second(0);
let mut next = date.clone();
let mut next = date;
let initial_day = date.day() + 1;
for i in 0..18 {

View File

@ -4,12 +4,12 @@ use crate::sched::rules::{range, ruleset};
#[test]
fn between_10_and_20_seconds() {
let date = Local.with_ymd_and_hms(2024, 4, 7, 16, 15, 05).unwrap();
let date = Local.with_ymd_and_hms(2024, 4, 7, 16, 15, 5).unwrap();
let mut rules = ruleset();
rules.seconds_rule(range(10, 20, 1));
let mut next = date.clone();
let mut next = date;
let initial_minute = date.minute();
for i in 0..20 {
@ -33,12 +33,12 @@ fn between_10_and_20_seconds() {
#[test]
fn each_day_between_9_and_17_at_hour_start() {
// will start next day because it's already > 17:00:00
let date = Local.with_ymd_and_hms(2024, 4, 25, 19, 15, 05).unwrap();
let date = Local.with_ymd_and_hms(2024, 4, 25, 19, 15, 5).unwrap();
let mut rules = ruleset();
rules.hours_rule(range(9, 17, 1)).at_minute(0).at_second(0);
let mut next = date.clone();
let mut next = date;
let initial_day = date.day() + 1;
for i in 0..18 {

View File

@ -6,12 +6,12 @@ use crate::sched::rules::ruleset;
fn at_second_1_of_each_minute() {
// we have passed the second 1 of the minute
// so it should go to the next minute
let date = Local.with_ymd_and_hms(2024, 4, 7, 16, 15, 05).unwrap();
let date = Local.with_ymd_and_hms(2024, 4, 7, 16, 15, 5).unwrap();
let mut rules = ruleset();
rules.at_second(1);
let mut next = date.clone();
let mut next = date;
let initial_minute = date.minute();
for i in 0..10 {
@ -20,7 +20,7 @@ fn at_second_1_of_each_minute() {
// should match 16:16:01, 16:17:01, 16:18:01, ...
assert_eq!(
next,
Local.with_ymd_and_hms(2024, 4, 7, 16, initial_minute + i + 1, 01).unwrap()
Local.with_ymd_and_hms(2024, 4, 7, 16, initial_minute + i + 1, 1).unwrap()
);
}
}
@ -29,12 +29,12 @@ fn at_second_1_of_each_minute() {
fn at_hour_1_of_each_day() {
// we have passed the hour 1 of the day
// so it should go to the next day
let date = Local.with_ymd_and_hms(2024, 4, 25, 16, 15, 05).unwrap();
let date = Local.with_ymd_and_hms(2024, 4, 25, 16, 15, 5).unwrap();
let mut rules = ruleset();
rules.at_time(1, 0, 0);
let mut next = date.clone();
let mut next = date;
let mut initial_day = date.day() as i32;
let mut initial_month = date.month();
@ -55,9 +55,9 @@ fn at_hour_1_of_each_day() {
2024,
initial_month,
(initial_day + i + 1) as u32,
01,
00,
00
1,
0,
0
)
.unwrap()
);
@ -68,12 +68,12 @@ fn at_hour_1_of_each_day() {
fn each_1st_of_month() {
// we have passed the 1st of the month
// so it should go to the next month
let date = Local.with_ymd_and_hms(2024, 5, 7, 16, 15, 05).unwrap();
let date = Local.with_ymd_and_hms(2024, 5, 7, 16, 15, 5).unwrap();
let mut rules = ruleset();
rules.on_day(1).at_time(0, 0, 0);
let mut next = date.clone();
let mut next = date;
let mut initial_month = date.month() as i32;
let mut initial_year = date.year();
@ -90,7 +90,7 @@ fn each_1st_of_month() {
assert_eq!(
next,
Local
.with_ymd_and_hms(initial_year, (initial_month + i + 1) as u32, 01, 00, 00, 00)
.with_ymd_and_hms(initial_year, (initial_month + i + 1) as u32, 1, 0, 0, 0)
.unwrap()
);
}
@ -100,20 +100,20 @@ fn each_1st_of_month() {
fn each_wednesday() {
// we have passed the Wednesday
// so it should go to the next Wednesday
let date = Local.with_ymd_and_hms(2024, 4, 7, 16, 15, 05).unwrap();
let date = Local.with_ymd_and_hms(2024, 4, 7, 16, 15, 5).unwrap();
let mut next_wednesday = Local.with_ymd_and_hms(2024, 4, 10, 0, 0, 0).unwrap();
let mut rules = ruleset();
rules.on_dow(Weekday::Wed).at_time(0, 0, 0);
let mut next = date.clone();
let mut next = date;
for _ in 0..10 {
next = rules.next_match_from(next).unwrap();
println!("next: {:?}", next);
assert_eq!(next, next_wednesday);
next_wednesday = next_wednesday + chrono::Duration::days(7);
next_wednesday += chrono::Duration::days(7);
}
}
@ -121,12 +121,12 @@ fn each_wednesday() {
fn from_31th_may_schedule_first_of_each_june() {
// we have passed the 31th of May
// so it should go to the 1st of June
let date = Local.with_ymd_and_hms(2024, 5, 31, 16, 15, 05).unwrap();
let date = Local.with_ymd_and_hms(2024, 5, 31, 16, 15, 5).unwrap();
let mut rules = ruleset();
rules.in_month(6).on_day(1).at_time(0, 0, 0);
let mut next = date.clone();
let mut next = date;
let initial_year = date.year();
let initial_month = date.month();
@ -138,7 +138,7 @@ fn from_31th_may_schedule_first_of_each_june() {
// should match 2024-06-01 00:00:00, 2024-07-01 00:00:00, 2024-08-01 00:00:00, ...
assert_eq!(
next,
Local.with_ymd_and_hms(initial_year + i, initial_month + 1, 01, 00, 00, 00).unwrap()
Local.with_ymd_and_hms(initial_year + i, initial_month + 1, 1, 0, 0, 0).unwrap()
);
}
}
@ -150,7 +150,7 @@ fn from_1st_may_schedule_first_of_each_june() {
let mut rules = ruleset();
rules.in_month(6).on_day(1).at_time(0, 0, 0);
let mut next = date.clone();
let mut next = date;
let initial_year = date.year();
for i in 0..10 {
@ -159,7 +159,7 @@ fn from_1st_may_schedule_first_of_each_june() {
assert_eq!(
next,
Local.with_ymd_and_hms(initial_year + i + 1, 06, 01, 00, 00, 00).unwrap()
Local.with_ymd_and_hms(initial_year + i + 1, 6, 1, 0, 0, 0).unwrap()
);
}
}

View File

@ -54,25 +54,25 @@ impl<Tz: TimeZone> LoolDate<Tz> {
/// adds `1` day to the current date
pub fn add_day(&mut self) {
self.date = self.date.clone() + Duration::days(1);
self.date += Duration::days(1);
self.set_start_of(TimeUnit::Day);
}
/// adds `1` hour to the current date
pub fn add_hour(&mut self) {
self.date = self.date.clone() + Duration::hours(1);
self.date += Duration::hours(1);
self.set_start_of(TimeUnit::Hour);
}
/// adds `1` minute to the current date
pub fn add_minute(&mut self) {
self.date = self.date.clone() + Duration::minutes(1);
self.date += Duration::minutes(1);
self.set_start_of(TimeUnit::Minute);
}
/// adds `1` second to the current date
pub fn add_second(&mut self) {
self.date = self.date.clone() + Duration::seconds(1);
self.date += Duration::seconds(1);
self.set_start_of(TimeUnit::Second);
}
@ -88,22 +88,22 @@ impl<Tz: TimeZone> LoolDate<Tz> {
/// subtracts `1` day from the current date
pub fn subs_day(&mut self) {
self.date = self.date.clone() - Duration::days(1);
self.date -= Duration::days(1);
}
/// subtracts `1` hour from the current date
pub fn subs_hour(&mut self) {
self.date = self.date.clone() - Duration::hours(1);
self.date -= Duration::hours(1);
}
/// subtracts `1` minute from the current date
pub fn subs_minute(&mut self) {
self.date = self.date.clone() - Duration::minutes(1);
self.date -= Duration::minutes(1);
}
/// subtracts `1` second from the current date
pub fn subs_second(&mut self) {
self.date = self.date.clone() - Duration::seconds(1);
self.date -= Duration::seconds(1);
}
/// returns the day of the month starting from `1`
@ -181,7 +181,11 @@ impl<Tz: TimeZone> LoolDate<Tz> {
pub fn set_nanos(&mut self, nanos: u32) {
// avoid `whith_nanosecond` returning None for > 2_000_000_000 values
// so we can safely unwrap the result
let nanos = if nanos > 2_000_000_000 { 1_999_999_999 } else { nanos };
let nanos = if nanos > 2_000_000_000 {
1_999_999_999
} else {
nanos
};
self.date = self.date.with_nanosecond(nanos).unwrap();
}
@ -189,8 +193,12 @@ impl<Tz: TimeZone> LoolDate<Tz> {
///
/// values greater than `2,000,000` will be clamped to `1,999,999`
pub fn set_micros(&mut self, micros: u32) {
let micros = if micros > 2_000_000 { 1_999_999 } else { micros };
self.date = self.date.with_nanosecond(micros as u32 * 1_000).unwrap();
let micros = if micros > 2_000_000 {
1_999_999
} else {
micros
};
self.date = self.date.with_nanosecond(micros * 1_000).unwrap();
}
/// sets the milliseconds since the last second change
@ -347,32 +355,32 @@ impl<Tz: TimeZone> LoolDate<Tz> {
self.set_minute(0);
self.set_second(0);
self.set_nanos(0);
},
}
TimeUnit::Month => {
self.set_day(1);
self.set_hour(0);
self.set_minute(0);
self.set_second(0);
self.set_nanos(0);
},
}
TimeUnit::Day => {
self.set_hour(0);
self.set_minute(0);
self.set_second(0);
self.set_nanos(0);
},
}
TimeUnit::Hour => {
self.set_minute(0);
self.set_second(0);
self.set_nanos(0);
},
}
TimeUnit::Minute => {
self.set_second(0);
self.set_nanos(0);
},
}
TimeUnit::Second => {
self.set_nanos(0);
},
}
};
}
}

View File

@ -58,8 +58,10 @@ pub fn tz_to_s(offset: &str) -> Result<i32> {
#[cfg(test)]
mod tests {
use eyre::{set_hook, DefaultHandler};
use super::*;
use {
super::*,
eyre::{set_hook, DefaultHandler},
};
fn setup_eyre() {
let _ = set_hook(Box::new(DefaultHandler::default_with));