Skip to content

Commit

Permalink
Panic if more than one attribute found
Browse files Browse the repository at this point in the history
In stratisd, we always expect exactly one. If that changes, we'll
probably have to enrich this method somehow.

Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Sep 9, 2024
1 parent 247d530 commit 2475a2b
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions stratisd_proc_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,23 @@ fn get_attr_level(attrs: &mut Vec<Attribute>) -> Option<Ident> {

/// Determine whether a method has the given attribute.
fn has_attribute(attrs: &mut Vec<Attribute>, attribute: &str) -> bool {
if let Some((index, _)) = attrs
let matching_attrs = attrs
.iter()
.enumerate()
.filter(|(_, a)| a.path().is_ident(attribute))
.last()
{
attrs.remove(index);
true
} else {
false
.collect::<Vec<_>>();

match matching_attrs.len() {
0 => false,
1 => {
let (index, _) = matching_attrs[0];
attrs.remove(index);
true
}
_ => panic!(
"More than 1 {} attribute specified for the same syntactic entity.",
attribute
),
}
}

Expand Down

0 comments on commit 2475a2b

Please sign in to comment.