structbuilder_derive   ![Latest Version]

A simple rust-lang macro to derive a _Builder trait for your struct. The trait lets you build up a struct in a modular fashion, and provides read-only reference access to members.

Usage

Add the crate as a dependency in your Cargo.toml:

[dependencies] structbuilder_derive = "1.0"

The simply import the deriver and add the derive(StructBuilder) directive as follows:

``` use structbuilder_derive::*;

[derive(StructBuilder)]

pub struct MyStruct { name: String, age: usize, partner_name: Option } ```

This will create a <StructName>Builder (e.g. MyStructBuilder trait), which provides a ::new(...) method, which takes every non-Option field as a parameter to create the basic struct. Also included are instance methods <field_name>(), which returns a reference to the field called <field_name>, and with_<field_name>(...) for every field, which sets the value of <field_name> to the provided value. For example:

```

fn main() { let name = String::from("Reid"); let age = 24; let s = MyStruct::new(name, age);

println!("{}", s.age()); // -> 24
println!("{:?}", s.partner_name()); // -> None

let s2 = MyStruct::new(String::from("Jack"), 21).with_partner_name(String::from("Sally"));

println!("{}", s2.age()); // -> 21
println!("{:?}", s.partner_name()); // -> Some("Sally")

} ```

The deriver also provides camelCase fields with snake_case accessors and setters; this is convenient with, for example, JSON and serde, where the external resource might provide a data shape like {"userId": 12, "userName": "f.mal"}; you can define your struct to match this, and allow serde to serialize and deserialize this structure, while allowing your code to use Rust-appropriate .user_id() and .with_user_name(...).

Notes and warnings