diff --git a/macro/src/control.rs b/macro/src/control.rs index ed0e5a2..32d9191 100644 --- a/macro/src/control.rs +++ b/macro/src/control.rs @@ -25,36 +25,36 @@ impl<'a> From> for TokenStream { // 控制器字段 let field_with_type_tokens = options.fields().iter().map(|field| { let ty = field.ty(); - let ident = field.ident(); - let struct_ident = field.struct_ident(); + let field_ident = field.ident(); + let field_struct_ident = field.struct_ident(ident); quote! { - #ident: leptos_controls::RwSignalField<#struct_ident, #ty> + #field_ident: leptos_controls::RwSignalField<#field_struct_ident, #ty> } }); // 创建RwSignalField let set_signal_tokens = options.fields().iter().map(|field| { - let ident = field.ident(); + let field_ident = field.ident(); quote! { - let #ident = leptos_controls::RwSignalField::new(#ident); + let #field_ident = leptos_controls::RwSignalField::new(#field_ident); } }); // rest函数 let fn_reset_tokens = options.fields().iter().map(|field| { - let ident = field.ident(); + let field_ident = field.ident(); quote! { - self.#ident.set_default(); + self.#field_ident.set_default(); } }); // snapshot函数 let get_untracked_tokens = options.fields().iter().map(|field| { let ty = field.ty(); - let ident = field.ident(); - let struct_ident = field.struct_ident(); + let field_ident = field.ident(); + let field_struct_ident = field.struct_ident(ident); quote! { - let #ident = as leptos::SignalGetUntracked>::get_untracked(&#ident); + let #field_ident = as leptos::SignalGetUntracked>::get_untracked(&#field_ident); } }); @@ -65,8 +65,8 @@ impl<'a> From> for TokenStream { .filter(|field| field.validate().is_some()) .map(|field| { let ty = field.ty(); - let ident = field.ident(); - let struct_ident = field.struct_ident(); + let field_ident = field.ident(); + let field_struct_ident = field.struct_ident(ident); let label = field.label(); let message = field.message(); let validate = field.validate().unwrap(); @@ -80,7 +80,7 @@ impl<'a> From> for TokenStream { } }; quote! { - if #validate_method(& as leptos::SignalGetUntracked>::get_untracked(&self.#ident)) { + if #validate_method(& as leptos::SignalGetUntracked>::get_untracked(&self.#field_ident)) { None }else{ Some(#error) diff --git a/macro/src/expand.rs b/macro/src/expand.rs index d50acb4..d0dc25f 100644 --- a/macro/src/expand.rs +++ b/macro/src/expand.rs @@ -1,5 +1,5 @@ use crate::control::ControlStruct; -use crate::field::FieldEnum; +use crate::field::FieldStruct; use crate::options::FormOptions; use darling::{Error, FromDeriveInput}; use proc_macro2::TokenStream; @@ -33,7 +33,7 @@ pub fn derive(input: DeriveInput) -> TokenStream { fn try_expand(input: &DeriveInput) -> Result { let options = FormOptions::from_derive_input(input).and_then(FormOptions::validate)?; - let field_enum = TokenStream::from(FieldEnum::new(&options)); + let field_enum = TokenStream::from(FieldStruct::new(&options)); let control_struct = TokenStream::from(ControlStruct::new(&options)); Ok(TokenStream::from_iter([field_enum, control_struct])) } diff --git a/macro/src/field.rs b/macro/src/field.rs index 0292e7b..a93dcba 100644 --- a/macro/src/field.rs +++ b/macro/src/field.rs @@ -2,37 +2,38 @@ use crate::options::FormOptions; use proc_macro2::TokenStream; use quote::quote; -pub struct FieldEnum<'a> { +pub struct FieldStruct<'a> { options: &'a FormOptions, } -impl<'a> FieldEnum<'a> { +impl<'a> FieldStruct<'a> { pub fn new(options: &'a FormOptions) -> Self { Self { options } } } -impl<'a> From> for TokenStream { - fn from(value: FieldEnum<'a>) -> Self { +impl<'a> From> for TokenStream { + fn from(value: FieldStruct<'a>) -> Self { let options = value.options; + let ident = options.ident(); let vis = options.vis(); // 字段结构体 let field_struct_tokens = options.fields().iter().map(|field| { - let pascal_case_ident = field.struct_ident(); + let field_struct_ident = field.struct_ident(ident); quote! { #[doc(hidden)] #[derive(Clone,Copy)] - #vis struct #pascal_case_ident; + #vis struct #field_struct_ident; } }); // 实现FieldMeta let impl_field_meta_tokens = options.fields().iter().map(|field| { - let pascal_case_ident = field.struct_ident(); + let field_struct_ident = field.struct_ident(ident); let label = field.label(); let required = field.validate().is_some(); quote! { - impl leptos_controls::FieldMeta for #pascal_case_ident { + impl leptos_controls::FieldMeta for #field_struct_ident { const LABEL: &'static str = #label; const REQUIRED: bool = #required; } diff --git a/macro/src/options.rs b/macro/src/options.rs index ffb0f16..3a5e936 100644 --- a/macro/src/options.rs +++ b/macro/src/options.rs @@ -105,7 +105,7 @@ impl FormFieldOptions { self.ident.as_ref().expect("Ident is not exists!") } - pub fn struct_ident(&self) -> Ident { + pub fn struct_ident(&self, parent: &Ident) -> Ident { let lit = format!("{}", self.ident()); let lit = lit .split('_') @@ -119,7 +119,7 @@ impl FormFieldOptions { }) .collect::>() .join(""); - format_ident!("__{}", lit) + format_ident!("__{}__{}", parent, lit) } pub fn ty(&self) -> &Type {