Skip to main content

stave/
lib.rs

1//! Compile-time typestate validation for type-safe builder patterns in Rust.
2//!
3//! `stave` provides an architectural solution to uninitialized configuration
4//! bugs by leveraging the Rust type system to shift verification errors from
5//! runtime to compile time. Instead of relying on runtime checks or panicking
6//! when a required parameter is omitted, `stave` tracks the initialization
7//! state of a struct at the type level, preventing dependent methods from
8//! compiling until the structural requirements are fully satisfied.
9//!
10//! # Core Mechanics
11//!
12//! The framework operates via two complementary procedural attribute macros:
13//!
14//! * [`builder`]: Applied to a struct definition. It analyzes field metadata,
15//!   wraps optional fields in an `Option<T>`, and synthesizes internal marker types
16//!   (e.g., `__FieldUnset` and `__FieldSet`) for required fields. It adds corresponding
17//!   generic parameters to the struct to securely track whether each required field
18//!   contains a value.
19//! * [`methods`]: Applied to an `impl` block for that struct. It synthesizes
20//!   boilerplate public setters (`set_{field_name}`) for unannotated fields, generates
21//!   type-restricted getters, and processes state transformations via two sub-attributes:
22//!   `#[sets(...)]` and `#[requires(...)].`
23//!
24//! # Macro Evaluation Ordering
25//!
26//! Because procedural macro attribute invocations do not naturally share state or
27//! token streams during compilation, `stave` bridges this boundary using an in-process
28//! compile-time registry. The `#[builder]` macro evaluates the layout schema and records
29//! the structural configuration to an internal global cache. The `#[methods]` macro
30//! subsequently queries this metadata registry by struct identifier to safely synthesize
31//! state-transition code.
32//!
33//! Due to this architecture, **the `#[builder]` attribute must always appear in the
34//! source stream prior to its corresponding `#[methods]` block**. Standard source layouts
35//! where the struct definition precedes its implementation block satisfy this rule.
36//! Split implementations across multiple `#[methods]` blocks or distinct modules are
37//! currently unsupported.
38//!
39//! # Quick Start Example
40//!
41//! ```rust
42//! use stave::{builder, methods};
43//!
44//! #[builder]
45//! struct Configurator {
46//!     #[stave(required)]
47//!     identity: String,
48//!     #[stave(required)]
49//!     target_port: u16,
50//!     timeout_ms: u64, // Defaults to optional, wrapped in Option<u64>
51//! }
52//!
53//! #[methods]
54//! impl Configurator {
55//!     // Custom setter specifying flexible input transformations
56//!     #[sets(identity)]
57//!     fn set_identity(self, val: impl Into<String>) -> String {
58//!         val.into()
59//!     }
60//!
61//!     // Arbitrary user-defined method restricted by compile-time typestates
62//!     #[requires(identity, target_port)]
63//!     fn establish_connection(self) {
64//!         // Inside here, self.identity() and self.target_port() safely yield references
65//!         println!("Connecting to {} on port {}", self.identity(), self.target_port());
66//!     }
67//! }
68//!
69//! fn main() {
70//!     // Compiles flawlessly:
71//!     Configurator::new()
72//!         .set_identity("node_alpha")
73//!         .set_target_port(9000) // Boilerplate generated automatically
74//!         .establish_connection();
75//!
76//!     // Will NOT compile (triggers E0599: method `establish_connection` not found):
77//!     // Configurator::new().set_identity("node_beta").establish_connection();
78//! }
79//! ```
80
81pub use stave_macros::{builder, methods};