I’m working on a Rust procedural macro and encountering an issue while parsing an attribute. The goal is to ensure the attribute follows a specific structure and to extract an expression from it. However, I’m getting an Err object when parsing the attribute even though it appears to be correctly set.
Here’s the function I’m using:
use syn::{self, parse::Parser};
fn is_valid_vec_attribute(
attrs: &Vec<syn::Attribute>,
) -> Result<syn::Ident, Box<dyn std::error::Error>> {
if attrs.len() != 1 {
panic!("Only 1 attribute is authorized");
}
let attr = &attrs[0];
if let syn::Meta::List(ref meta_list) = attr.meta {
if !meta_list.path.is_ident("builder") {
panic!("Attribute path must be `builder`");
}
let data: Result<syn::Expr, syn::Error> = attr.parse_args();
println!("{:#?}", data);
// Additional processing of `data` here...
} else {
panic!("Only List attributes are allowed like such: `path(..)`");
}
// Return a valid identifier for now
Ok(syn::Ident::new("dummy", proc_macro2::Span::call_site()))
}
The attribute I’m trying to parse looks like this:
#[builder(each = "args")]
Despite this, the data variable results in an Err object. Here’s the debug output I’m seeing:
Err(
Error(
"unexpected token",
),
)
What could be causing this parsing error, and how can I correctly extract the expression from the attribute?
What I’ve Tried:
- Ensured that the attribute path is correctly identified as builder.
- Checked the structure of the Meta::List to confirm it is correct.
Any insights or suggestions on how to resolve this would be greatly appreciated!
1