fluxo_typestate/lib.rs
1// Copyright (c) 2026 Fluxo Labs
2// AI-generated code based on idea by alisio85
3// SPDX-License-Identifier: MIT
4
5//! # Fluxo Typestate
6//!
7//! Fluxo Typestate is a Rust library that provides zero-cost type-state pattern
8//! implementations via procedural macros. It automatically generates type-safe state
9//! machines from simple enum definitions, ensuring compile-time guarantees of valid
10//! state transitions.
11//!
12//! ## Overview
13//!
14//! The type-state pattern is a powerful technique in Rust that uses the type system
15//! to encode state information. By representing each state as a different type,
16//! the compiler can enforce that invalid state transitions are caught at compile-time,
17//! rather than causing runtime errors.
18//!
19//! Fluxo Typestate automates this pattern by allowing you to define your state machine
20//! as a simple enum, and then automatically generating all the necessary structs,
21//! traits, and transition methods.
22//!
23//! ## Features
24//!
25//! - **Zero-Cost Abstractions**: All state checking happens at compile-time. The
26//! generated code has no runtime overhead compared to hand-written implementations.
27//!
28//! - **Compile-Time Graph Validation**: Invalid transitions produce clear compile
29//! errors that show you exactly which transitions are valid from the current state.
30//!
31//! - **Automatic Event Logging**: When the `logging` feature is enabled, every state
32//! transition can automatically log its occurrence using the `tracing` crate.
33//!
34//! - **Mermaid Visualization**: Generate Mermaid.js state diagrams to visualize
35//! your state machine structure in documentation or IDEs.
36//!
37//! ## Quick Start
38//!
39//! Here's a simple example of how to use Fluxo Typestate:
40//!
41//! ```ignore
42//! use fluxo_typestate::state_machine;
43//!
44//! #[state_machine]
45//! enum Computer {
46//! Idle,
47//! Running { cpu_load: f32 },
48//! Sleeping,
49//! }
50//!
51//! fn main() {
52//! let computer: Computer<Idle> = Computer::new();
53//! let running: Computer<Running> = computer.start();
54//! println!("CPU Load: {}", running._inner_running.cpu_load);
55//! }
56//! ```
57//!
58//! ## Installation
59//!
60//! Add this to your `Cargo.toml`:
61//!
62//! ```toml
63//! [dependencies]
64//! fluxo-typestate = "0.1"
65//! ```
66//!
67//! For logging support, enable the `logging` feature:
68//!
69//! ```toml
70//! [dependencies]
71//! fluxo-typestate = { version = "0.1", features = ["logging"] }
72//! ```
73//!
74//! For serialization support, enable the `serde` feature:
75//!
76//! ```toml
77//! [dependencies]
78//! fluxo-typestate = { version = "0.1", features = ["serde"] }
79//! ```
80//!
81//! ## Defining a State Machine
82//!
83//! To define a state machine, create an enum and annotate it with `#[state_machine]`.
84//! Each variant of the enum represents a state. You can have:
85//!
86//! - **Unit variants**: `Idle` - states with no associated data
87//! - **Tuple variants**: `Running(f32)` - states with unnamed data
88//! - **Named variants**: `Running { cpu_load: f32 }` - states with named data
89//!
90//! ## Defining Transitions
91//!
92//! Transitions are defined using the `#[transition]` attribute on enum variants.
93//! The syntax is:
94//!
95//! ```ignore
96//! #[transition(SourceState -> TargetState: method_name)]
97//! ```
98//!
99//! This generates a method called `method_name` that transitions from the source
100//! state to the target state. The method consumes the current state and returns
101//! the new state, making it impossible to accidentally use the old state after
102//! a transition.
103//!
104//! ## The State Trait
105//!
106//! All generated state structs implement the `State` trait, which provides the
107//! `name()` method to get a string representation of the state type.
108//!
109//! ## The Sealed Trait
110//!
111//! The `Sealed` trait prevents external crates from implementing the `State`
112//! trait, which ensures the type safety guarantees of the pattern.
113//!
114//! ## Example: A Computer State Machine
115//!
116//! ```ignore
117//! use fluxo_typestate::state_machine;
118//!
119//! #[state_machine]
120//! enum Computer {
121//! #[transition(Computer::Idle -> Computer::Running: start)]
122//! #[transition(Computer::Idle -> Computer::Sleeping: sleep)]
123//! Idle,
124//! #[transition(Computer::Running -> Computer::Idle: stop)]
125//! #[transition(Computer::Running -> Computer::Sleeping: suspend)]
126//! Running { cpu_load: f32 },
127//! #[transition(Computer::Sleeping -> Computer::Idle: wake)]
128//! Sleeping,
129//! }
130//!
131//! fn main() {
132//! // Create a new computer in the Idle state
133//! let computer: Computer<Idle> = Computer::new();
134//!
135//! // Start the computer (transition to Running)
136//! let running: Computer<Running> = computer.start();
137//! println!("CPU Load: {}", running._inner_running.cpu_load);
138//!
139//! // Suspend the computer (transition to Sleeping)
140//! let sleeping: Computer<Sleeping> = running.suspend();
141//!
142//! // Wake up the computer (transition back to Idle)
143//! let idle: Computer<Idle> = sleeping.wake();
144//! }
145//! ```
146//!
147//! ## Viewing the State Machine
148//!
149//! You can generate a Mermaid diagram of your state machine:
150//!
151//! ```ignore
152//! println!("{}", Computer::<Idle>::mermaid_diagram());
153//! ```
154//!
155//! ## Performance
156//!
157//! Fluxo Typestate is designed with zero-cost abstractions in mind:
158//!
159//! - All state checking happens at compile-time through Rust's type system
160//! - The `PhantomData<S>` marker has zero size
161//! - Transition methods are statically dispatched with no function pointer overhead
162//!
163//! ## Comparison with Alternatives
164//!
165//! | Feature | Fluxo | Manual Type-State | Other Crates |
166//! |---------|-------|------------------|--------------|
167//! | Auto-generated transitions | ✓ | ✗ | Partial |
168//! | Compile-time validation | ✓ | ✓ | Partial |
169//! | Mermaid visualization | ✓ | ✗ | ✗ |
170//! | Zero-cost | ✓ | ✓ | ✓ |
171//! | Logging integration | ✓ | ✗ | ✗ |
172//!
173//! ## License
174//!
175//! Copyright (c) 2026 Fluxo Labs
176//! AI-generated code based on idea by alisio85
177//! SPDX-License-Identifier: MIT
178//!
179//! For more information, see the [documentation](https://docs.rs/fluxo-typestate).
180
181// Copyright (c) 2026 Fluxo Labs
182// AI-generated code based on idea by alisio85
183// SPDX-License-Identifier: MIT
184
185#![doc(html_root_url = "https://docs.rs/fluxo-typestate/0.1")]
186#![warn(missing_docs)]
187#![warn(rustdoc::missing_crate_level_docs)]
188
189//! The main entry point for the Fluxo Typestate library.
190//!
191//! This module re-exports the core types and traits needed to use Fluxo Typestate.
192//! The main component is the `state_machine` procedural macro which transforms
193//! enum definitions into complete type-state implementations.
194
195pub mod error;
196pub mod state;
197pub mod transition;
198
199pub use error::FluxoError;
200pub use state::{Sealed, State};
201pub use transition::{StateMachine, StateMachineExt};
202
203#[cfg(feature = "logging")]
204pub use tracing;
205
206pub use fluxo_typestate_macros::state_machine;