use crate::builder::TypeBuilder;
use crate::testing::error::TestingError;
use crate::testing::specs::csp::Event;
use std::time::Duration;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Deadline {
pub duration: Duration,
pub start_event: Event,
pub end_event: Event,
pub min_slack: Option<Duration>,
}
#[derive(Debug, Default, Clone)]
pub struct DeadlineBuilder {
duration: Option<Duration>,
start_event: Option<Event>,
end_event: Option<Event>,
min_slack: Option<Duration>,
}
impl DeadlineBuilder {
pub fn with_duration(mut self, duration: Duration) -> Self {
self.duration = Some(duration);
self
}
pub fn with_start_event(mut self, start_event: Event) -> Self {
self.start_event = Some(start_event);
self
}
pub fn with_end_event(mut self, end_event: Event) -> Self {
self.end_event = Some(end_event);
self
}
pub fn with_min_slack(mut self, min_slack: Duration) -> Self {
self.min_slack = Some(min_slack);
self
}
}
impl TypeBuilder<Deadline> for DeadlineBuilder {
type Error = TestingError;
fn build(self) -> Result<Deadline, Self::Error> {
let duration = self.duration.ok_or(TestingError::InvalidTimingConstraint)?;
let start_event = self.start_event.ok_or(TestingError::InvalidTimingConstraint)?;
let end_event = self.end_event.ok_or(TestingError::InvalidTimingConstraint)?;
if let Some(slack) = self.min_slack {
if slack > duration {
return Err(TestingError::InvalidSlack);
}
}
Ok(Deadline { duration, start_event, end_event, min_slack: self.min_slack })
}
}