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

Reject multiple attributes on fields #38

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading