A derivable macro for declaring a builder pattern. This crate is highly inspired by derive_builder.
```rust use builder_pattern::Builder;
struct Person { #[into] name: String, age: i32, #[default(Gender::Nonbinary)] gender: Gender, }
let p1 = Person::new() // PersonBuilder<(), (), ()>
.name(String::from("Joe")) // PersonBuilder // Orders does not matter.
let p2 = Person::new() // PersonBuilder<(), (), ()>
.age(32) // PersonBuilder<(), i32, ()>
// // Add ```toml [dependencies]
builder-pattern = "0.3"
``` A field having this attribute will be considered as optional and the If this attribute is present, the builder function would not be generated for the field. This field requires Example: ```rust struct Test {
#[default(Uuid::new_v4())]
#[hidden]
id: Uuid,
name: String,
} let test1 = Test::new() // TestBuilder<(), ()>
.name(String::from("Joe")) // TestBuilder let test2 = Test::new() // TestBuilder<(), ()>
.name(String::from("Jack")) // TestBuilder A setter function for a field having this attribute will accept an Example: ```rust struct Test {
#[into]
pub name: String,
} let test = Test::new() // TestBuilder<()>
// Implement a validator for a field. ```rust struct Test {
#[validator(isnotempty)]
#[into]
pub name: String,
} fn isnotempty(name: String) -> Result let test1 = Test::new() // TestBuilder<()>
.name("Hello") // Ok(TestBuilder let test2 = Test::new() // TestBuilder<()>
.name("") // Err(String{ "Name cannot be empty." })
.unwrap() // panic!
.build();
``` This crate generates documentations for the builder functions. If you documentate the fields,
the builder functions for them also copy the documentations. Example code: ```rust struct Test {
/// A positive integer.
pub positive: i32, }
``` Generated code: `` /// A builder for impl TestBuilder impl impl The following code ```rust struct Person {
#[into]
#[validator(isnotempty)]
name: String,
age: i32,
#[default(Gender::Nonbinary)]
gender: Gender,
} ``` will generates: ```rust
struct PersonBuilder impl Person {
// Create an empty builder
fn new() -> PersonBuilder<(), (), ()> {
PersonBuilder {
name: None,
age: None,
// Default value
gender: Some(Gender::Nonbinary),
_phantom: PhantomData
}
}
} // Builder for // Builder for // Builder for // &str
is implicitly converted into String
// because of into
attribute!
.name("Jack") // PersonBuildername
field required - Compilation error.
let p3 = Person::new() // PersonBuilder<(), (), ()>
.age(15) // PersonBuilder<(), i32, ()>
.build();
```Get Started
builder-pattern
to Cargo.toml
.Cargo.toml
Features
build
function is allowed only the all of required fields are provided. No Result, No Unwrap. Just use it.Attributes
#[default(expr)]
expr
will be evaluated as a default value of the field. build
function can be called without providing this field.#[hidden]
default
attribute.[derive(Builder)]
#[into]
Into
trait as a parameter. You can use this setter with implicit conversion.[derive(Builder)]
&str
is implicitly converted into String
.
.name("Hello") // TestBuilder#[validator(expr)]
expr
could be a validating function that takes the field's type and returns Result
.[derive(Builder)]
Auto-Generated Documentions
Example
[derive(Builder)]
/// A integer having zero as a default value.
#[default(0)]
pub zero: i32,
rust
impl Test {
/// Creating a builder.
/// ## Required fields
/// ###
positive
/// - Type:
i32
///
/// A positive integer.
///
/// ## Optional fields
/// ###
zero
/// - Type:
i32
/// - Default:
0`
///
/// A integer having zero as a default value.
fn new() -> TestBuilder<(), ()> {
TestBuilder {
_phatom: PhantomData,
positive: None,
zero: Some(0),
}
}
}Test
.
struct TestBuilderi32
///
/// A positive integer.
pub fn positive(self, value: i32) -> TestBuilder0
///
/// A integer having zero as a default value.
pub fn zero(self, value: i32) -> TestBuilderHow it works
[derive(Builder)]
name
.
implInto
traits.
fn nameIntoType
into String
.
name: Some(value.into()),
age: self.age,
gender: self.gender,
phantom: PhantomData,
}),
Err() => Err(())
}
}
}age
.
implgender
.
implbuild
function
// It can be called regardless of whether T3
is ()
or Gender
.
implLicense