Statum
Statum is a zero-boilerplate library for finite-state machines in Rust, with compile-time state transition validation. It provides two attribute macros:
#[state]for defining states (as enums).#[machine]for creating a state machine struct that tracks which state you’re in at compile time.
Quick Start (Minimal Example)
Here’s the simplest usage of Statum without any extra features:
use ;
// 1. Define your states as an enum.
// 2. Create a machine struct that references one of those states.
// 3. Implement transitions for each state.
How It Works
#[state]transforms your enum, generating one struct per variant (likeOffandOn), plus a traitLightState.#[machine]injects extra fields (marker,state_data) to track which state you’re in, letting you define transitions that change the state at the type level.
That’s it! You now have a compile-time guaranteed state machine where invalid transitions are impossible.
Additional Features & Examples
1. Adding Debug, Clone, or Other Derives
By default, you can add normal Rust derives on your enum and struct. For example:
Important: If you place #[derive(...)] above #[machine], you may see an error like:
error[E0063]: missing fields `marker` and `state_data` in initializer of `Light<_>`
|
14 | #[derive(Debug, Clone)]
| ^ missing `marker` and `state_data`
That’s because the derive macro for Clone, Debug, etc., expands before #[machine] has injected these extra fields. To avoid this, either:
- Put
#[machine]above the derive(s), or - Remove the conflicting derive(s) from the same item.
For example, this works:
2. serde Integration
Statum can optionally propagate Serialize/Deserialize derives if you enable the "serde" feature and derive those on your #[state] enum. For example:
[]
= { = "x.y.z", = ["serde"] }
= { = "1.0", = ["derive"] }
Then, in your code:
use state;
If you enable Statum’s "serde" feature, any #[derive(Serialize)] and #[derive(Deserialize)] you put on the enum will get passed through to the expanded variant structs. If you do not enable that feature, deriving those traits will likely fail to compile.
3. Complex Transitions & Data-Bearing States
States can hold data. For example:
// ...
// ...
Accessing State Data
Use .get_state_data() or .get_state_data_mut() to interact with the state-specific data:
4. Attribute Ordering
#[state]must go on an enum.#[machine]must go on a struct.- Because
#[machine]injects extra fields, you need it above any user#[derive(...)]. If you place#[derive(...) ]first, you might see “missing fieldsmarkerandstate_datain initializer” errors.
5. Database Integration
statum provides a seamless way to convert database entries into state machines using a declarative macro-based approach. By combining #[model], #[state], and #[machine] attributes, it simplifies state validation and transition logic for Rust applications.
- Declarative syntax for linking database models to state machines.
- Automated generation of try_to_* methods for state validation and transition.
- Flexible validation logic using user-defined is_* methods.
- Type-safe and ergonomic state machine transitions.
- Clear error handling via StatumError.
Defining Your State Machine
Define your state machine states using the #[state] and #[machine] macros:
Linking Database Models with #[model]
Use the #[model] attribute to link your database representation to the state machine:
Writing Validators
Define custom is_* methods for each state variant to validate transitions:
These methods provide fine-grained control over the conditions that must be met for each state.
Generated try_to_* Methods
The #[model] macro automatically generates try_to_* methods for each state variant:
These methods are:
- Type-safe: Ensure only valid transitions are allowed.
- Easy to use: Automatically wired to the custom validators we made earlier.
Example workflow
- Start with a DbData object representing a database entry.
- Use try_to_* methods to validate and transition to the appropriate state.
- Call state-specific methods (start, process, etc.) on the machine.
Error Handling
All try_to_* methods return a Result with:
- Ok: A valid TaskMachine instance in the desired state.
- Err(StatumError): An error indicating invalid state transitions.
Common Errors and Tips
-
missing fields marker and state_data- Usually means your derive macros (e.g.,
CloneorDebug) expanded before Statum could inject those fields. Move#[machine]above your derives, or remove them.
- Usually means your derive macros (e.g.,
-
cannot find type X in this scope- Ensure that you define your
#[machine]struct before you reference it inimplblocks or function calls.
- Ensure that you define your
-
Feature gating
- If you’re using
#[derive(Serialize, Deserialize)]on a#[state]enum but didn’t enable theserdefeature in Statum, you’ll get compile errors about missing trait bounds.
- If you’re using
Lint Warnings (unexpected_cfgs)
If you see warnings like:
= note: no expected values for `feature`
= help: consider adding `foo` as a feature in `Cargo.toml`
it means you have the unexpected_cfgs lint enabled but you haven’t told your crate “feature = foo” is valid. This is a Rust nightly lint that ensures you only use #[cfg(feature="...")] with known feature values.
To fix it, either disable the lint or declare the allowed values in your crate’s Cargo.toml:
[]
= [
'cfg(feature, values("serde"))'
]
= "warn"
License
Statum is distributed under the terms of the MIT license. See LICENSE for details.