use std::future::IntoFuture;
use crate::context::DurableContext;
use crate::engine::OperationId;
use crate::error::OperationError;
use crate::future::DurableFuture;
#[must_use = "builders do nothing unless awaited or spawned"]
#[non_exhaustive]
pub struct WaitBuilder {
ctx: DurableContext,
op_id: OperationId,
name: Option<String>,
duration_secs: i32,
}
impl std::fmt::Debug for WaitBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WaitBuilder")
.field("name", &self.name)
.finish_non_exhaustive()
}
}
impl WaitBuilder {
pub(crate) fn new_internal(
ctx: DurableContext,
op_id: OperationId,
duration_secs: i32,
) -> Self {
Self {
ctx,
op_id,
name: None,
duration_secs,
}
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn future(self) -> DurableFuture<()> {
self.into_future()
}
pub fn spawn(self) -> DurableFuture<()> {
spawn_terminal!(self, started_awaited)
}
fn started_future(self) -> DurableFuture<()> {
match self.execution() {
Ok(execution) => DurableFuture::for_started_operation(
&execution.ctx.clone(),
execution.execute_started(),
),
Err(future) => *future,
}
}
fn execution(self) -> Result<crate::wait::WaitExecution, Box<DurableFuture<()>>> {
if let Err(err) = self.ctx.preflight_replay_identity(
&self.op_id,
"Wait",
Some(crate::wait::WAIT_SUB_TYPE),
self.name.as_deref(),
) {
return Err(Box::new(DurableFuture::from_async(async move { Err(err) })));
}
Ok(crate::wait::WaitExecution {
ctx: self.ctx,
op_id: self.op_id,
name: self.name,
duration_secs: self.duration_secs,
})
}
}
impl IntoFuture for WaitBuilder {
type Output = Result<(), OperationError>;
type IntoFuture = DurableFuture<()>;
fn into_future(self) -> Self::IntoFuture {
match self.execution() {
Ok(execution) => DurableFuture::for_start_step(&execution.ctx.clone(), move || {
execution.execute_started()
}),
Err(future) => *future,
}
}
}