statum

Crates.iostatum
lib.rsstatum
version
sourcesrc
created_at2025-01-07 21:17:24.021271
updated_at2025-01-08 22:00:25.108487
descriptionCompile-time state machine magic for Rust: Zero-boilerplate typestate patterns with automatic transition validation
homepage
repositoryhttps://github.com/eboody/statum
max_upload_size
id1507706
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Eran Boodnero (eboody)

documentation

https://docs.rs/statum

README

statum

A zero-boilerplate library for finite-state machines in Rust, with compile-time state transition validation.

Overview

The typestate pattern lets you encode state machines at the type level, making invalid state transitions impossible at compile time. This crate makes implementing typestates effortless through two attributes:

  • #[state] - Define your states
  • #[machine] - Create your state machine

Installation

Run the following Cargo command in your project directory:

cargo add statum

Quick Start

Here's a simple example of a task processor:

use statum::{state, machine};

#[state]
pub enum TaskState {
    New,
    InProgress,
    Complete,
}

#[machine]
pub struct Task<S: TaskState> {
    id: String,
    name: String,
}

impl Task<New> {
    fn start(self) -> Task<InProgress> {
        // Use transition() for simple state transitions
        self.transition()
    }
}

impl Task<InProgress> {
    fn complete(self) -> Task<Complete> {
        self.transition()
    }
}

fn main() {
    let task = Task::new(
        "task-1".to_owned(),
        "Important Task".to_owned(),
    );
    
    let task = task.start();
    let task = task.complete();
}

Features

  • debug (enabled by default) - Implements Debug for state machines and states
  • serde - Adds serialization support via serde

Enable features in your Cargo.toml:

[dependencies]
statum = { version = "...", features = ["serde"] }

Advanced Features

States with Data

States can carry state-specific data:

#[state]
pub enum DocumentState {
    Draft,                      // Simple state
    Review(ReviewData),         // State with data
    Published,
}

pub struct ReviewData {
    reviewer: String,
    comments: Vec<String>,
}

#[machine]
pub struct Document<S: DocumentState> {
    id: String,
    content: String,
}

impl Document<Draft> {
    fn submit_for_review(self, reviewer: String) -> Document<Review> {
        // Use transition_with() for states with data
        self.transition_with(ReviewData {
            reviewer,
            comments: vec![],
        })
    }
}

Accessing State Data

When a state has associated data, you can access it safely:

impl Document<Review> {
    fn add_comment(&mut self, comment: String) {
        // Safely modify state data
        if let Some(review_data) = self.get_state_data_mut() {
            review_data.comments.push(comment);
        }
    }

    fn get_reviewer(&self) -> Option<&str> {
        // Safely read state data
        self.get_state_data().map(|data| data.reviewer.as_str())
    }

    fn approve(self) -> Document<Published> {
        // Transition to a state without data
        self.transition()
    }
}

Database Integration

Here's how to integrate with external data sources:

#[derive(Debug)]
pub enum Error {
    InvalidState,
}

#[derive(Clone)]
pub struct DbRecord {
    id: String,
    state: String,
}

// Convert from database record to state machine
impl TryFrom<&DbRecord> for Document<Draft> {
    type Error = Error;
    
    fn try_from(record: &DbRecord) -> Result<Self, Error> {
        if record.state != "draft" {
            return Err(Error::InvalidState);
        }
        Ok(Document::new(
            record.id.clone(),
            String::new(),
        ))
    }
}

// Or use methods for more complex conversions with data
impl DbRecord {
    fn try_to_review(&self, reviewer: String) -> Result<Document<Review>, Error> {
        if self.state != "review" {
            return Err(Error::InvalidState);
        }
        
        let doc = Document::new(
            self.id.clone(),
            String::new(),
        );
        
        Ok(doc.transition_with(ReviewData {
            reviewer,
            comments: vec![],
        }))
    }
}

Rich Context

Your state machine can maintain any context it needs:

#[machine]
pub struct DocumentProcessor<S: DocumentState> {
    id: Uuid,
    created_at: DateTime<Utc>,
    metadata: HashMap<String, String>,
    config: Config,
}

Contributing

Contributions welcome! Feel free to submit pull requests.

License

MIT License - see LICENSE for details.

Commit count: 23

cargo fmt