Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cemoktra/rusty_state
A simple state machine for rust
https://github.com/cemoktra/rusty_state
rust state-machine
Last synced: 22 days ago
JSON representation
A simple state machine for rust
- Host: GitHub
- URL: https://github.com/cemoktra/rusty_state
- Owner: cemoktra
- License: mit
- Created: 2020-10-15T18:45:44.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2020-11-20T08:52:11.000Z (about 4 years ago)
- Last Synced: 2024-12-01T02:22:51.034Z (about 1 month ago)
- Topics: rust, state-machine
- Language: Rust
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://github.com/cemoktra/rusty_state/workflows/CI/badge.svg)](https://github.com/cemoktra/rusty_state/actions)
# rusty_state
A simple state machine for rust## Example
Copied from [tests](https://github.com/cemoktra/rusty_state/blob/main/src/tests.rs)```rust
// define an traffic light enumeration
#[derive(Clone, Copy, PartialEq)]
enum TrafficLight {
Red,
RedYellow,
Green,
Yellow
}// implement State trait for state enumeration
impl State 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 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));
}
```