use sfsm::*; use std::marker::PhantomData; #[derive(Debug)] pub struct Ascent {} #[derive(Debug)] pub struct Descent {} #[derive(Debug)] pub struct WaitForLaunch {} #[derive(Debug)] pub struct Action { phantom: PhantomData } add_state_machine!( #[derive(Debug)] // Attributes for the generated struct can be defined pub Rocket, // Name of the state machine. Accepts a visibility modifier. Action, // The state machine will start at the count Descent [Action, Action, Action], // All possible states [ Action => Action, Action => Action ] ); // Use the derive macros to implement empty default implementations. // Check out the basic examples to know how to implement them manually derive_state!(Action); derive_state!(Action); derive_state!(Action); derive_transition!(Action, Action, TransitGuard::Transit); derive_transition!(Action, Action, TransitGuard::Transit); /// Register a logger function /// Enable the trace features for the tracing to work /// The logger function receives logs from the state machine and forwards them /// to what ever logging mechanism desired. #[sfsm_trace] fn trace(log: &str) { println!("{}", log); } impl Into> for Action { fn into(self) -> Action { Action { phantom: PhantomData }} } impl Into> for Action { fn into(self) -> Action { Action { phantom: PhantomData }} } fn run_basic_extended_example() -> Result<(), SfsmError> { let mut rocket = Rocket::new(); let wait_for_launch = Action {phantom: PhantomData}; rocket.start(wait_for_launch)?; assert!(IsState::>::is_state(&rocket)); rocket.step()?; assert!(IsState::>::is_state(&rocket)); rocket.step()?; assert!(IsState::>::is_state(&rocket)); rocket.step()?; let stopped_state = rocket.stop()?; match stopped_state { match_state_entry!(Rocket, Action, exit_state) => { // Access "exit_state" here println!("Exit state: {:?}", exit_state); assert!(true); } _ => { assert!(false); } } Ok(()) } fn main() { run_basic_extended_example().unwrap(); } #[cfg(test)] mod tests { use crate::run_basic_extended_example; #[test] fn basic_extended_example() { run_basic_extended_example().unwrap(); } }