Module :: former
A flexible implementation of the Builder pattern supporting nested builders, collection-specific subformers, and comprehensive enum variant constructors.
What is Former?
The former crate provides a powerful derive macro, #[ derive( Former ) ], that automatically implements the Builder pattern for your Rust structs and enums.
Its primary goal is to simplify the construction of complex objects, especially those with numerous fields, optional values, default settings, collections, nested structures, or complex enum variants, making your initialization code more readable and maintainable.
For enums, former automatically generates constructors for each variant, intelligently choosing between direct constructors, subformers, and standalone functions based on the variant structure and applied attributes.
Why Use Former?
Compared to manually implementing the Builder pattern or using other builder crates, former offers several advantages:
- Reduced Boilerplate:
#[ derive( Former ) ]automatically generates the builder struct, storage, and setters, saving you significant repetitive coding effort. - Fluent & Readable API: Construct objects step-by-step using clear, chainable methods (
.field_name( value )). - Intelligent Enum Support: Automatically generates appropriate constructors for enum variants:
- Unit variants get direct constructors (e.g.,
Status::active()) - Simple variants get scalar constructors (e.g.,
Message::text("hello")) - Complex variants get subformers for step-by-step construction
- Flexible attributes (
#[scalar],#[subform_scalar],#[standalone_constructors]) for fine-grained control
- Unit variants get direct constructors (e.g.,
- Effortless Defaults & Optionals: Fields automatically use their
Defaultimplementation if not set.Option< T >fields are handled seamlessly – you only set them if you have aSome( value ). Custom defaults can be specified easily with#[ former( default = ... ) ]. - Powerful Collection & Nested Struct Handling:
formertruly shines with its subformer system. Easily buildVec,HashMap,HashSet, and other collections element-by-element, or configure nested structs using their own dedicated formers within the parent's builder chain. This is often more complex to achieve with other solutions.
Installation
Add former to your Cargo.toml:
The default features enable the Former derive macro and support for standard collections, covering most common use cases.
Basic Usage
Derive Former on your struct and use the generated ::former() method to start building:
#
#
#
# #
Run this example locally | Try it online
Handling Optionals and Defaults
Former makes working with optional fields and default values straightforward:
-
Option< T >Fields: As seen in the basic example, fields of typeOption< T >automatically default toNone. You only need to call the setter if you have aSome( value ). -
Custom Defaults: For required fields that don't implement
Default, or when you need a specific default value other than the type's default, use the#[ former( default = ... ) ]attribute:
#
#
#
# #
Building Collections & Nested Structs (Subformers)
Where former significantly simplifies complex scenarios is in building collections (Vec, HashMap, etc.) or nested structs. It achieves this through subformers. Instead of setting the entire collection/struct at once, you get a dedicated builder for the field:
Example: Building a Vec
#
#
#
# #
See Vec example | See HashMap example
former provides different subform attributes (#[ subform_collection ], #[ subform_entry ], #[ subform_scalar ]) for various collection and nesting patterns.
Standalone Constructors
For scenarios where you want a direct constructor function instead of always starting with YourType::former(), former offers standalone constructors.
- Enable: Add
#[ standalone_constructors ]to your struct or enum definition. - Function Name: A function named after your type (in
snake_case) will be generated (e.g.,my_struct()forstruct MyStruct). For enums, functions are named after variants (e.g.,my_variant()forenum E { MyVariant }). - Arguments: By default, the constructor takes no arguments and returns the
Formertype. - Specify Arguments: Mark specific fields with
#[ arg_for_constructor ]to make them required arguments for the standalone constructor. - Return Type (Option 2 Logic):
- If all fields of the struct/variant are marked with
#[ arg_for_constructor ], the standalone constructor returns the instance directly (Self). - If zero or some fields are marked, the standalone constructor returns the
Formertype, pre-initialized with the provided arguments.
- If all fields of the struct/variant are marked with
Example: Struct Standalone Constructors
#
#
#
# #
Example: Enum Standalone Constructors
Vocabulary & Terminology
Understanding the terminology used in former will help you leverage its full potential, especially when working with enums and variants:
Core Concepts
Former: A builder object that accumulates field values and produces the final instance via.form().Storage: Internal structure that holds the building state, containing options for each field.Subformer: A specialized former for building nested structures, collections, or complex field types.FormingEnd: A mechanism that controls what happens when.form()is called on a (sub)former.
Variant Types (for Enums)
- Unit Variant: An enum variant with no associated data (e.g.,
Status::Active). - Tuple Variant: An enum variant with unnamed fields in parentheses (e.g.,
Message::Error(String),Point::Coords(i32, i32)). - Struct Variant: An enum variant with named fields in braces (e.g.,
Request::Get { url: String, headers: Vec<String> }).
Variant Field Categories
- Zero-Field Variant: A variant with no fields - can be unit (
Status::Active) or empty tuple (Status::Active()). - Single-Field Variant: A variant with exactly one field (e.g.,
Message::Text(String)orUser::Profile { name: String }). - Multi-Field Variant: A variant with multiple fields (e.g.,
Point::Coords(i32, i32)orRequest::Post { url: String, body: String }).
Constructor Types
- Scalar Constructor: A method that takes direct values and immediately returns the enum instance (e.g.,
Message::text("hello")→Message::Text("hello")). - Subform Constructor: A method that returns a former/builder for constructing the variant step-by-step, useful for complex variants.
- Direct Constructor: Simple constructor for variants with no fields (e.g.,
Status::active()→Status::Active).
Enum Constructor Patterns
- Method-style Constructor: Instance methods on the enum type (e.g.,
MyEnum::variant_name(...)). - Standalone Constructor: Top-level functions generated when
#[standalone_constructors]is used (e.g.,variant_name(...)).
Variant Attributes
#[scalar]: Forces generation of a scalar constructor that takes field values directly and returns the enum instance.#[subform_scalar]: For single-field variants where the field type implementsFormer- generates a method returning the field's former.#[standalone_constructors]: Applied to the enum itself, generates top-level constructor functions for each variant.#[arg_for_constructor]: Applied to individual fields, includes them as parameters in standalone constructors.
Advanced Concepts
- Implicit Variant Former: An automatically generated former for variants with multiple fields, providing individual field setters.
- End-of-forming Logic: Custom behavior when a former completes, enabling advanced patterns like validation or transformation.
- Context Propagation: Mechanism for passing data through nested formers in complex builder hierarchies.
Key Features Overview
- Automatic Builder Generation:
#[ derive( Former ) ]for structs and enums. - Fluent API: Chainable setter methods for a clean construction flow.
- Comprehensive Enum Support: Full support for all enum variant types:
- Unit variants: Direct constructors (e.g.,
MyEnum::variant()) - Tuple variants: Scalar constructors or subformers based on field count and attributes
- Struct variants: Subformers with individual field setters or scalar constructors
- Zero, single, and multi-field variants with different behavioral patterns
- Unit variants: Direct constructors (e.g.,
- Flexible Constructor Generation:
- Method-style constructors:
MyEnum::variant_name(...)on the enum type - Standalone constructors: Top-level functions when
#[standalone_constructors]is used - Scalar constructors: Direct value-to-instance conversion with
#[scalar] - Subform constructors: Builder pattern for complex variants
- Method-style constructors:
- Defaults & Optionals: Seamless handling of
Defaultvalues andOption< T >fields. Custom defaults via#[ former( default = ... ) ]. - Subformers: Powerful mechanism for building nested structures and collections:
#[ subform_scalar ]: For fields whose type also derivesFormer, or for single-field enum variants#[ subform_collection ]: For collections likeVec,HashMap,HashSet, etc., providing methods like.add()or.insert()#[ subform_entry ]: For collections where each entry is built individually using its own former
- Variant-Specific Attributes:
#[ scalar ]: Forces scalar constructor generation for enum variants#[ subform_scalar ]: Enables subformer delegation for compatible variants#[ standalone_constructors ]: Generates top-level constructor functions#[ arg_for_constructor ]: Controls parameter inclusion in standalone constructors
- Customization:
- Rename setters:
#[ scalar( name = ... ) ],#[ subform_... ( name = ... ) ] - Disable default setters:
#[ scalar( setter = false ) ],#[ subform_... ( setter = false ) ] - Define custom setters directly in
impl Former - Specify collection definitions:
#[ subform_collection( definition = ... ) ]
- Rename setters:
- Advanced Control:
- Storage-only fields:
#[ storage_fields( ... ) ]. - Custom mutation logic:
#[ mutator( custom ) ]+impl FormerMutator. - Custom end-of-forming logic: Implement
FormingEnd. - Custom collection support: Implement
Collectiontraits.
- Storage-only fields:
Where to Go Next
- Advanced Usage & Concepts: Dive deeper into subformers, customization options, storage, context, definitions, mutators, and custom collections.
- Examples Directory: Explore practical, runnable examples showcasing various features.
- API Documentation (docs.rs): Get detailed information on all public types, traits, and functions.
- Repository (GitHub): View the source code, contribute, or report issues.