Skip to main content

Crate fluxo_typestate_macros

Crate fluxo_typestate_macros 

Source
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:

  1. State Structs: For each variant, a struct representing that state
  2. Main Wrapper: A generic Computer<S> struct
  3. State Trait Implementations: Implementations of State and Sealed
  4. Transition Methods: Methods for each defined transition
  5. Constructor: A new() method for the initial state
  6. Visualization: A mermaid_diagram() method

§Internal Structure

The macro implementation is organized as follows:

  • TransitionInfo: Parsed transition attribute data
  • VariantIr: Intermediate representation of an enum variant
  • FieldsIr: Representation of variant fields
  • generate_*: 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 (requires logging feature)
  • #[visualize]: Enable Mermaid diagram generation

Attribute Macros§

state_machine
The main entry point for the #[state_machine] attribute macro.