Crates.io | microstate |
lib.rs | microstate |
version | 1.0.0 |
source | src |
created_at | 2015-06-13 12:01:21.81417 |
updated_at | 2016-01-15 22:24:03.655122 |
description | Finite state machine, inspired by micromachine |
homepage | https://github.com/badboy/microstate |
repository | https://github.com/badboy/microstate |
max_upload_size | |
id | 2369 |
size | 7,497 |
Minimal Finite State Machine.
A finite state machine is in only one state at a time. From there it can change from one state to another when initiated by an triggering event: the transition. A finite state machine is fully defined by a list of states and the transitions triggering a change from one state to another.
And that's all this crate does: it let's you define the states and transitions. The rest is up to you.
Inspired by @soveran's micromachine in Ruby.
First you need to import the macro:
#[macro_use] extern crate microstate;
You can then create a new state machine and call transitions.
microstate!{
MicroMachine { New };
states { New, Confirmed, Ignored };
confirm {
New => Confirmed
}
ignore {
New => Ignored
}
reset {
Confirmed => New
Ignored => New
}
}
let mut machine = MicroMachine::new();
machine.confirm(); // => Some(Confirmed)
machine.state(); // => Confirmed
machine.ignore(); // => None
machine.state(); // => Confirmed
machine.reset(); // => Some(New)
machine.state(); // => New
machine.ignore(); // => Some(Ignored)
machine.state(); // => Ignored
If you find bugs or want to help otherwise, please open an issue.
MIT. See LICENSE.