Skip to content

imbrn/building

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

building

Rust procedural macro for deriving Builder in structs.

This project was developed while I was playing with the Builder section of this workshop teaching about Rust procedural macros.

Simple usage example

use building::Builder;
use std::vec::Vec;

#[derive(Builder)]
pub struct Product {
    id: i32,
    code: Option<u32>,
    name: String,
    description: Option<String>,
    #[builder(each = "tag")]
    tags: Vec<String>,
}

fn main() {
    let product = Product::builder()
        .id(12)
        .code(654321)
        .name("Foo".to_owned())
        .description("Bar".to_owned())
        .tags(vec!["foo".to_owned(), "bar".to_owned()])
        .tag("baz".to_owned())
        .build()
        .unwrap();

    assert_eq!(12, product.id);
    assert_eq!(Some(654321), product.code);
    assert_eq!("Foo".to_owned(), product.name);
    assert_eq!(Some("Bar".to_owned()), product.description);
    assert_eq!(
        vec!["foo".to_owned(), "bar".to_owned(), "baz".to_owned()],
        product.tags
    );
}

License

MIT License

About

Rust procedural macro for derive Builder

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages