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, with full support for model-based testing (MBT).
Features
- ✅ Finite state machines and statecharts
- ✅ Hierarchical states
- ✅ Parallel states
- ✅ Guards/conditions for transitions
- ✅ Actions/side effects
- ✅ Context (extended state)
- ✅ Typesafe API
- ✅ Serializable machines
- ✅ Model-based testing support
- ✅ Cross-crate integration patterns
Model-Based Testing Integration
RuState now includes comprehensive model-based testing features:
- Automated Test Generation: Generate test cases from your state machine model
- Test Execution: Run tests directly against your state machine or export them
- Coverage Analysis: Measure state and transition coverage
- Model Checking: Verify properties like reachability, safety, and liveness
Key MBT Components:
- TestGenerator: Creates test cases for states, transitions, and loop coverage
- TestRunner: Executes test cases against your machine
- ModelChecker: Verifies model properties and detects deadlocks and unreachable states
Cross-Crate Integration Patterns
RuState provides patterns for integrating state machines across crates with type safety:
- Event Forwarding Pattern: Share state machine references and forward events between machines
- Context Sharing Pattern: Share context data between multiple state machines
- Hierarchical Integration Pattern: Connect parent-child state machines with traits
Enable with the integration feature:
[]
= { = "0.2.2", = ["integration"] }
Integration Example
use ;
// Create and share a state machine
let machine = create_machine;
let shared_machine = new;
// Forward events
shared_machine.send_event?;
See the examples/integration directory for complete integration examples.
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;
Model-Based Testing Example
use ;
// Assuming you have a state machine defined as above...
let machine = /* ... */;
// Generate test cases
let mut generator = new;
let test_cases = generator.generate_all_transitions;
// Run tests
let mut runner = new;
let results = runner.run_tests;
println!;
// Coverage analysis
let coverage = results.get_coverage;
println!;
println!;
// Model checking
let mut checker = new;
// Define property to check
let property = Property ;
// Verify the property
let verification = checker.verify_property;
if verification.satisfied else
// Detect deadlocks
let deadlocks = checker.detect_deadlocks;
println!;
See the examples directory for complete examples.
Installation
Add this to your Cargo.toml:
[]
= "0.2.2"
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
- TestGenerator: Creates test cases from your state machine model
- TestRunner: Executes test cases against your machine
- ModelChecker: Verifies properties and analyzes your state machine model
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 machinesTestGenerator: Generate test cases from a state machine modelTestRunner: Run tests against your state machineModelChecker: Verify properties and analyze your state machine model
Future Directions
- Advanced model checking algorithms
- Property-based testing integration
- Test visualization tools
- Fuzzing-based MBT
- Temporal logic (LTL/CTL) property specification and verification
- Performance optimizations for large state machines
- ✅ Distributed system state machine coordination
- Enhanced WebAssembly (WASM) support
- Integration with visual state machine editors
- Automatic state machine model generation from existing systems
- Advanced concurrency model support
- Domain-specific language (DSL) for state machine definition
- Microcontroller-optimized version
License
MIT