I'm trying to setup unit tests for some functions used in a proc-macro-attribute. It is proving quite hard to replicate syn::parse_macro_input!(args as AttributeArgs) which is what is passed to the functions.
I expected this to work, but cannot see why parse_args fails with Error("unexpected token"):
let ts: Vec<syn::Attribute> = syn::parse::Parser::parse_str(
syn::Attribute::parse_outer,
"#[mine(name = \"name\", tf = false)]",
)
.unwrap();
println!("{:#?}", ts);
let _args: Vec<syn::NestedMeta> = ts
.iter()
// this fails:
.map(|attr| attr.parse_args::<syn::NestedMeta>().unwrap())
.collect();
One NestedMeta represents something like name = "name" or tf = false, but a combination of those two, comma-separated, requires expressing that extra property. In syn, this involves the Punctuated<Element, Separator> type, and then a parsing flavor (either trailing separators and empty sequences are tolerated (parse_terminated flavor), or neither are (parse_separated_nonempty, for rarer use cases)).
Finally, you have to realize that
#[mine(name = "name")]
#[mine(tf = false)]
will probably have to yield equivalent semantics to your use case. So you have two dimensions to iterate through, and yet want to compile a single-dimensional Vec. It's time to .flatten() (or, since we already have a .map(), to .flat_map() instead). Hence yielding: