so close tho

master
Bel LaPointe 2024-05-15 23:27:03 -04:00
parent 2a04a030f7
commit 1639286189
1 changed files with 30 additions and 6 deletions

View File

@ -26,7 +26,10 @@ impl Task {
fn is_due_now(&self, now: TS) -> bool {
match self.when() {
Some(when) => now.unix() <= when.next(self.ts()).unix(),
Some(when) => {
eprintln!("next: {}", when.next(self.ts()).to_string());
now.unix() <= when.next(self.ts()).unix()
},
None => true,
}
}
@ -35,7 +38,10 @@ impl Task {
match self.get("schedule".to_string()) {
Some(v) => match When::new(v) {
Ok(when) => Some(when),
Err(_) => None,
Err(msg) => {
eprintln!("Task.when(): {}", msg);
return None;
},
},
None => None,
}
@ -55,9 +61,12 @@ impl Task {
match self.0.get(k) {
Some(v) => Some(
serde_yaml::to_string(v)
.unwrap()
.trim()
.to_string()
.unwrap()
.to_string()
.trim()
.trim_start_matches('\'')
.trim_end_matches( '\'')
.to_string()
),
None => None,
}
@ -84,6 +93,17 @@ mod test_task {
Some("v".to_string()),
);
}
#[test]
fn is_due() {
let mut t = Task::new();
assert!(t.is_due());
t.set("ts".to_string(), "61".to_string());
t.set("schedule".to_string(), "* * * * *".to_string());
assert!(!t.is_due_now(TS::from_unix(119)));
assert!(t.is_due_now(TS::from_unix(120)));
assert!(t.is_due_now(TS::from_unix(121)));
}
}
#[derive(Debug)]
@ -103,7 +123,7 @@ impl When {
Some(x) => { return Ok(x); },
None => {},
};
Err(format!("cannot parse when {}", src))
Err(format!("cannot parse when: {}", src))
}
fn new_duration(src: String) -> Option<When> {
@ -228,6 +248,10 @@ mod test_cron {
#[test]
fn parse() {
match Cron::new("* * * * *".to_string()) {
Ok(c) => {}
Err(err) => assert!(false, "failed to parse cron: {}", err),
};
match Cron::new("1 * * * *".to_string()) {
Ok(c) => assert_eq!(1714525200+60, c.next(TS::from_unix(1714525200)).unix()),
Err(err) => assert!(false, "failed to parse cron: {}", err),