# mdeloof/statig architecture

> Architecture diagram of the GitHub repository mdeloof/statig, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mdeloof/statig
- Repository: https://github.com/mdeloof/statig

## Overview

##### Main components
a. User/Application Layer
- Your Rust code that invokes `#[state_machine]`, defines states, events, actions.
- Examples (`examples/macro/*`, `examples/no_macro/*`) and tests (`statig/tests/*`) live here.
b. Compile-time Proc-Macro Pipeline (`macro/` crate)
- Entry point: `macro/src/lib.rs` (the `#[state_machine]` procedural macro).
- Submodules:
- parse.rs – Builds a syn::DeriveInput AST
- .rs – Validates and collects state machine metadata
- lower.rs – Converts AST to an intermediate representation (IR)
- codegen.rs – Emits Rust code: trait impls for `State`, `Superstate`, `StateMachine`
- visitors/ – Helpers (generic lifetimes, parameters, etc.)
c. Runtime Library (`statig/` crate)
- lib.rs – Core traits and exports
- Modules:
- awaitable/ – Async state‐machine implementation
- blocking/ – Sync (blocking) state‐machine implementation
- response.rs – `Response` enum (`Handled`, `Transition`, `Super`)
- state_or_superstate.rs – Helper enum for introspection
- response.rs & response machinery
d. External Dependencies
- proc_macro, syn, quote, proc_macro2 (compile time)
- std/no_std (runtime feature)
- async (runtime feature)

##### Relationships and data flow
1. Your Rust source with `#[state_machine]`
2. Procedural macro entry (`macro/src/lib.rs`) invoked by Rust compiler
3. parse → analyze → lower → codegen → generate expanded Rust code
4. Expanded code plus `statig` runtime crate get compiled into the final binary
1. Main program calls `YourType::state_machine()` → returns a `StateMachine` instance
2. `state_machine.handle(event)` or `handle_with_context`
3. Dispatch to current leaf‐state handler
4. If `Super`, dispatch to its superstate, repeat until handled or top reached
5. On `Transition`, compute exit/entry sequence via shortest path in state hierarchy
6. Execute exit actions, then entry actions
c. Testing/Examples
- Examples drive both macro and runtime behavior
- Tests verify generated code vs hand‐written code

##### Architectural patterns
- Separation of concerns:
- Proc‐macro pipeline (compile time) vs runtime library
- Hierarchical state machine pattern (tree of states + superstates)
- Dual runtime implementations: async (`awaitable/`) vs sync (`blocking/`)
- No-std compatibility (zero-allocation, ROM-based machines)

##### Relevant technologies/frameworks
- Rust Proc-Macros: `proc_macro`, `syn`, `quote`
- Core library: `statig` crate
- Features: `async` (generates an async state‐machine), `no_std`

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User/Application Layer
    subgraph "User/Application Layer"
        ExamplesMacro["Examples (Macro)"]:::user
        ExamplesNoMacro["Examples (No-Macro)"]:::user
        Tests["Tests"]:::user
    end

    %% Proc-Macro Pipeline
    subgraph "Proc-Macro Pipeline"
        MacroEntry["macro/src/lib.rs (Entry Point)"]:::compile
        Parse["macro/src/parse.rs"]:::compile
        Analyze["macro/src/analyze.rs"]:::compile
        Lower["macro/src/lower.rs"]:::compile
        Codegen["macro/src/codegen.rs"]:::compile
        Visitors["macro/src/visitors/"]:::compile
        Generated["Generated Code"]:::compile
    end

    %% Runtime Library
    subgraph "Statig Runtime Library"
        Core["statig/src/lib.rs"]:::runtime
        Awaitable["statig/src/awaitable/"]:::async
        Blocking["statig/src/blocking/"]:::sync
        Response["statig/src/response.rs"]:::runtime
        StateHelper["statig/src/state_or_superstate.rs"]:::runtime
        Engine["StateMachine Engine"]:::runtime
    end

    %% Relationships
    ExamplesMacro -->|"macro expansion"| MacroEntry
    ExamplesNoMacro -->|"macro expansion"| MacroEntry
    Tests -->|"macro expansion"| MacroEntry

    MacroEntry --> Parse
    Parse --> Analyze
    Analyze --> Lower
    Lower --> Codegen
    Codegen --> Generated
    Parse --> Visitors
    Analyze --> Visitors
    Lower --> Visitors

    Generated -->|"compilation"| Core

    Core --> Engine
    Engine -->|async path| Awaitable
    Engine -->|sync path| Blocking
    Engine --> Response
    Engine --> StateHelper

    %% Runtime execution flow
    ExamplesMacro -->|"handle(event)"| Engine

    %% Click Events
    click ExamplesMacro "https://github.com/mdeloof/statig/tree/main/examples/macro/"
    click ExamplesNoMacro "https://github.com/mdeloof/statig/tree/main/examples/no_macro/"
    click Tests "https://github.com/mdeloof/statig/tree/main/statig/tests/"
    click MacroEntry "https://github.com/mdeloof/statig/blob/main/macro/src/lib.rs"
    click Parse "https://github.com/mdeloof/statig/blob/main/macro/src/parse.rs"
    click Analyze "https://github.com/mdeloof/statig/blob/main/macro/src/analyze.rs"
    click Lower "https://github.com/mdeloof/statig/blob/main/macro/src/lower.rs"
    click Codegen "https://github.com/mdeloof/statig/blob/main/macro/src/codegen.rs"
    click Visitors "https://github.com/mdeloof/statig/tree/main/macro/src/visitors/"
    click Core "https://github.com/mdeloof/statig/blob/main/statig/src/lib.rs"
    click Awaitable "https://github.com/mdeloof/statig/tree/main/statig/src/awaitable/"
    click Blocking "https://github.com/mdeloof/statig/tree/main/statig/src/blocking/"
    click Response "https://github.com/mdeloof/statig/blob/main/statig/src/response.rs"
    click StateHelper "https://github.com/mdeloof/statig/blob/main/statig/src/state_or_superstate.rs"

    %% Styles
    classDef user fill:#f96,stroke:#333,stroke-width:1px
    classDef compile fill:#ccc,stroke:#333,stroke-width:1px
    classDef runtime fill:#9cf,stroke:#333,stroke-width:1px
    classDef async fill:#8f8,stroke:#333,stroke-width:1px
    classDef sync fill:#88f,stroke:#333,stroke-width:1px
```
