use crate::error::Result;
use crate::store::Store;
use crate::workers::{Run, Step};
pub fn step() -> StepBuilder<'static> {
StepBuilder::new()
}
pub struct StepBuilder<'a> {
run: Option<&'a Run>,
name: Option<String>,
current_time: Option<chrono::DateTime<chrono::Utc>>,
}
impl<'a> StepBuilder<'a> {
pub fn new() -> Self {
Self {
run: None,
name: None,
current_time: None,
}
}
pub fn run(mut self, run: &'a Run) -> Self {
self.run = Some(run);
self
}
pub fn name(mut self, name: &str) -> Self {
self.name = Some(name.to_string());
self
}
pub fn id(self, id: &str) -> Self {
self.name(id)
}
pub fn with_time(mut self, current_time: chrono::DateTime<chrono::Utc>) -> Self {
self.current_time = Some(current_time);
self
}
pub fn store<S: Store>(self, _store: &S) -> Self {
self
}
pub async fn execute(self) -> Result<Step> {
let run = self
.run
.ok_or_else(|| crate::error::Error::ValidationFailed {
reason: "Run handle is required for StepBuilder::execute".to_string(),
})?;
let name = self
.name
.ok_or_else(|| crate::error::Error::ValidationFailed {
reason: "Step name is required for StepBuilder::execute".to_string(),
})?;
let current_time = self.current_time.unwrap_or_else(chrono::Utc::now);
run.acquire_step(&name, current_time).await
}
}
impl<'a> Default for StepBuilder<'a> {
fn default() -> Self {
Self::new()
}
}