use std::ops::ControlFlow;
use crate::{State, Transition};
pub trait Workflow {
fn state(&self) -> &dyn State;
}
pub trait WorkflowState<Result>: Workflow {
fn name(&self) -> String {
self.state().name()
}
fn next(self: Box<Self>) -> Transition<Result>;
fn run(self) -> Result
where
Self: WorkflowState<Result> + Sized,
{
let mut workflow: Box<dyn WorkflowState<Result>> = Box::new(self);
loop {
match workflow.next() {
ControlFlow::Continue(next) => workflow = next,
ControlFlow::Break(result) => return result,
}
}
}
fn finish(self, result: Result) -> Transition<Result>
where
Self: Sized,
{
ControlFlow::Break(result)
}
fn finish_with<Fn>(self, map_fn: Fn) -> Transition<Result>
where
Self: Sized,
Fn: FnOnce(Self) -> Result,
{
ControlFlow::Break(map_fn(self))
}
}