Skip to content

Parse multiple attributes #7

@kangalio

Description

@kangalio

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions