microstate::microstate!
[-] [+]
[src]
macro_rules! microstate ( ( $machine:ident { $initial:ident } states { $($states:ident),* } $($meth:ident { $($from:ident => $to:ident)* })* ) => ( #[allow(non_snake_case)] pub mod $machine { #[derive(Clone,PartialEq,Eq,Debug)] pub enum State { __InvalidState__, // Just be able to match _ further down $($states),* } #[derive(PartialEq,Eq,Debug)] pub struct Machine { state: State, } pub fn new() -> Machine { Machine::new() } impl Machine { pub fn new() -> Machine { Machine { state: State::$initial } } pub fn state(&self) -> State { self.state.clone() } $(pub fn $meth(&mut self) -> Option<State> { match self.state { $( State::$from => { self.state = State::$to; Some(State::$to) } ),* _ => None } })* } } ));
Create a new state machine
It takes a name, the initial value, all possible states and the transitions.
See the main documentation for a proper example.