Crates.io | rusty_state |
lib.rs | rusty_state |
version | 0.1.1 |
source | src |
created_at | 2020-10-15 19:07:36.90134 |
updated_at | 2020-11-20 08:43:54.577622 |
description | A simple state machine for rust |
homepage | https://github.com/cemoktra/rusty_state |
repository | |
max_upload_size | |
id | 300145 |
size | 7,877 |
A simple state machine for rust
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));
}