From f1eb9d80a4aa7952078a8c133bdf7f4be4c1a707 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Mon, 30 Oct 2023 05:54:13 +0100 Subject: [PATCH 1/2] Allow customizing the struct properties name --- src/lib.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e404229..4364ca4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,14 +58,37 @@ pub fn autoprops_component( let visibility = &function.vis; let generics = &function.sig.generics; - let component_name = match args.len() { - 0 => None, + let (component_name, struct_name) = match args.len() { + 0 => ( + None, + Some(syn::Ident::new( + &format!("{}Props", fn_name), + Span::call_site().into(), + )), + ), 1 => { let TokenTree::Ident(name) = &args[0] else { panic!("Invalid argument: {}", args[0].to_string()); }; - Some(name) + ( + Some(name), + Some(syn::Ident::new( + &format!("{}Props", name), + Span::call_site().into(), + )), + ) + } + 3 => { + let TokenTree::Ident(name) = &args[0] else { + panic!("Invalid argument: {}", args[0].to_string()); + }; + + let TokenTree::Ident(props) = args[2].clone() else { + panic!("Invalid argument: {}", args[2].to_string()); + }; + + (Some(name), Some(props)) } _ => panic!("Invalid arguments: {:?}", args), }; @@ -108,7 +131,6 @@ pub fn autoprops_component( let partial_eq_constraints = arg_types.iter().map(|ty| quote! { #ty: PartialEq }); - let struct_name = syn::Ident::new(&format!("{}Props", fn_name), Span::call_site().into()); let (impl_generics, ty_generics, _) = generics.split_for_impl(); let bounds = generics.where_clause.clone(); From 957eb71a2d924d449c15e5e0838b87eb20c8fdac Mon Sep 17 00:00:00 2001 From: Valentin Golev Date: Mon, 30 Oct 2023 12:29:54 +0100 Subject: [PATCH 2/2] fix partialeq test --- tests/cases/partialeq-fail.stderr | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/cases/partialeq-fail.stderr b/tests/cases/partialeq-fail.stderr index 40030f1..f35afa2 100644 --- a/tests/cases/partialeq-fail.stderr +++ b/tests/cases/partialeq-fail.stderr @@ -12,5 +12,6 @@ note: an implementation of `PartialEq` might be missing for `NotPartialEq` = note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider annotating `NotPartialEq` with `#[derive(PartialEq)]` | -14 | #[derive(PartialEq)] +14 + #[derive(PartialEq)] +15 | struct NotPartialEq(); |