Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to extend collection-like fields #196

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions derive_builder/tests/setter_extend.rs
@@ -0,0 +1,43 @@
#[macro_use]
extern crate pretty_assertions;
#[macro_use]
extern crate derive_builder;

use std::collections::HashMap;

#[derive(Debug, PartialEq, Default, Builder, Clone)]
struct Lorem {
#[builder(extend)]
foo: String,
#[builder(extend)]
bar: Vec<String>,
#[builder(extend)]
baz: HashMap<String, i32>,
}

#[test]
fn generic_field() {
let x = LoremBuilder::default()
.foo("foo".into())
.bar_extend_one("bar".into())
.bar_extend_one("bar bar".into())
.bar_extend_one("bar bar bar".into())
.foo_extend_one('-')
.baz_extend_one(("baz".into(), 1))
.baz_extend_one(("bazz".into(), 2))
.baz_extend_one(("bazzz".into(), 3))
.foo_extend_one("foo")
.build()
.unwrap();

assert_eq!(
x,
Lorem {
foo: "foo-foo".into(),
bar: vec!["bar".into(), "bar bar".into(), "bar bar bar".into()],
baz: vec![("baz".into(), 1), ("bazz".into(), 2), ("bazzz".into(), 3)]
.into_iter()
.collect(),
}
);
}
8 changes: 8 additions & 0 deletions derive_builder_core/src/macro_options/darling_opts.rs
Expand Up @@ -238,6 +238,8 @@ pub struct Field {
try_setter: Flag,
#[darling(default)]
field: FieldMeta,
#[darling(default)]
extend: Flag,
}

impl FlagVisibility for Field {
Expand Down Expand Up @@ -442,6 +444,11 @@ impl<'a> FieldWithDefaults<'a> {
.unwrap_or(true)
}

/// Check if this field should emit a setter for extend.
pub fn setter_extend_enabled(&self) -> bool {
self.field.extend.into()
}

pub fn field_enabled(&self) -> bool {
self.field
.setter
Expand Down Expand Up @@ -555,6 +562,7 @@ impl<'a> FieldWithDefaults<'a> {
generic_into: self.setter_into(),
strip_option: self.setter_strip_option(),
deprecation_notes: self.deprecation_notes(),
extend: self.setter_extend_enabled(),
}
}

Expand Down
29 changes: 27 additions & 2 deletions derive_builder_core/src/setter.rs
Expand Up @@ -62,6 +62,8 @@ pub struct Setter<'a> {
pub strip_option: bool,
/// Emit deprecation notes to the user.
pub deprecation_notes: &'a DeprecationNotes,
/// Emit extend
pub extend: bool,
}

impl<'a> ToTokens for Setter<'a> {
Expand Down Expand Up @@ -135,7 +137,8 @@ impl<'a> ToTokens for Setter<'a> {
let mut new = #self_into_return_ty;
new.#field_ident = ::derive_builder::export::core::option::Option::Some(#into_value);
new
}));
}
));

if self.try_setter {
let try_ty_params =
Expand All @@ -151,7 +154,28 @@ impl<'a> ToTokens for Setter<'a> {
let mut new = #self_into_return_ty;
new.#field_ident = ::derive_builder::export::core::option::Option::Some(converted);
Ok(new)
}));
}
));
}

if self.extend {
let ident_extend_one = format_ident!("{}_extend_one", self.ident);

tokens.append_all(quote!(
#(#attrs)*
#[allow(unused_mut)]
#vis fn #ident_extend_one <ITEM>(#self_param, item: ITEM) -> #return_ty
where
#ty: ::derive_builder::export::core::default::Default + ::derive_builder::export::core::iter::Extend<ITEM>,
{
#deprecation_notes
let mut new = #self_into_return_ty;
new.#field_ident
.get_or_insert_with(|| ::derive_builder::export::core::default::Default::default())
.extend(::derive_builder::export::core::option::Option::Some(item));
new
}
));
}
}
}
Expand Down Expand Up @@ -222,6 +246,7 @@ macro_rules! default_setter {
generic_into: false,
strip_option: false,
deprecation_notes: &Default::default(),
extend: false,
};
};
}
Expand Down