@@ -782,22 +782,26 @@ fn default_for_type(typ: &str) -> &'static str {
782782 }
783783}
784784
785+ struct NameTypNullable < ' a > {
786+ name : String ,
787+ typ : & ' a str ,
788+ nullable : bool ,
789+ }
790+
785791/// Generate default (insides of the `impl Default for StructName { fn default() -> Self {} }`)
786792fn build_default_impl_fn < ' a > (
787793 struct_name : & str ,
788- columns : impl Iterator < Item = & ' a ParsedColumnMacro > ,
794+ column_name_type_nullable : impl Iterator < Item = NameTypNullable < ' a > > ,
789795) -> String {
790- let column_name_type_nullable =
791- columns. map ( |col| ( col. name . to_string ( ) , col. ty . as_str ( ) , col. is_nullable ) ) ;
792796 let fields_to_defaults = column_name_type_nullable
793- . map ( |( name , typ , nullable ) | {
797+ . map ( |name_typ_nullable | {
794798 format ! (
795799 " {name}: {typ_default}" ,
796- name = name,
797- typ_default = if nullable {
800+ name = name_typ_nullable . name,
801+ typ_default = if name_typ_nullable . nullable {
798802 "None"
799803 } else {
800- default_for_type( typ)
804+ default_for_type( name_typ_nullable . typ)
801805 }
802806 )
803807 } )
@@ -820,6 +824,9 @@ pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -
820824 let struct_name = & table. struct_name ;
821825 let table_options = config. table ( & table. name . to_string ( ) ) ;
822826 let generated_columns = table_options. get_autogenerated_columns ( ) ;
827+ let not_generated = |col : & & ParsedColumnMacro | -> bool {
828+ !generated_columns. contains ( & col. column_name . as_str ( ) )
829+ } ;
823830
824831 let mut ret_buffer = format ! ( "{FILE_SIGNATURE}\n \n " ) ;
825832
@@ -832,10 +839,6 @@ pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -
832839
833840 let create_struct = Struct :: new ( StructType :: Create , table, config) ;
834841
835- let not_generated = |col : & & ParsedColumnMacro | -> bool {
836- !generated_columns. contains ( & col. column_name . as_str ( ) )
837- } ;
838-
839842 if create_struct. has_code ( ) {
840843 ret_buffer. push ( '\n' ) ;
841844 ret_buffer. push_str ( create_struct. code ( ) ) ;
@@ -844,7 +847,11 @@ pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -
844847 ret_buffer. push_str (
845848 build_default_impl_fn (
846849 & StructType :: format ( & StructType :: Create , & struct_name) ,
847- create_struct. table . columns . iter ( ) . filter ( not_generated) ,
850+ create_struct. fields ( ) . iter ( ) . map ( |col| NameTypNullable {
851+ name : col. name . to_owned ( ) ,
852+ typ : col. base_type . as_str ( ) ,
853+ nullable : col. is_optional ,
854+ } ) ,
848855 )
849856 . as_str ( ) ,
850857 ) ;
@@ -867,8 +874,19 @@ pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -
867874 if config. options . default_impl {
868875 ret_buffer. push ( '\n' ) ;
869876 ret_buffer. push_str (
870- build_default_impl_fn ( & struct_name, table. columns . iter ( ) . filter ( not_generated) )
871- . as_str ( ) ,
877+ build_default_impl_fn (
878+ & struct_name,
879+ table
880+ . columns
881+ . iter ( )
882+ . filter ( not_generated)
883+ . map ( |col| NameTypNullable {
884+ name : col. name . to_string ( ) . to_owned ( ) ,
885+ typ : col. ty . as_str ( ) ,
886+ nullable : col. is_nullable ,
887+ } ) ,
888+ )
889+ . as_str ( ) ,
872890 ) ;
873891 }
874892
0 commit comments