#![deny(missing_docs)]
use smlang::statemachine;
statemachine! {
transitions: {
*State1 + Event1 [guard] / action1 = State2,
State2 + Event2 [guard_fail] / action2 = State3,
}
}
pub struct Context;
impl StateMachineContext for Context {
fn guard(&self) -> Result<bool, ()> {
Ok(true)
}
fn guard_fail(&self) -> Result<bool, ()> {
Ok(false)
}
fn action1(&mut self) -> Result<(), ()> {
Ok(())
}
fn action2(&mut self) -> Result<(), ()> {
Ok(())
}
}
fn main() {
let mut sm = StateMachine::new(Context);
assert!(matches!(sm.state(), &States::State1));
println!("Before action 1");
let r = sm.process_event(Events::Event1);
assert!(matches!(r, Ok(&States::State2)));
println!("After action 1");
println!("Before action 2");
let r = sm.process_event(Events::Event2);
assert!(matches!(r, Err(Error::TransitionsFailed)));
println!("After action 2");
assert!(matches!(sm.state(), &States::State2));
}