5 releases (stable)

2.0.0 May 5, 2022
1.1.0 Apr 19, 2022
1.0.1 Apr 7, 2022
1.0.0 Apr 3, 2022
0.1.0 Mar 31, 2022

#2044 in Algorithms

MIT license

24KB
416 lines

Rust State Machine

Build

A (Finite) State Machine library

This Rust Library aims to provide a developer-friendly, Business-Oriented, (Finite) State Machine.

Features

  • Use any type for your States and Events
  • Define State Machines, even easier with the library's DSL
  • Apply events on states to know the output
  • Guard functions on transitions

More features coming Soon™

Examples

Quickly define your State Machine using the integrated DSL define!() macro:

fn main() {
    let light_switch = define!(
        "OFF" - "TURN_ON"  -> "ON",
        "ON"  - "TURN_OFF" -> "OFF"
    );
}

Use any type to define your States and Events, why not enums:

fn main() {
    let mut enum_light_switch = Machine::new();
    enum_light_switch.add_transition(Transition::new(LightState::Off, LightEvent::TurnOn, LightState::On));
    enum_light_switch.add_transition(Transition::new(LightState::On, LightEvent::TurnOff, LightState::Off));
    let mut state_light = StatefulLight { state: LightState::Off };

    enum_light_switch.apply(&mut state_light, LightEvent::TurnOn);
}

No runtime deps