fsmentry
A code generator for finite state machines (FSMs) with the following features:
- Define your machine as a graph in e.g
DOT. - An
entryapi to transition the state machine. - Illegal states and transitions are unrepresentable.
- States can contain data.
- Custom
#[derive(..)]support. #![no_std]support.- Inline SVG diagrams of the state machine in docs.
// define the machine.
// you can also use the DOT language if you prefer.
dsl!
use ;
// instantiate the machine
let mut machine = new;
loop
Cargo features
macros(default): Include the [dot] and [dsl] macros.svg(default): The macros will shell out todot, if available, and generate a diagram of the state machine for documentation.std(default): Includes the [FSMGenerator], for custom codegen tools.cli: This does not affect the library, but if you
You will get ancargo install fsmentry --features=clifsmentrybinary that you can use to generate code.
Advanced usage
dsl!
use ;
// ^ A module with matching publicity is generated for the state machine.
// The `#[derive(..)]`s apply to the `State` and the `MyStateMachine` items.
let mut machine = arbitrary; // we can use derived traits!
// you can also inspect and mutate the state yourself.
let state: &State = machine.state;
let state: &mut State = machine.state_mut;
match machine.entry
Hierarchical state machines
fsmentry needs no special considerations for sub-state machines - simply store one
on the relevant node!
Here is the example from the statig crate:
┌─────────────────────────┐
│ Blinking │🞀─────────┐
│ ┌───────────────┐ │ │
│ ┌─🞂│ LedOn │──┐ │ ┌───────────────┐
│ │ └───────────────┘ │ │ │ NotBlinking │
│ │ ┌───────────────┐ │ │ └───────────────┘
│ └──│ LedOff │🞀─┘ │ 🞁
│ └───────────────┘ │──────────┘
└─────────────────────────┘
dsl!
dsl!
let mut machine = new;
loop
Comparison with other state machine libraries
| Crate | Illegal states/transitions unrepresentable | States contain data | State machine definition | Comments |
|---|---|---|---|---|
fsmentry |
Yes | Yes | Graph | |
sm |
Yes | No | States, events, transitions | |
rust-fsm |
No | Yes (manually) | States, events, transitions | |
finny |
No | Yes | Builder | |
sfsm |
No | No | States and transitions | |
statig |
? | ? | ? | Complicated API! |
sad_machine |
Yes | No | States, events, transitions | |
machine |
No | Yes | States, events, transitions |