3 releases (breaking)
Uses old Rust 2015
0.3.0 | May 19, 2017 |
---|---|
0.2.0 | Mar 1, 2017 |
0.1.0 | Feb 28, 2017 |
#193 in #state-machine
9KB
215 lines
fxsm
A very simple state machine procedural macro for enums.
How does it work
It simply generates match conditions on your enums in appropriate StateMachine
trait's methods.
You may use the state machine through a StateMachine
trait.
Usage
- Add
fxsm
as dependency in yourCargo.toml
:
[dependencies]
fxsm-derive = "0.2"
fxsm = "0.2"
- Create a State Machine:
#[macro_use]
extern crate fxsm_derive;
extern crate fxsm;
#[derive(Clone, Debug, FiniteStateMachine)]
enum CupState {
#[state_transitions(Checkins, Aborted, Rescheduled)]
Waiting,
#[state_transitions(InProgress, Aborted, Rescheduled)]
Checkins,
#[state_transitions(Finished, Aborted, Rescheduled)]
InProgress(String),
// Finish-states
Aborted(u64),
Rescheduled { info: String },
Finished,
}
- Use it:
fn main() {
use fxsm::{ StateMachine };
let mut fsm = CupState::Waiting;
assert_eq!(CupState::finish_states(), 3);
// must not be able to change to itself
assert!(!fsm.can_change(CupState::Waiting));
assert!(fsm.can_change(CupState::Checkins));
assert!(!fsm.can_change(CupState::InProgress(String::default())));
assert!(fsm.can_change(CupState::Aborted(0u64)));
assert!(fsm.can_change(CupState::Rescheduled{info: String::default()}));
assert!(!fsm.can_change(CupState::Finished));
assert!(!fsm.at_finish_state());
assert!(fsm.change(CupState::Checkins));
assert!(!fsm.can_change(CupState::Waiting));
assert!(fsm.can_change(CupState::Aborted(0u64)));
assert!(fsm.can_change(CupState::Rescheduled{info: String::default()}));
assert!(fsm.can_change(CupState::InProgress(String::default())));
assert!(!fsm.can_change(CupState::Finished));
assert!(!fsm.at_finish_state());
// You still always able to change it without FSM rules:
fsm = CupState::Finished;
assert!(fsm.at_finish_state());
assert!(CupState::is_finish_state(CupState::Finished));
assert!(CupState::is_finish_state(CupState::Aborted(0u64)));
assert!(CupState::is_finish_state(CupState::Rescheduled{ info: String::default()}));
assert!(!CupState::is_finish_state(CupState::Waiting));
assert!(!CupState::is_finish_state(CupState::Checkins));
assert!(!CupState::is_finish_state(CupState::InProgress(String::default())));
}
More and updated examples are in examples directory.
License
This project is licensed under the MIT license.
Dependencies
~1.5MB
~41K SLoC