A simple Finite State Machine library in Rust. Provide State and Event types (usually enums), then generate a machine with an initial state, give it some transition behaviours and you have your state machine!
Usage
Using the simple coin-operated turnstyle example from the FSM wikipedia entry:
Define your states and events:
// states and events must be C-like enums (copyable and easily converted to primitives)
// implement the EnumTag trait for states and events
Create your machine and define your transitions:
// create the machine initially in the Locked state
let mut machine = new;
// create the transition from Locked -> Unlocked upon inserting a coin
machine.add_transition;
// create the transition from Unlocked -> Locked upon pushing the turnstyle
machine.add_transition;
Trigger events as needed and huzzah, off you go:
// initially we're in the Locked state
machine.on_event;
// now we're Unlocked, ("unlocked" was just printed)
machine.on_event;
// now we're Locked again, ("locked" was just printed)
This example is also the test case for the library, although here I've ommitted the test-related details.
To Do
- Expose predicate interface and write unit tests for it