yuankunzhang / charming

A visualization library for Rust
Apache License 2.0
1.85k stars 74 forks source link

Add proc macro to generate functions for the setter pattern automatically #76

Open LukaOber opened 2 months ago

LukaOber commented 2 months ago

Adding a proc macro that we can derive on the structs could automatically generate most functions for the builder pattern for us.

Currently we have this:

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Chart {
    #[serde(skip_serializing_if = "Vec::is_empty")]
    title: Vec<Title>,

    #[serde(skip_serializing_if = "Option::is_none")]
    tooltip: Option<Tooltip>,

   // cut for brevity

And we manually implement the function to set the fields:

pub fn title(mut self, title: Title) -> Self {
    self.title.push(title);
    self
}

pub fn tooltip(mut self, tooltip: Tooltip) -> Self {
    self.tooltip = Some(tooltip);
    self
}

// cut for brevity

With a proc macro we could switch to this:

#[derive(Serialize, CharmingBuilder)]
#[serde(rename_all = "camelCase")]
pub struct Chart {
    #[serde(skip_serializing_if = "Vec::is_empty")]
    title: Vec<Title>,

    #[serde(skip_serializing_if = "Option::is_none")]
    tooltip: Option<Tooltip>,

   // cut for brevity

And the derived trait will automatically generate the functions for Vec<T> and Option<T> like this without having to write them manually:

pub fn title(mut self, title: Title) -> Self {
    self.title.push(title);
    self
}

pub fn tooltip(mut self, tooltip: Tooltip) -> Self {
    self.tooltip = Some(tooltip);
    self
}

// cut for brevity

Pros: Would cut down on potential errors and time for adding new fields.

Cons: Slight increase in fresh compilation time.

I think it would be better to use a proc macro to reduce a lot of repetition in the crate and I would also be able to prepare the corresponding PR if you are interested.

LukaOber commented 2 months ago

We should probably use or build on something like derive_setters

LukaOber commented 3 weeks ago

I opened up a draft pr in #105. If someone is familiar with proc macros, please feel free to check it out and comment on it.