Crates.io | simple_statemachine |
lib.rs | simple_statemachine |
version | 1.0.0 |
source | src |
created_at | 2023-03-26 16:45:41.22089 |
updated_at | 2023-03-26 16:45:41.22089 |
description | Statemachine defined via a simple and easy-to-read domain-specific language |
homepage | |
repository | https://github.com/garin1000/simple_statemachine |
max_upload_size | |
id | 821395 |
size | 79,934 |
This rust crate defines the statemachine!()
macro. As the single argument the macro takes the definition of a
statemachine, described in a simple and easy to ready DSL.
The statemachine macro supports guards, actions on triggers, state entry and exit. Events may carry payload, e.g. for implementation of communication systems.
The full documentation is available on docs.rs.
Add this to your Cargo.toml:
[dependencies]
simple-statemachine = "1.0"
This implements the pedestrian part of a simple traffic light with a button to switch the traffic light to "walk".
use std::thread;
use std::time::Duration;
use simple_statemachine::statemachine;
statemachine!{
Name TrafficLightStatemachine
InitialState DontWalk
DontWalk {
ButtonPressed ==set_button_to_pressed=> DontWalk
TimerFired[check_button_is_pressed] ==switch_to_walk=> Walk
TimerFired => DontWalk
}
Walk {
OnEntry set_button_to_not_pressed
TimerFired ==switch_to_dont_walk=> DontWalk
}
}
struct LightSwitch{
button_pressed:bool
}
impl TrafficLightStatemachineHandler for LightSwitch{
fn check_button_is_pressed(&self) -> bool {
self.button_pressed
}
fn set_button_to_pressed(&mut self) {
self.button_pressed=true;
}
fn set_button_to_not_pressed(&mut self) {
self.button_pressed=false;
}
fn switch_to_walk(&mut self) {
println!("Switching \"Don't Walk\" off");
println!("Switching \"Walk\" on");
}
fn switch_to_dont_walk(&mut self) {
println!("Switching \"Walk\" off");
println!("Switching \"Don't Walk\" on");
}
}