6 releases
0.1.5 | Sep 6, 2024 |
---|---|
0.1.4 | Sep 6, 2024 |
0.1.1 | Aug 25, 2024 |
#2133 in Development tools
25 downloads per month
Used in 2 crates
(via gitea-sdk)
15KB
168 lines
simple-builder
Easily generate builder patterns in Rust.
Usage
use build_it::Builder;
#[derive(Default, Builder)]
struct MyAwesomeStruct {
name: Option<String>,
pub age: Option<u32>,
#[build_it(skip)]
address: String,
#[build_it(skip)]
pub phone: Option<String>,
}
let builder = MyAwesomeStruct::default()
.name("Alice".to_string())
.age(42);
// Note that `address` and `phone` do not have builder
// methods because of the #[build_it(skip)] attribute.
assert_eq!(builder.name, Some("Alice".to_string()));
assert_eq!(builder.age, Some(42));
// These fields are skipped, so they're value will still be the default value.
assert_eq!(builder.address, String::default());
assert_eq!(builder.phone, None);
The #[build_it(rename = "new_name")]
attribute can be used to rename the builder
method. In this case, the builder method will be called new_name
instead of renamed
:
struct MyAwesomeStruct {
#[build_it(rename = "new_name")]
renamed: Option<String>,
}
let builder = MyAwesomeStruct::default()
.new_name("Alice".to_string());
The #[build_it(into)]
attribute can be used to allow the builder method to accept
types that can be converted into the field type. In this case, the builder method will
accept a &str
instead of a String
:
struct MyAwesomeStruct {
#[build_it(into)]
name_into: Option<String>,
}
let builder = MyAwesomeStruct::default()
.name_into("Alice");
The #[build_it(into)]
attribute can also be used on the struct itself to allow the
builder to accept Into
implementations for all fields:
#[derive(Builder)]
#[build_it(into)]
struct MyAwesomeStruct {
name: Option<String>,
language: Option<String>,
}
let builder = MyAwesomeStruct::default()
.name("Alice")
.language("Rust");
Dependencies
~220–660KB
~16K SLoC