state

Attribute Macro state 

Source
#[state]
Expand description

Generates application state with entity collections.

§Syntax

#[stately::state]
pub struct AppState {
    // Regular collections (many entities)
    pipelines: Pipeline,
    sources: SourceConfig,

    // Singletons (one entity)
    #[singleton]
    parse_settings: BufferSettings,
}

§API Generation

Optionally generate web API code by specifying the api attribute:

#[stately::state(api = ["axum"])]
pub struct AppState {
    pipelines: Pipeline,
}

This generates a namespaced module (e.g., axum_api) with:

  • Concrete response types with OpenAPI schemas
  • Handler functions with OpenAPI path annotations
  • A router function
  • An OpenAPI documentation struct

§Generated Code

This generates:

  • StateEntry enum with variants for each entity type
  • Entity enum wrapping each entity for type erasure
  • The state struct with collection fields
  • CRUD operation methods
  • (Optional) API-specific modules for web frameworks