【求助】属性宏想要实现如#[hello(name = "Rust", order = 1)]这样的效果,该怎么做呢
我通过类属性宏的方式去实现类似的效果,但是不知道下面的问题怎么解决
1、如何定义name
,order
,或者如何获取name
,order
?
2、定义成类属性宏不能作用在struct
的字段上?会出现expected non-macro attribute, found attribute macro
excel_col not a non-macro attribute
这样的错误。
1
共 2 条评论, 1 页
评论区
写评论非常感谢,我晚上试一试
--
👇
Grobycn: ``` use proc_macro::TokenStream; use syn::{LitStr, LitInt, Token, parse::{Parse, ParseStream}}; use syn::parse_macro_input;
mod kw { use syn::custom_keyword;
}
#[derive(Debug)] struct Hello { name: LitStr, order: LitInt }
impl Parse for Hello { fn parse(input: ParseStream<'>) -> syn::Result { let (mut name, mut order) = (None, None); loop { let lookahead = input.lookahead1(); if lookahead.peek(kw::name) { input.parse::kw::name()?; input.parse::<Token![=]>()?; name = Some(input.parse::()?); } else if lookahead.peek(kw::order) { input.parse::kw::order()?; input.parse::<Token![=]>()?; order = Some(input.parse::()?); } else { return Err(input.error("invalid argument")); } if let Err() = input.parse::<Token![,]>() { break; } } match (name, order) { (Some(name), Some(order)) => Ok(Self { name, order }), _ => Err(input.error("missing some argument")), } } }
#[proc_macro_attribute] pub fn hello(args: TokenStream, input: TokenStream) -> TokenStream { let hello = parse_macro_input!(args as Hello); // do what you want with hello and input stream unimplemented!() }