Remove some unnecessary clones in the GBNF derive macro.

This commit is contained in:
projectmoon 2024-02-05 16:33:53 +01:00
parent 1e80ae508e
commit f7df8af068
1 changed files with 11 additions and 16 deletions

View File

@ -37,26 +37,22 @@ fn generate_gbnf(input: TokenStream, create_struct: bool) -> TokenStream {
// run, and then shove the resulting GBNF rule into the type as a // run, and then shove the resulting GBNF rule into the type as a
// static string. // static string.
if let Ok(expr_struct) = syn::parse::<GbnfStructDef>(input.clone()) { if let Ok(expr_struct) = syn::parse::<GbnfStructDef>(input) {
let struct_name_str = LitStr::new(&expr_struct.name.to_string(), Span::call_site().into()); let struct_name_str = LitStr::new(&expr_struct.name.to_string(), Span::call_site().into());
let struct_name = expr_struct.name; let struct_name = expr_struct.name;
let fields = expr_struct.fields.iter(); let fields = expr_struct.fields.iter();
let gbnfs: Vec<_> = expr_struct let gbnfs = expr_struct.fields.iter().map(|field| {
.fields let field_type = &field.ty;
.iter() let field_ident = field
.map(|field| { .ident
let field_type = &field.ty; .as_ref()
let field_ident = field .map(|i| i.to_string())
.ident .map(|field_name| LitStr::new(&field_name, Span::call_site().into()))
.clone() .expect("no ident");
.map(|i| i.to_string())
.map(|field_name| LitStr::new(&field_name, Span::call_site().into()))
.expect("no ident");
quote! { gbnf_field!(#field_ident, #field_type) } quote! { gbnf_field!(#field_ident, #field_type) }
}) });
.collect();
let struct_frag = if create_struct { let struct_frag = if create_struct {
quote! { quote! {
@ -95,7 +91,6 @@ fn generate_gbnf(input: TokenStream, create_struct: bool) -> TokenStream {
} }
} }
/// Create a GBNF complex type as a Rust struct. /// Create a GBNF complex type as a Rust struct.
#[proc_macro] #[proc_macro]
pub fn gbnf_complex(input: TokenStream) -> TokenStream { pub fn gbnf_complex(input: TokenStream) -> TokenStream {