build-it

Crates.iobuild-it
lib.rsbuild-it
version0.1.5
sourcesrc
created_at2024-08-25 16:01:27.18921
updated_at2024-09-06 18:02:24.509334
descriptionA derive-macro for simple builder-pattern generation
homepage
repositoryhttps://github.com/benpueschel/build-it
max_upload_size
id1351200
size18,813
Ben PĆ¼schel (benpueschel)

documentation

README

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");
Commit count: 0

cargo fmt