Skip to content

Commit

Permalink
Properly forward attrs in FromAttributes
Browse files Browse the repository at this point in the history
Fixes #295
  • Loading branch information
TedDriggs committed Jul 9, 2024
1 parent 6a057da commit d32836d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions core/src/codegen/from_attributes_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl ToTokens for FromAttributesImpl<'_> {
}
}

let passed_attrs = self.forward_attrs.as_initializer();
let inits = self.base.initializers();
let default = self.base.fallback_decl();

Expand All @@ -61,6 +62,7 @@ impl ToTokens for FromAttributesImpl<'_> {
#default

::darling::export::Ok(#ty_ident {
#passed_attrs
#inits
}) #post_transform
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/options/from_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<'a> From<&'a FromAttributesOptions> for FromAttributesImpl<'a> {
FromAttributesImpl {
base: (&v.base.container).into(),
attr_names: &v.base.attr_names,
forward_attrs: Default::default(),
forward_attrs: v.base.as_forward_attrs(),
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/forward_attrs_to_from_attributes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use darling::FromAttributes;
use syn::parse_quote;

#[derive(Default, darling::FromAttributes)]
#[darling(attributes(builder), forward_attrs)]
struct Params {
default: Option<syn::Expr>,
attrs: Vec<syn::Attribute>,
}

#[test]
fn forward_attrs_with_field() {
let input: syn::DeriveInput = parse_quote! {
#[doc = "Hello"]
#[builder(default = 15)]
struct Example;
};

let parsed = Params::from_attributes(&input.attrs).unwrap();
assert!(parsed.default.is_some());
assert_eq!(parsed.attrs.len(), 1);
}

0 comments on commit d32836d

Please sign in to comment.