parse not crons
This commit is contained in:
@@ -502,6 +502,126 @@ impl Delta {
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
struct Task(serde_yaml::Value);
|
||||
|
||||
impl Task {
|
||||
pub fn next_due(&self) -> Option<u64> {
|
||||
match self.schedule() {
|
||||
Some(schedule) => self.parse_schedule_next(schedule),
|
||||
None => Some(1),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_schedule_next(&self, schedule: String) -> Option<u64> {
|
||||
let mut schedule = schedule;
|
||||
|
||||
if regex::Regex::new(r"^[0-9]+h$").unwrap().is_match(&schedule) {
|
||||
let hours = &schedule[..schedule.len() - 1];
|
||||
match hours.parse::<u64>() {
|
||||
Ok(hours) => return Some(Delta::now_time() + hours * 60 * 60),
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
|
||||
if regex::Regex::new(r"[0-9]{4}-[0-9]{2}-[0-9]{2}$")
|
||||
.unwrap()
|
||||
.is_match(&schedule)
|
||||
{
|
||||
schedule += "T00";
|
||||
}
|
||||
|
||||
eprintln!("0 PARSING DATE HOUR {}", &schedule);
|
||||
if regex::Regex::new(r"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}$")
|
||||
.unwrap()
|
||||
.is_match(&schedule)
|
||||
{
|
||||
eprintln!("1 PARSING DATE HOUR {} like %Y-%m-%dT%H", &schedule);
|
||||
let date = schedule.clone() + ":00:00";
|
||||
match chrono::NaiveDateTime::parse_from_str(&date, "%Y-%m-%dT%H:%M:%S") {
|
||||
Ok(datehour) => {
|
||||
eprintln!("2 PARSING DATE HOUR {}", &schedule);
|
||||
let seconds = datehour.format("%s").to_string();
|
||||
match seconds.parse::<u64>() {
|
||||
Ok(n) => return Some(n),
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
Err(msg) => panic!("{}", msg),
|
||||
};
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn schedule(&self) -> Option<String> {
|
||||
match &self.0 {
|
||||
serde_yaml::Value::Mapping(m) => match m.get("schedule".to_string()) {
|
||||
Some(schedule) => match schedule {
|
||||
serde_yaml::Value::String(s) => Some(s.clone()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_task {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_unscheduled() {
|
||||
let task = Task(serde_yaml::Value::String("hello world".to_string()));
|
||||
assert_eq!(None, task.schedule());
|
||||
assert_eq!(Some(1 as u64), task.next_due());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scheduled_date_before() {
|
||||
let mut m = serde_yaml::Mapping::new();
|
||||
m.insert("schedule".into(), "2006-01-02".into());
|
||||
let task = Task(serde_yaml::Value::Mapping(m));
|
||||
assert_eq!(Some("2006-01-02".to_string()), task.schedule());
|
||||
assert_eq!(Some(1136160000 as u64), task.next_due());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scheduled_date_after() {
|
||||
let mut m = serde_yaml::Mapping::new();
|
||||
m.insert("schedule".into(), "2036-01-02".into());
|
||||
let task = Task(serde_yaml::Value::Mapping(m));
|
||||
assert_eq!(Some("2036-01-02".to_string()), task.schedule());
|
||||
assert_eq!(Some(2082844800 as u64), task.next_due());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scheduled_hour_after() {
|
||||
let mut m = serde_yaml::Mapping::new();
|
||||
m.insert("schedule".into(), "2036-01-02T16".into());
|
||||
let task = Task(serde_yaml::Value::Mapping(m));
|
||||
assert_eq!(Some("2036-01-02T16".to_string()), task.schedule());
|
||||
assert_eq!(Some(2082902400 as u64), task.next_due());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scheduled_duration() {
|
||||
let mut m = serde_yaml::Mapping::new();
|
||||
m.insert("schedule".into(), "1h".into());
|
||||
let task = Task(serde_yaml::Value::Mapping(m));
|
||||
assert_eq!(Some("1h".to_string()), task.schedule());
|
||||
assert_eq!(Some(Delta::now_time() + 60 * 60 as u64), task.next_due());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scheduled_cron() {
|
||||
let mut m = serde_yaml::Mapping::new();
|
||||
m.insert("schedule".into(), "* * * * *".into());
|
||||
let task = Task(serde_yaml::Value::Mapping(m));
|
||||
assert_eq!(Some("* * * * *".to_string()), task.schedule());
|
||||
assert_eq!(Some((Delta::now_time()) + 60 % 60 as u64), task.next_due());
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Events(Vec<Delta>);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user