YASM (Yet Another State Machine)
A simple and powerful Rust state machine library focused on usability and visualization.
Features
- 🚀 Easy to Use: Define state machines with macros using clean and concise syntax
- 📊 Visualization: Automatically generate Mermaid format state diagrams
- 🔍 Query Functions: Rich state machine query and analysis capabilities
- 📝 Documentation Generation: Automatically generate state transition tables and documentation
- 🛡️ Type Safety: Leverage Rust's type system to ensure correct state transitions
Quick Start
Installation and Running
# Clone the project
# Run basic demo
# Run advanced examples
# Generate documentation
Define State Machine
Use the define_state_machine!
macro to define state machines:
use *;
Using State Machine
// Create state machine instance
let mut door = new;
// Check current state
println!; // Closed
// Check valid inputs
println!; // [OpenDoor, Lock]
// Execute state transition
door.transition.unwrap;
println!; // Open
// View transition history
println!;
Query Functions
// Query all reachable states from a given state
let reachable = reachable_states;
println!;
// Query all states that can reach a target state
let leading_to = states_leading_to;
println!;
// Check if there's a path between two states
let has_path = has_path;
println!;
Generate Documentation
Mermaid State Diagram
let mermaid = generate_mermaid;
println!;
Output:
stateDiagram-v2
[*] --> Closed
Closed --> Open : OpenDoor
Open --> Closed : CloseDoor
Closed --> Locked : Lock
Locked --> Closed : Unlock
State Transition Table
let table = generate_transition_table;
println!;
Output:
Examples
The project includes multiple examples showcasing different use cases:
📖 Basic Demo
- Door state machine and order state machine
- Basic state transitions and queries
- Documentation generation demo
🚀 Advanced Usage
- Network connection state machine
- Game character state machine
- State machine analysis tools
📚 Documentation Generation
- Automatically generate Markdown documentation
- Output Mermaid diagram files
- Create complete project documentation
For more example details, see examples/README.md.
Running Tests
Project Structure
yasm/
├── src/
│ └── lib.rs # Core library implementation
├── examples/
│ ├── README.md # Examples documentation
│ ├── basic_demo.rs # Basic functionality demo
│ ├── advanced_usage.rs # Advanced usage examples
│ └── generate_docs.rs # Documentation generation tool
├── docs/ # Generated documentation
├── Cargo.toml
└── README.md
API Documentation
Core Traits
StateMachine
Core trait defining state machine behavior, including states, inputs, and transition logic.
StateMachineInstance<SM>
Runtime instance of a state machine that can execute transitions and record history.
Main methods:
new()
: Create new instancecurrent_state()
: Get current statevalid_inputs()
: Get valid inputs for current statecan_accept(input)
: Check if input is validtransition(input)
: Execute state transitionhistory()
: Get transition history
Query Tools
StateMachineQuery<SM>
Utility class providing state machine query functionality.
Main methods:
reachable_states(from)
: Get all states reachable from specified statestates_leading_to(target)
: Get all states that can reach target statehas_path(from, to)
: Check if path exists between two states
Documentation Generation
StateMachineDoc<SM>
Utility class for generating state machine documentation.
Main methods:
generate_mermaid()
: Generate Mermaid format state diagramgenerate_transition_table()
: Generate Markdown format state transition table
Design Principles
- MVP First: Current version focuses on core functionality while keeping it simple and easy to use
- Type Safety: Leverage Rust's type system to prevent invalid state transitions
- Extensible: Design allows for future additions (non-deterministic transitions, conditional transitions, etc.)
- Visualization: Built-in documentation generation for better understanding and debugging
Future Plans
- Support DSL syntax for defining state machines
- Support non-deterministic state transitions
- Support conditional transitions and guards
- Support state machine composition and nesting
- Support more output formats (GraphViz, PlantUML, etc.)
- Support state machine verification and analysis tools
License
MIT License