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

Parse multiple attributes #7

Open
kangalio opened this issue Mar 6, 2022 · 0 comments
Open

Parse multiple attributes #7

kangalio opened this issue Mar 6, 2022 · 0 comments

Comments

@kangalio
Copy link

kangalio commented Mar 6, 2022

How to parse multiple attributes into a single struct? For example

// Library proc macro code
#[derive(bae::FromAttributes)]
struct Poise {
    name: Option<syn::LitStr>,
    placeholder: Option<syn::LitStr>,
    min_length: Option<syn::LitInt>,
    max_length: Option<syn::LitInt>,
}

// User code
struct MyModal {
    #[poise(name = "First input label")]
    #[poise(placeholder = "Your first input goes here")]
    #[poise(min_length = 5)]
    #[poise(min_length = 500)]
    first_input: String,
}

When parsing the field attributes with Poise::from_attributes(&field.attrs), only the first attributes is parsed:

Poise { name: Some(LitStr { token: "First input label" }), placeholder: None, min_length: None, max_length: None, paragraph: None }

I assume this is because the parser code immediately returns upon encountering the first valid attribute:

bae/src/lib.rs

Lines 165 to 173 in 3b018d1

for attr in attrs {
match attr.path.get_ident() {
Some(ident) if ident == #attr_name => {
return Some(syn::parse2::<Self>(attr.tokens.clone())).transpose()
}
// Ignore other attributes
_ => {},
}
}

Would a PR for parsing all valid attributes be accepted?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant