TedDriggs / darling

A Rust proc-macro attribute parser
MIT License
1.02k stars 66 forks source link

feat: add parse_attr_into #277

Closed cathaysia closed 8 months ago

cathaysia commented 8 months ago

add parse_attr_into macro, making the following code possible:

#[derive(Debug, Default, FromMeta)]
#[darling(default)]
pub struct Attr {
    pub default: bool,
}

#[proc_macro_attribute]
pub fn api(attr: TokenStream, input: TokenStream) -> TokenStream {
    let _attr = parse_attr_into!(attr as Attr);
    input
}
TedDriggs commented 8 months ago

I made some changes here but for some reason those aren't showing up on the PR.

cathaysia commented 8 months ago

I agree with your changes. Now that the changes have been written to the branch of this repository, this PR should be closed. If you think it's okay, please close this PR

cathaysia commented 8 months ago

I see that the branch has not been merged yet. Do you have any questions?

TedDriggs commented 8 months ago

No - I’m traveling for work right now, so haven’t had time to tidy this up.

TedDriggs commented 8 months ago

I'm going to have to revert this, since it appears to be incorrect in the macro definition. I don't think it should be ident, and I'm not sure why it passed tests with this in place.

cathaysia commented 8 months ago

ok

TedDriggs commented 8 months ago

I think it probably wants a tt or something else in this position; do you know what syn's equivalent macro takes?

TedDriggs commented 8 months ago

If we can revert and fix in rapid succession that'd be great, because I do like the concept here.

TedDriggs commented 8 months ago

Yep, it's a tt (source)

TedDriggs commented 8 months ago

Here's the call that's failing and I don't think should fail:

let x: NegatableIdentList = parse_meta!(inner(example::flag, my_feature));

The impl of NegatableIdentList shouldn't matter, as the error I'm getting is...

error: no rules expected the token `(`
  --> examples/negative_flag.rs:46:38
   |
46 |     let attr_demo = parse_meta!(inner(example::flag, my_feature));
   |                                      ^ no rules expected this token in macro call
   |
note: while trying to match meta-variable `$attr:tt`
  --> /Users/teddriggs/repos/darling/core/src/lib.rs:63:6
   |
63 |     ($attr:tt) => {
   |      ^^^^^^^^
cathaysia commented 8 months ago

may it should be ($id:ident $params:tt). Judging from the source code of quote, this piece needs to be parsed recursively.

TedDriggs commented 8 months ago

Okay - I'm not sure how we reopen a PR; can you take a fork from my branch for this and submit a new PR with the updates off that?

cathaysia commented 8 months ago

emm, ok. I will do it tomorrow


From: Ted Driggs @.> Sent: Monday, March 11, 2024 11:02:21 PM To: TedDriggs/darling @.> Cc: DragonBillow @.>; Author @.> Subject: Re: [TedDriggs/darling] feat: add parse_attr_into (PR #277)

Okay - I'm not sure how we reopen a PR; can you take a fork from my branch for this and submit a new PR with the updates off that?

― Reply to this email directly, view it on GitHubhttps://github.com/TedDriggs/darling/pull/277#issuecomment-1988652149, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AKBJ6AIJYYBW3VCP6WBDIV3YXXIQ3AVCNFSM6AAAAABD7K3FGOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOBYGY2TEMJUHE. You are receiving this because you authored the thread.Message ID: @.***>