How to parse the TokenStream inside the custom attributes

I’m currently writing a procedural macros. I want to use some syntax similar to serde_derive:

struct Foo {
    #[bar(default = "default")]
    pub foo: String,

The content inside the parenthneses – default = "default"is a TokenStream. But I have no idea how to parse it because syn::parse doesn’t accept reference.

There’s an interpret_meta method on Attribute that looks like what you’re after. If you called it on that attribute, I think you’d get Some(Meta::NameValue(...)) back with the information you’re looking for.

1 Like

Oh, thanks for pointing this out!

1 Like