Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复同名字段冲突的问题 #6

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions macro/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,36 @@ impl<'a> From<ControlStruct<'a>> 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 = <leptos_controls::RwSignalField<#struct_ident,#ty> as leptos::SignalGetUntracked>::get_untracked(&#ident);
let #field_ident = <leptos_controls::RwSignalField<#field_struct_ident,#ty> as leptos::SignalGetUntracked>::get_untracked(&#field_ident);
}
});

Expand All @@ -65,8 +65,8 @@ impl<'a> From<ControlStruct<'a>> 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();
Expand All @@ -80,7 +80,7 @@ impl<'a> From<ControlStruct<'a>> for TokenStream {
}
};
quote! {
if #validate_method(&<leptos_controls::RwSignalField<#struct_ident,#ty> as leptos::SignalGetUntracked>::get_untracked(&self.#ident)) {
if #validate_method(&<leptos_controls::RwSignalField<#field_struct_ident,#ty> as leptos::SignalGetUntracked>::get_untracked(&self.#field_ident)) {
None
}else{
Some(#error)
Expand Down
4 changes: 2 additions & 2 deletions macro/src/expand.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -33,7 +33,7 @@ pub fn derive(input: DeriveInput) -> TokenStream {

fn try_expand(input: &DeriveInput) -> Result<TokenStream, Error> {
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]))
}
17 changes: 9 additions & 8 deletions macro/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FieldEnum<'a>> for TokenStream {
fn from(value: FieldEnum<'a>) -> Self {
impl<'a> From<FieldStruct<'a>> 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;
}
Expand Down
4 changes: 2 additions & 2 deletions macro/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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('_')
Expand All @@ -119,7 +119,7 @@ impl FormFieldOptions {
})
.collect::<Vec<_>>()
.join("");
format_ident!("__{}", lit)
format_ident!("__{}__{}", parent, lit)
}

pub fn ty(&self) -> &Type {
Expand Down