Build Status Coverage Status Latest Version

Builder pattern derive

Rust macro (based on custom_derive) to automatically implement the builder pattern for arbitrary structs. A simple #[derive(Builder)] will generate code of public setter-methods for all struct fields.

This is a work in progress. Use it at your own risk.

And this is how it works:

```rust

[macrouse] extern crate customderive;

[macrouse] extern crate derivebuilder;

customderive! { #[derive(Default, Builder)] struct Channel { token: i32, specialinfo: i32, // .. a whole bunch of other fields .. } }

impl Channel { // All that's left to do for you is writing a method, // which actually does something. :-) pub fn build(&self) -> String { format!("The meaning of life is {}.", self.special_info) } }

fn main() { // builder pattern, go, go, go!... let x = Channel::default().special_info(42u8).build(); println!("{:?}", x); } ```

Note that we did not write any implementation of a method called special_info. Instead the custom_derive! macro scans the #[derive(..)] attribute of the struct for non-std identifiers – in our case #[derive(Builder)] – and delegates the code generation to the Builder! macro defined in this crate.

The automatically generated setter method for the special_info field will look like this:

rust pub fn special_info<VALUE: Into<i32>>(mut self, value: VALUE) -> Self { self.special_info = value.into(); self }

Usage and Features

Gotchas

Documentation

The builder pattern is explained here, including its variants.

Changelog

Yes, we keep a changelog.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.