You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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")]pubstructChart{#[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:
pubfntitle(mutself,title:Title) -> Self{self.title.push(title);self}pubfntooltip(mutself,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")]pubstructChart{#[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:
pubfntitle(mutself,title:Title) -> Self{self.title.push(title);self}pubfntooltip(mutself,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.
The text was updated successfully, but these errors were encountered:
We should probably use or build on something like derive_setters
LukaOber
changed the title
Add proc macro to generate functions for the builder pattern automatically
Add proc macro to generate functions for the setter pattern automatically
Oct 31, 2024
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:
And we manually implement the function to set the fields:
With a proc macro we could switch to this:
And the derived trait will automatically generate the functions for Vec<T> and Option<T> like this without having to write them manually:
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.
The text was updated successfully, but these errors were encountered: