Skip to content

Commit

Permalink
Allow customizing the struct properties name
Browse files Browse the repository at this point in the history
  • Loading branch information
cecton committed Oct 30, 2023
1 parent 070689a commit f1eb9d8
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit f1eb9d8

Please sign in to comment.