3 releases (breaking)

0.3.0 Sep 11, 2022
0.2.0 Sep 6, 2022
0.1.0 Aug 29, 2022

#13 in #named-fields

21 downloads per month
Used in eiga

MIT license

8KB
106 lines

This crate provides a derive macro that implements the builder lite pattern.

Since this was designed to be used by eiga, it makes some assumptions:

  • The target struct has named fields.
  • Optional fields have their type written as Option<...>. The macro won't recognize the Option type in any other form, e.g., std::option::Option.
  • Optional fields represent query string parameters.

Example

Applying #[derive(Builder)] to

#[derive(Builder)]
struct Foo<'a> {
    x: i32,
    y: Option<&'a str>,
}

generates

impl<'a> Foo<'a> {
    /// Constructs a new [`Foo`].
    pub fn new(x: i32) -> Self {
        Self {
            x,
            y: None,
        }
    }

    /// Sets the y query string parameter.
    pub fn y(mut self, y: &'a str) -> Self {
        self.y = Some(y);
        self
    }
}

Dependencies

~1.5MB
~37K SLoC