better-builder

Crates.iobetter-builder
lib.rsbetter-builder
version0.1.0
sourcesrc
created_at2024-07-29 21:53:23.224345
updated_at2024-07-29 21:53:23.224345
descriptionA library to derive an improved builder pattern for structs, leveraging Rust's type system.
homepage
repositoryhttps://github.com/JosiahBull/better-builder
max_upload_size
id1319241
size25,729
Josiah Bull (JosiahBull)

documentation

README

BetterBuilder

This rust crate aims to create better type-safe builders. Builders must fully provide all required and any optional fields before the end type can be instantiated.

tldr; No more .build()?, just .build() - and leverage the Rust type system to your advantage!

Installation

Cargo Add Run the command:

cargo add better_builder

Cargol Toml

better_builder = "0.1"

Example Usage

use better_builder::BetterBuilder;

#[derive(Debug, BetterBuilder)]
struct Cart {
    owner: String,
    num_wheels: u8,
    // Because this is an Option<T>, we assume it's not required to construct the object.
    num_seats: Option<u8>,
    inventory: Vec<String>,
}

fn main() {
    let t = Cart::builder()
        // All required properties, in order.
        .owner("Alice".to_string())
        .num_wheels(4)
        .inventory(vec!["apple".to_string(), "banana".to_string()])
        // Any optional properties.
        .num_seats(Some(2))
        // Finish building and get the final object.
        .build();

    assert_eq!(t.owner, "Alice".to_string());
    assert_eq!(t.num_wheels, 4);
    assert_eq!(t.num_seats, Some(2));
    assert_eq!(t.inventory, vec!["apple".to_string(), "banana".to_string()]);
}

Licensing and Contributions

This project is licensed under the MIT License and ApacheV2 at your option.. You can find the full text of these licenses in the LICENSE-MIT and LICENSE-APACHE files included in the repository.

Contributions to this project are welcomed and will also be licensed under the same terms. By submitting a pull request or contributing in any other way, you agree to license your contributions under the MIT License and Apache.

We value and appreciate all contributions to make this project better!

Commit count: 0

cargo fmt