use crate::utils::sign::{L1Action, Map};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ScheduleCancel {
time: Option<u64>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error("a scheduled cancel at {time} is under five seconds away; the earliest is {earliest}")]
pub struct TooSoon {
pub time: u64,
pub earliest: u64,
}
impl ScheduleCancel {
pub const LEAD_MS: u64 = 5_000;
pub const DAILY_TRIGGERS: u32 = 10;
pub const fn arm(time: u64, now: u64) -> Result<ScheduleCancel, TooSoon> {
let earliest = now.saturating_add(ScheduleCancel::LEAD_MS);
match time >= earliest {
true => Ok(ScheduleCancel { time: Some(time) }),
false => Err(TooSoon { time, earliest }),
}
}
#[must_use]
pub const fn at(time: u64) -> ScheduleCancel {
ScheduleCancel { time: Some(time) }
}
#[must_use]
pub const fn disarm() -> ScheduleCancel {
ScheduleCancel { time: None }
}
#[must_use]
pub const fn time(&self) -> Option<u64> {
self.time
}
}
impl L1Action for ScheduleCancel {
fn action(&self) -> Map {
Map::new().put("type", "scheduleCancel").put_some("time", self.time)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Noop;
impl L1Action for Noop {
fn action(&self) -> Map {
Map::new().put("type", "noop")
}
}
#[cfg(test)]
#[path = "tests/schedule.rs"]
mod tests;