Crates.io | former |
lib.rs | former |
version | |
source | src |
created_at | 2021-11-23 16:04:07.369546+00 |
updated_at | 2025-04-22 21:27:01.809188+00 |
description | A flexible implementation of the Builder pattern supporting nested builders and collection-specific subformers. Simplify the construction of complex objects. |
homepage | https://github.com/Wandalen/wTools/tree/master/module/core/former |
repository | https://github.com/Wandalen/wTools/tree/master/module/core/former |
max_upload_size | |
id | 486314 |
Cargo.toml error: | TOML parse error at line 21, column 1 | 21 | 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` |
size | 0 |
A flexible implementation of the Builder pattern supporting nested builders and collection-specific subformers.
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, or nested structures, making your initialization code more readable and maintainable.
Former
?Compared to manually implementing the Builder pattern or using other builder crates, former
offers several advantages:
#[ derive( Former ) ]
automatically generates the builder struct, storage, and setters, saving you significant repetitive coding effort..field_name( value )
).Default
implementation if not set. Option< T >
fields are handled seamlessly – you only set them if you have a Some( value )
. Custom defaults can be specified easily with #[ former( default = ... ) ]
.former
truly shines with its subformer system. Easily build Vec
, 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.Add former
to your Cargo.toml
:
cargo add former
The default features enable the Former
derive macro and support for standard collections, covering most common use cases.
Derive Former
on your struct and use the generated ::former()
method to start building:
# #[ cfg( any( not( feature = "derive_former" ), not( feature = "enabled" ) ) ) ]
# fn main() {}
# #[ cfg( all( feature = "derive_former", feature = "enabled" ) ) ]
# fn main()
# {
use former::Former;
#[ derive( Debug, PartialEq, Former ) ]
pub struct UserProfile
{
age : i32, // Required field
username : String, // Required field
bio : Option< String >, // Optional field
}
let profile = UserProfile::former()
.age( 30 )
.username( "JohnDoe".to_string() )
// .bio is optional, so we don't *have* to call its setter
.form();
let expected = UserProfile
{
age : 30,
username : "JohnDoe".to_string(),
bio : None, // Defaults to None if not set
};
assert_eq!( profile, expected );
dbg!( &profile );
// > &profile = UserProfile {
// > age: 30,
// > username: "JohnDoe",
// > bio: None,
// > }
// Example setting the optional field:
let profile_with_bio = UserProfile::former()
.age( 30 )
.username( "JohnDoe".to_string() )
.bio( "Software Developer".to_string() ) // Set the optional bio
.form();
let expected_with_bio = UserProfile
{
age : 30,
username : "JohnDoe".to_string(),
bio : Some( "Software Developer".to_string() ),
};
assert_eq!( profile_with_bio, expected_with_bio );
dbg!( &profile_with_bio );
// > &profile_with_bio = UserProfile {
// > age: 30,
// > username: "JohnDoe",
// > bio: Some( "Software Developer" ),
// > }
# }
Run this example locally | Try it online
Former
makes working with optional fields and default values straightforward:
Option< T >
Fields: As seen in the basic example, fields of type Option< T >
automatically default to None
. You only need to call the setter if you have a Some( 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:
# #[ cfg( any( not( feature = "derive_former" ), not( feature = "enabled" ) ) ) ]
# fn main() {}
# #[ cfg( all( feature = "derive_former", feature = "enabled" ) ) ]
# fn main()
# {
use former::Former;
#[ derive( Debug, PartialEq, Former ) ]
pub struct Config
{
#[ former( default = 1024 ) ] // Use 1024 if .buffer_size() is not called
buffer_size : i32,
timeout : Option< i32 >, // Defaults to None
#[ former( default = true ) ] // Default for bool
enabled : bool,
}
// Only set the optional timeout
let config1 = Config::former()
.timeout( 5000 )
.form();
assert_eq!( config1.buffer_size, 1024 ); // Got default
assert_eq!( config1.timeout, Some( 5000 ) );
assert_eq!( config1.enabled, true ); // Got default
// Set everything, overriding defaults
let config2 = Config::former()
.buffer_size( 4096 )
.timeout( 1000 )
.enabled( false )
.form();
assert_eq!( config2.buffer_size, 4096 );
assert_eq!( config2.timeout, Some( 1000 ) );
assert_eq!( config2.enabled, false );
# }
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
# #[ cfg( not( all( feature = "enabled", feature = "derive_former", any( feature = "use_alloc", not( feature = "no_std" ) ) ) ) ) ]
# fn main() {}
# #[ cfg( all( feature = "enabled", feature = "derive_former", any( feature = "use_alloc", not( feature = "no_std" ) ) ) ) ]
# fn main()
# {
use former::Former;
#[ derive( Debug, PartialEq, Former ) ]
pub struct Report
{
title : String,
#[ subform_collection ] // Enables the `.entries()` subformer
entries : Vec< String >,
}
let report = Report::former()
.title( "Log Report".to_string() )
.entries() // Get the subformer for the Vec
.add( "Entry 1".to_string() ) // Use subformer methods to modify the Vec
.add( "Entry 2".to_string() )
.end() // Return control to the parent former (ReportFormer)
.form(); // Finalize the Report
assert_eq!( report.title, "Log Report" );
assert_eq!( report.entries, vec![ "Entry 1".to_string(), "Entry 2".to_string() ] );
dbg!( &report );
// > &report = Report {
// > title: "Log Report",
// > entries: [
// > "Entry 1",
// > "Entry 2",
// > ],
// > }
# }
See Vec example | See HashMap example
former
provides different subform attributes (#[ subform_collection ]
, #[ subform_entry ]
, #[ subform_scalar ]
) for various collection and nesting patterns.
#[ derive( Former ) ]
for structs and enums.Default
values and Option< T >
fields. Custom defaults via #[ former( default = ... ) ]
.#[ subform_scalar ]
: For fields whose type also derives Former
.#[ subform_collection ]
: For collections like Vec
, HashMap
, HashSet
, etc., providing methods like .add()
or .insert()
.#[ subform_entry ]
: For collections where each entry is built individually using its own former.#[ scalar( name = ... ) ]
, #[ subform_... ( name = ... ) ]
.#[ scalar( setter = false ) ]
, #[ subform_... ( setter = false ) ]
.impl Former
.#[ subform_collection( definition = ... ) ]
.#[ storage_fields( ... ) ]
.#[ mutator( custom ) ]
+ impl FormerMutator
.FormingEnd
.Collection
traits.Assign
, ComponentFrom
, ComponentsAssign
, FromComponents
) for type-based field access and conversion (See former_types
documentation).