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

Implement syn::Parse for FromMeta types #285

Open
sorcio opened this issue Apr 14, 2024 · 0 comments
Open

Implement syn::Parse for FromMeta types #285

sorcio opened this issue Apr 14, 2024 · 0 comments

Comments

@sorcio
Copy link

sorcio commented Apr 14, 2024

I think it should be possible to codegen a syn::Parse implementation for structs that derive From* (I'm only looking at FromMeta because that's what I'm working with, but this can apply to other derive macros).

According to #62 a blanket implementation is not possible, but code generation might very well be, especially if it's opt-in. This works for me and emits errors just as expected:

#[derive(Debug, FromMeta)]
#[darling(syn_parse)]  // <-- attribute name open for bikeshedding
struct MyMacroArgs {
    // ...
}

// generated implementation:
impl Parse for MyMacroArgs {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let items =
            syn::punctuated::Punctuated::<NestedMeta, syn::Token![,]>::parse_terminated(input)?
                .into_iter()
                .collect::<Vec<_>>();
        Ok(MyMacroArgs::from_list(&items)?)
    }
}

This goes through a Vec because FromMeta::from_list takes a slice, but I guess that we could have a method that takes impl IntoIterator (or even implement FromIterator, maybe) to avoid collecting.

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