Skip to content

Commit 66054ae

Browse files
committed
refactor insertable
1 parent e43043f commit 66054ae

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

macros/src/lib.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,29 @@ pub fn derive_insertable(input: proc_macro::TokenStream) -> proc_macro::TokenStr
1919
};
2020

2121
let name = derive_input.ident;
22-
let fields = struct_fields_idents(derive_input.data);
23-
let fields_to_stringify = fields.iter().map(|field| field.unraw());
24-
let number_of_fields = fields.len();
22+
let field_idents = struct_field_idents(derive_input.data);
23+
let field_idents_to_stringify = field_idents.iter().map(|field| field.unraw());
2524

2625
let table_struct = parse_table_struct(opts.table_struct);
2726

28-
let expanded = quote! {
27+
quote! {
2928
#[::async_trait::async_trait]
3029
#[automatically_derived]
31-
impl Insertable<#number_of_fields> for #name {
30+
impl Insertable for #name {
3231
type T = #table_struct;
3332

3433
fn columns() -> &'static [&'static str] {
35-
[#(stringify!(#fields_to_stringify)),*]
34+
[#(stringify!(#field_idents_to_stringify)),*]
3635
}
3736

3837
fn bind(
3938
self,
4039
separated: &mut ::sqlx::query_builder::Separated<'_, '_, ::sqlx::Postgres, &'static str>
4140
) {
42-
#(separated.push_bind(self.#fields);)*
41+
#(separated.push_bind(self.#field_idents);)*
4342
}
4443
}
45-
};
46-
47-
expanded.into()
44+
}.into()
4845
}
4946

5047
#[derive(FromDeriveInput, Debug)]
@@ -65,7 +62,7 @@ pub fn derive_changeset(input: proc_macro::TokenStream) -> proc_macro::TokenStre
6562
};
6663

6764
let name = derive_input.ident;
68-
let fields = struct_fields_idents(derive_input.data);
65+
let fields = struct_field_idents(derive_input.data);
6966
let fields_to_stringify = fields.iter().map(|field| field.unraw());
7067
let number_of_fields = fields.len();
7168

@@ -138,7 +135,7 @@ fn filtered_struct_fields<T>(
138135
}
139136
}
140137

141-
fn struct_fields_idents(derive_input_data: Data) -> Vec<Ident> {
138+
fn struct_field_idents(derive_input_data: Data) -> Vec<Ident> {
142139
filtered_struct_fields(derive_input_data, |field| Some(field.ident.unwrap()))
143140
}
144141

0 commit comments

Comments
 (0)