2 releases
0.1.1 | Nov 20, 2020 |
---|---|
0.1.0 | Oct 15, 2020 |
#2654 in Rust patterns
7KB
86 lines
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));
}