#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Schedule {
Cron(&'static str),
Every(&'static str),
}
#[derive(Debug)]
pub struct Job {
name: &'static str,
path: &'static str,
schedule: Schedule,
title: Option<&'static str>,
}
impl Job {
pub const fn new(name: &'static str, path: &'static str, schedule: Schedule) -> Self {
Self {
name,
path,
schedule,
title: None,
}
}
pub const fn title(mut self, title: &'static str) -> Self {
self.title = Some(title);
self
}
pub const fn name(&self) -> &'static str {
self.name
}
pub const fn path(&self) -> &'static str {
self.path
}
pub const fn schedule(&self) -> Schedule {
self.schedule
}
}
#[cfg(test)]
mod tests {
use super::*;
static CLEANUP: Job = Job::new("cleanup", "/internal/cleanup", Schedule::Cron("0 4 * * *"))
.title("Nightly cleanup");
static REFRESH: Job = Job::new("refresh-rates", "/internal/rates", Schedule::Every("30m"));
#[test]
fn a_job_is_declared_in_a_static() {
assert_eq!(CLEANUP.name(), "cleanup");
assert_eq!(CLEANUP.path(), "/internal/cleanup");
assert_eq!(CLEANUP.schedule(), Schedule::Cron("0 4 * * *"));
assert_eq!(CLEANUP.title, Some("Nightly cleanup"));
assert_eq!(REFRESH.schedule(), Schedule::Every("30m"));
assert_eq!(REFRESH.title, None);
}
}