Skip to content

Commit

Permalink
Reject multiple attributes on fields (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul authored Feb 6, 2024
1 parent f6578e9 commit 6df8d4e
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ pub fn superstruct(args: TokenStream, input: TokenStream) -> TokenStream {
.next()
.unwrap_or_default();

// Check for conflicting attributes.
check_for_conflicting_superstruct_attrs(&field.attrs);

// Drop the field-level superstruct attributes
let mut output_field = field.clone();
output_field.attrs = discard_superstruct_attrs(&output_field.attrs);
Expand Down Expand Up @@ -1080,6 +1083,21 @@ fn make_as_variant_method(
}
}

/// Check that there is at most one superstruct attribute, and panic otherwise.
fn check_for_conflicting_superstruct_attrs(attrs: &[Attribute]) {
if attrs
.iter()
.filter(|attr| is_superstruct_attr(attr))
.count()
> 1
{
// TODO: this is specific to fields right now, but we could maybe make it work for the
// top-level attributes. I'm just not sure how to get at them under the `AttributeArgs`
// stuff.
panic!("cannot handle more than one superstruct attribute per field");
}
}

/// Keep all non-superstruct-related attributes from an array.
fn discard_superstruct_attrs(attrs: &[Attribute]) -> Vec<Attribute> {
attrs
Expand Down

0 comments on commit 6df8d4e

Please sign in to comment.