Rust-FSM
A statically checked finite state machine written in rust.
Motivation
Because I can. And I wanted to practice creating proc macros in rust. They can be fun...
Usage
To create a state machine, use the fsm macro.
fsm!
// --snip--
// Initialize the finite state machine
let mut fsm = FSMnew;
// run transition and save new struct
fsm = fsm.transition_k;
let f =
// You can pass a callback function to be called with the transition
fsm = fsm.transition_l_fn;
// Call the end function. Confirm that the FSM reached a final state.
fsm.end;
// or pass a closure
f_end =
fsm.end_fn;
How does it work
The macro creates types for all the states and an FSM struct. The FSM struct is what the users should use. The state types allow it to define and restrict what transitions/functions can be called.
Sample generated code
type struct state_1;
...
type struct
type struct Transition
...
Issues
- Sanitization
- The generated code is placed in the same module as the macro call. It should ideally create a new module and place the new struct there.
- Macros and LSP
- Macros don't have the greatest lsp support. Doesn't always give intellisence and might not warn about bad code.
Features to add
- Provide function to print to FSM to a graph format
- probably will stick to dot for now
- Check for unreachable states
- should give some sort of warning then...
- Allow for multiple transition labels on the same line
- something like:
A -> B: a,b,c,...
- something like: