macro struct-builder

Derive a builder for your structs

5 unstable releases

0.3.0 Sep 24, 2024
0.2.1 Sep 24, 2024
0.2.0 Sep 20, 2024
0.1.1 Sep 17, 2024
0.1.0 Sep 17, 2024

#675 in Rust patterns

Download history 433/week @ 2024-09-16 259/week @ 2024-09-23 68/week @ 2024-09-30 10/week @ 2024-10-07

770 downloads per month

MIT license

56KB
1.5K SLoC

Derive builders for your structs.

Put #[builder] on your structs to derive a builder pattern for that struct. The builder can be used to create the struct from only required fields (those without the Option type) and modify the content of the struct.

A struct builder enforces required fields to be specified and allows optional arguments to be specified post-construction. This is done by defining a "params" struct that the builder depends on to be initialized. This struct defines all the fields in the original struct that don't have the "Option" type. Once the builder is initialized with the params, both required and optional fields can be updated by calling builder methods (using the identifiers with_<field>).

Examples

Using builder to build a request with named fields.

use struct_builder::builder;

#[builder]
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct CreateUserRequest<P> {
    pub email: String,
    pub first_name: Option<String>,
    pub last_name: Option<String>,
    pub age: Option<u64>,
    pub payload: P
}

fn main() {
    // Inherits attributes and generics from `CreateUserRequest`
    let params: CreateUserRequestParams<String> = CreateUserRequestParams {
        email: "john.doe@email.com".to_owned(),
        payload: "John Doe's User".to_owned()
    };

    let request = CreateUserRequest::builder(params)
        .with_first_name(Some("John".to_owned()))
        .with_age(Some(35))
        .build();
    
    assert_eq!(request.email, "john.doe@email.com".to_owned());
    assert_eq!(request.first_name, Some("John".to_owned()));
    assert_eq!(request.last_name, None);
    assert_eq!(request.age, Some(35));
}

Dependencies

~245–690KB
~16K SLoC