simple_statemachine

Crates.iosimple_statemachine
lib.rssimple_statemachine
version1.0.0
sourcesrc
created_at2023-03-26 16:45:41.22089
updated_at2023-03-26 16:45:41.22089
descriptionStatemachine defined via a simple and easy-to-read domain-specific language
homepage
repositoryhttps://github.com/garin1000/simple_statemachine
max_upload_size
id821395
size79,934
(garin1000)

documentation

README

Simple Statemachine

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.

Usage

Add this to your Cargo.toml:

[dependencies]
simple-statemachine = "1.0"

Example

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");
    }
}
Commit count: 4

cargo fmt