Expand description
§Fluxo Typestate Macros
This crate contains the procedural macros for the Fluxo Typestate library.
It is not intended to be used directly; instead, use the fluxo-typestate crate.
§Overview
The fluxo-typestate-macros crate provides the implementation of the
#[state_machine] procedural macro. This macro transforms enum definitions
into complete type-state pattern implementations.
§What the Macro Generates
Given an enum like:
ⓘ
#[state_machine]
enum Computer {
Idle,
Running { cpu_load: f32 },
Sleeping,
}The macro generates:
- State Structs: For each variant, a struct representing that state
- Main Wrapper: A generic
Computer<S>struct - State Trait Implementations: Implementations of
StateandSealed - Transition Methods: Methods for each defined transition
- Constructor: A
new()method for the initial state - Visualization: A
mermaid_diagram()method
§Internal Structure
The macro implementation is organized as follows:
TransitionInfo: Parsed transition attribute dataVariantIr: Intermediate representation of an enum variantFieldsIr: Representation of variant fieldsgenerate_*: Functions that generate different parts of the output
§Transition Attributes
Transitions are defined using the #[transition] attribute:
ⓘ
#[transition(SourceState -> TargetState: method_name)]This syntax means: “from SourceState, you can transition to TargetState
by calling method_name()”.
The macro supports two syntaxes:
- Short form:
#[transition(Idle -> Running: start)] - Full form:
#[transition(Computer::Idle -> Computer::Running: start)]
§Attributes
The state machine macro supports several attributes:
#[state_machine]: Required attribute to enable the macro#[transition(...)]: Define a state transition#[trace]: Enable tracing of state transitions (requiresloggingfeature)#[visualize]: Enable Mermaid diagram generation
Attribute Macros§
- state_
machine - The main entry point for the
#[state_machine]attribute macro.