use super::{integrations::FsmIntegration, Fsm, State};
pub struct FsmController<T>
where
T: FsmIntegration<T>,
{
fsm: Fsm<T>,
}
impl<T> FsmController<T>
where
T: FsmIntegration<T>,
{
pub fn new(fsm: Fsm<T>) -> Self {
Self { fsm }
}
pub fn goto(
&self,
state: impl State<T> + 'static,
guard: Option<Box<dyn Fn() -> bool>>,
wait: Option<bool>,
) -> bool {
if let Some(guard) = guard {
let allowed = guard();
if !allowed && !wait.unwrap_or(true) {
return false;
} else if !allowed {
self.goto(state, Some(guard), None);
return true;
}
}
self.fsm.goto(state)
}
pub fn current_state_name(&self) -> String {
self.fsm.current_state_name()
}
}