#builder-pattern #macro-derive #derive-builder #annotated #m4cro

macro builder_m4cro

Simple derive macro to implement the builder pattern

5 releases (3 breaking)

0.4.0 Jul 1, 2024
0.3.1 May 3, 2024
0.3.0 May 3, 2024
0.2.0 Apr 21, 2024
0.1.0 Jan 27, 2024

#904 in Procedural macros

Download history 3/week @ 2024-05-31 5/week @ 2024-06-07 2/week @ 2024-06-14 222/week @ 2024-06-28 17/week @ 2024-07-05 1/week @ 2024-07-12

418 downloads per month
Used in tquest

MIT/Apache

18KB
317 lines

ci crates.io

This is a crate provides two simple macros:

  • Builder - an implementation of the factory pattern
  • BuilderFromDefault - factory pattern implementation that relies on the implementation of the Default trait, to provide customized init values

For more examples look at:

  • Builder: examples/builder/src/main.rs
  • BuilderFromDefault: examples/builder_from_default/src/main.rs
use builder_m4cro::Builder;

/// Custom type that's annotated with the Builder macro
#[derive(Builder, Debug)]
pub struct TestType {
    pub a_unsigned: u32,
    pub a_opt_unsigned: Option<u32>,
    pub a_string: String,
    pub a_opt_string: Option<String>,
}

/// The macro expandes to the following code
impl TestType {
    pub fn builder() -> TestTypeBuilder {
        TestTypeBuilder::new()
    }
}
pub struct TestTypeBuilder {
    a_unsigned: Option<u32>,
    a_string: Option<String>,
    a_opt_unsigned: Option<u32>,
    a_opt_string: Option<String>,
}
// ...

impl TestTypeBuilder {
    pub fn new() -> TestTypeBuilder {
        TestTypeBuilder::default()
    }
    pub fn a_unsigned(&mut self, a_unsigned: u32) -> &mut Self {
        self.a_unsigned = Some(a_unsigned);
        self
    }
    pub fn a_string(&mut self, a_string: &str) -> &mut Self {
        self.a_string = Some(a_string.to_string());
        self
    }
    pub fn a_opt_unsigned(&mut self, a_opt_unsigned: u32) -> &mut Self {
        self.a_opt_unsigned = Some(a_opt_unsigned);
        self
    }
    pub fn a_opt_string(&mut self, a_opt_string: &str) -> &mut Self {
        self.a_opt_string = Some(a_opt_string.to_string());
        self
    }
    pub fn build(&self) -> Result<TestType, String> {
        Ok(TestType {
            a_unsigned: self
                .a_unsigned
                .clone()
                .ok_or_else(|| {
                    let res = ::alloc::fmt::format(
                        format_args!("{0} is not set", "a_unsigned"),
                    );
                    res
                })?,
            a_string: self
                .a_string
                .clone()
                .ok_or_else(|| {
                    let res = ::alloc::fmt::format(
                        format_args!("{0} is not set", "a_string"),
                    );
                    res
                })?,
            a_opt_unsigned: self.a_opt_unsigned.clone(),
            a_opt_string: self.a_opt_string.clone(),
        })
    }
}

Helper to support marco dev

# Install cargo expand
# https://github.com/dtolnay/cargo-expand
cargo install cargo-expand

# e. g. ...
cd examples/result_builder
cargo expand

Dependencies

~260–710KB
~17K SLoC