3 releases (breaking)

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

#11 in #named-fields

Download history 1/week @ 2024-02-14 9/week @ 2024-02-21 6/week @ 2024-02-28 3/week @ 2024-03-13 28/week @ 2024-03-27 34/week @ 2024-04-03

65 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
~34K SLoC