Crates.io | macro-machines |
lib.rs | macro-machines |
version | 0.10.6 |
source | src |
created_at | 2018-04-29 09:06:55.808342 |
updated_at | 2024-08-25 13:49:19.134103 |
description | State machine macros with logging and graphviz DOT file generation |
homepage | |
repository | https://github.com/spearman/macro-machines |
max_upload_size | |
id | 62977 |
size | 225,054 |
macro-machines
State machine macros with logging and graphviz dotfile generation
Current features
Macros for creating state machines with and without Debug
and Default
:
def_machine!
-- state machine implementing Default
constructiondef_machine_debug!
-- state machine implementing Default
construction
and deriving Debug
def_machine_nodefault!
-- state machine requiring initialization
argumentsdef_machine_nodefault_debug!
-- state machine requiring initialization
arguments and deriving Debug
Specified initial (required) and terminal (optional) states with (optional) initialization and termination actions
States may have local state variables and state machines may have extended state variables
Events with optional associated parameters and actions in which extended state variables may be modified:
Syntax allowing use of constrained type parameters in the types of extended state variables
Logging using the log
logging API
Graphviz DOT file generation of state machine transition diagrams
Define and use a minimal state machine:
use macro_machines::def_machine_debug;
def_machine_debug!{
machine M {
STATES [
state S ()
state T ()
]
EVENTS [
event A <S> => <T> ()
]
EXTENDED []
initial_state: S
}
}
fn main () {
use macro_machines::HandleEventException;
let mut m = M::initial();
m.handle_event (EventId::A.into()).unwrap();
assert_eq!(m.handle_event (EventId::A.into()), Err (HandleEventException::WrongState));
}
Generate a dotfile and write to file:
use std::io::Write;
use macro_machines::MachineDotfile;
let mut f = std::fs::File::create ("minimal.dot").unwrap();
f.write_all (M::dotfile().as_bytes()).unwrap();
drop (f);
Rendered as PNG with $ dot -Tpng minimal.dot > minimal.png
:
For examples of more complex state machines, see the ./examples/
directory.