attribute-derive
Basically clap for attribute macros:
use Attribute;
use Type;
Will be able to parse an attribute like this:
)]
Limitations
There are some limitations in syntax parsing that will be lifted future releases.
- literals in top level (meaning something like
#[attr(42, 3.14, "hi")] - function like arguments (something like
#[attr(view(a = "test"))] - other syntaxes, maybe something like
key: value
Parse methods
There are multiple ways of parsing a struct deriving Attribute.
For helper attributes there is:
Attribute::from_attributeswhich takes in anIntoIterator<Item = &'a syn::Attribute(e.g. a&Vec<syn::Attribute>). Most useful for derive macros.Attribute::remove_attributeswhich takes an&mut Vec<syn::Attribute>and does not only parse theAttributebut also removes those matching. Useful for helper attributes for proc macros, where the helper attributes need to be removed.
For parsing a single TokenStream e.g. for parsing the proc macro input there a two ways:
Attribute::from_argstaking in aTokenStream- As
derive(Attribute)also derivesParseso you can use the parse API, e.g. withparse_macro_input!(tokens as Attribute).