rusty_state

Crates.iorusty_state
lib.rsrusty_state
version0.1.1
sourcesrc
created_at2020-10-15 19:07:36.90134
updated_at2020-11-20 08:43:54.577622
descriptionA simple state machine for rust
homepagehttps://github.com/cemoktra/rusty_state
repository
max_upload_size
id300145
size7,877
Bastian (cemoktra)

documentation

README

CI

rusty_state

A simple state machine for rust

Example

Copied from tests

// define an traffic light enumeration
#[derive(Clone, Copy, PartialEq)]
enum TrafficLight {
    Red,
    RedYellow,
    Green,
    Yellow
}

// implement State trait for state enumeration
impl State<TrafficLight> for TrafficLight {
    fn transition_allowed(self, new_state: TrafficLight) -> bool {
        match (self, new_state) {
            (TrafficLight::Red, TrafficLight::RedYellow) => true,
            (TrafficLight::RedYellow, TrafficLight::Green) => true,
            (TrafficLight::Green, TrafficLight::Yellow) => true,
            (TrafficLight::Yellow, TrafficLight::Red) => true,
            _ => false
        }
    }
}

#[derive(Clone, Copy, PartialEq)]
enum TrafficFeature {
    Drive
}

impl Feature<TrafficLight> for TrafficFeature {
    fn allowed(self, state: &TrafficLight) -> bool {
        match state {
            TrafficLight::Green => true,
            _ => false
        }
    }
}

// usage
#[test]
fn traffic_light_transitions() {
    let mut state_machine = StateMachine::new(TrafficLight::Red);
    assert!(state_machine.set(TrafficLight::RedYellow).is_ok());
    assert!(state_machine.set(TrafficLight::Green).is_ok());
    assert!(state_machine.set(TrafficLight::Yellow).is_ok());
    assert!(state_machine.set(TrafficLight::RedYellow).is_err());
}

#[test]
fn traffic_light_features() {
    let mut state_machine = StateMachine::new(TrafficLight::Red);
    assert!(!state_machine.feature_allowed(&TrafficFeature::Drive));

    assert!(state_machine.set(&TrafficLight::RedYellow).is_ok());
    assert!(!state_machine.feature_allowed(&TrafficFeature::Drive));

    assert!(state_machine.set(&TrafficLight::Green).is_ok());
    assert!(state_machine.feature_allowed(&TrafficFeature::Drive));

    assert!(state_machine.set(&TrafficLight::Yellow).is_ok());
    assert!(!state_machine.feature_allowed(&TrafficFeature::Drive));
}
Commit count: 0

cargo fmt