RuState
A Rust implementation of statecharts inspired by XState. RuState provides a type-safe way to model and implement finite state machines and statecharts in Rust.
Features
- ✅ Finite state machines and statecharts
- ✅ Hierarchical states
- ✅ Parallel states
- ✅ Guards/conditions for transitions
- ✅ Actions/side effects
- ✅ Context (extended state)
- ✅ Typesafe API
- ✅ Serializable machines
Usage Example
Simple State Machine
use ;
// Create states
let green = new;
let yellow = new;
let red = new;
// Create transitions
let green_to_yellow = new;
let yellow_to_red = new;
let red_to_green = new;
// Define actions
let log_green = new;
// Build the machine
let mut machine = new
.state
.state
.state
.initial
.transition
.transition
.transition
.on_entry
.build
.unwrap;
// Send an event to the machine
machine.send.unwrap;
Hierarchical State Machine
use ;
// Create hierarchical states
let power_off = new;
let mut player = new_compound;
player.parent = Some;
let mut stopped = new;
stopped.parent = Some;
let mut playing = new_compound;
playing.parent = Some;
// Create transitions
let power_toggle = new;
let play = new;
// Create context
let mut context = new;
context.set.unwrap;
// Create a machine with hierarchical states
let mut machine = new
.initial
.state
.state
.state
.state
.transition
.transition
.context
.build
.unwrap;
See the examples directory for complete examples.
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Documentation
Core Concepts
- State: Represents a node in the state chart
- Transition: Defines how the machine moves between states in response to events
- Guard: Conditional logic that determines if a transition should occur
- Action: Side effects that execute during state transitions
- Context: Stores extended state for the machine
API Overview
State: Create simple, compound, parallel, or history statesTransition: Define transitions between states, including guards and actionsGuard: Create guard conditions for transitionsAction: Define actions/side effects for state transitionsContext: Store and retrieve extended state dataMachine: The runtime state machine instanceMachineBuilder: Fluent API for creating state machines
License
MIT