Skip to content

Commit d624bec

Browse files
authored
fix(sol-macro): internal SC derives (#1017)
* Fix internal SC derives * Simplify * Idiomatic * Redundant filter * Unary operation * Refactor * Typo
1 parent 5356363 commit d624bec

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

crates/sol-macro-input/src/json.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ impl SolInput {
2424
.into_iter()
2525
.partition::<Vec<_>, _>(|attr| matches!(attr.style, AttrStyle::Inner(_)));
2626

27-
let derives =
28-
attrs.iter().filter(|attr| attr.path().is_ident("derive")).collect::<Vec<_>>();
27+
let (derives, sol_derives) = extract_derive_attrs(&attrs);
2928

3029
let mut library_tokens_iter = all_tokens
3130
.by_ref()
@@ -47,6 +46,7 @@ impl SolInput {
4746

4847
let tokens = quote! {
4948
#(#derives)*
49+
#(#sol_derives)*
5050
#sol_library_tokens
5151
};
5252

@@ -131,6 +131,28 @@ pub fn tokens_for_sol(name: &Ident, sol: &str) -> Result<TokenStream> {
131131
.collect())
132132
}
133133

134+
/// Extract both regular and `sol` derive attributes for propagation further.
135+
fn extract_derive_attrs(attrs: &[syn::Attribute]) -> (Vec<&syn::Attribute>, Vec<&syn::Attribute>) {
136+
attrs.iter().fold((Vec::new(), Vec::new()), |(mut derives, mut sol_derives), attr| {
137+
if attr.path().is_ident("derive") {
138+
derives.push(attr);
139+
} else if attr.path().is_ident("sol") {
140+
if let Ok(meta) = attr.meta.require_list() {
141+
let mut contains_derives = false;
142+
let _ = meta.parse_nested_meta(|meta| {
143+
contains_derives |=
144+
meta.path.is_ident("all_derives") || meta.path.is_ident("extra_derives");
145+
Ok(())
146+
});
147+
if contains_derives {
148+
sol_derives.push(attr);
149+
}
150+
}
151+
}
152+
(derives, sol_derives)
153+
})
154+
}
155+
134156
#[inline]
135157
#[track_caller]
136158
fn id(s: impl AsRef<str>) -> Ident {

0 commit comments

Comments
 (0)