@@ -228,7 +228,7 @@ impl<'a> Struct<'a> {
228228 derives_vec. push ( derives:: PARTIALEQ ) ;
229229 }
230230
231- /* if !self.config.options.default_impl*/ {
231+ if !self . config . options . default_impl {
232232 derives_vec. push ( derives:: DEFAULT ) ;
233233 }
234234 }
@@ -299,7 +299,7 @@ impl<'a> Struct<'a> {
299299 . collect :: < Vec < String > > ( )
300300 . join ( " " ) ;
301301
302- let fields = self . fields ( ) ;
302+ let mut fields = self . fields ( ) ;
303303
304304 if fields. is_empty ( ) {
305305 self . has_fields = Some ( false ) ;
@@ -332,18 +332,18 @@ impl<'a> Struct<'a> {
332332 } ;
333333
334334 let mut lines = Vec :: with_capacity ( fields. len ( ) ) ;
335- for mut f in fields. into_iter ( ) {
335+ for f in fields. iter_mut ( ) {
336336 let field_name = & f. name ;
337337
338338 if f. base_type == "String" {
339339 f. base_type = match self . ty {
340- StructType :: Read => f. base_type ,
340+ StructType :: Read => f. base_type . clone ( ) ,
341341 StructType :: Update => self . opts . get_update_str_type ( ) . as_str ( ) . to_string ( ) ,
342342 StructType :: Create => self . opts . get_create_str_type ( ) . as_str ( ) . to_string ( ) ,
343343 }
344344 } else if f. base_type == "Vec<u8>" {
345345 f. base_type = match self . ty {
346- StructType :: Read => f. base_type ,
346+ StructType :: Read => f. base_type . clone ( ) ,
347347 StructType :: Update => self . opts . get_update_bytes_type ( ) . as_str ( ) . to_string ( ) ,
348348 StructType :: Create => self . opts . get_create_bytes_type ( ) . as_str ( ) . to_string ( ) ,
349349 }
@@ -382,7 +382,7 @@ impl<'a> Struct<'a> {
382382 ) ,
383383 } ;
384384
385- let struct_code = formatdoc ! (
385+ let mut struct_code = formatdoc ! (
386386 r#"
387387 {doccomment}
388388 {tsync_attr}{derive_attr}
@@ -409,6 +409,14 @@ impl<'a> Struct<'a> {
409409 lines = lines. join( "\n " ) ,
410410 ) ;
411411
412+ if self . config . options . default_impl {
413+ struct_code. push ( '\n' ) ;
414+ struct_code. push_str ( & build_default_impl_fn (
415+ & ty. format ( & table. struct_name ) ,
416+ & fields,
417+ ) ) ;
418+ }
419+
412420 self . has_fields = Some ( true ) ;
413421 self . rendered_code = Some ( struct_code) ;
414422 }
@@ -784,51 +792,40 @@ fn default_for_type(typ: &str) -> &'static str {
784792 }
785793}
786794
787- struct NameTypNullable < ' a > {
788- name : String ,
789- typ : & ' a str ,
790- nullable : bool ,
791- }
792-
793795/// Generate default (insides of the `impl Default for StructName { fn default() -> Self {} }`)
794- fn build_default_impl_fn < ' a > (
795- struct_name : & str ,
796- column_name_type_nullable : impl Iterator < Item = NameTypNullable < ' a > > ,
797- ) -> String {
798- let fields_to_defaults = column_name_type_nullable
796+ fn build_default_impl_fn < ' a > ( struct_name : & str , fields : & [ StructField ] ) -> String {
797+ let fields: Vec < String > = fields
798+ . iter ( )
799799 . map ( |name_typ_nullable| {
800800 format ! (
801- " {name}: {typ_default}" ,
801+ "{name}: {typ_default}, " ,
802802 name = name_typ_nullable. name,
803- typ_default = if name_typ_nullable. nullable {
803+ typ_default = if name_typ_nullable. is_optional {
804804 "None"
805805 } else {
806- default_for_type( name_typ_nullable. typ )
806+ default_for_type( & name_typ_nullable. base_type )
807807 }
808808 )
809809 } )
810- . collect :: < Vec < String > > ( )
811- . join ( ",\n " ) ;
810+ . collect ( ) ;
812811 formatdoc ! (
813- r#"impl Default for {struct_name} {{
814- fn default() -> Self {{
815- Self {{
816- {fields_to_defaults}
817- }}
818- }}
819- }}"#
812+ r#"
813+ impl Default for {struct_name} {{
814+ fn default() -> Self {{
815+ Self {{
816+ {fields}
817+ }}
818+ }}
819+ }}
820+ "# ,
821+ fields = fields. join( "\n " )
820822 )
821823}
822824
823825/// Generate a full file for a given diesel table
824826pub fn generate_for_table ( table : & ParsedTableMacro , config : & GenerationConfig ) -> String {
825827 // early to ensure the table options are set for the current table
826- let struct_name = & table. struct_name ;
827828 let table_options = config. table ( & table. name . to_string ( ) ) ;
828- let generated_columns = table_options. get_autogenerated_columns ( ) ;
829- let not_generated = |col : & & ParsedColumnMacro | -> bool {
830- !generated_columns. contains ( & col. column_name . as_str ( ) )
831- } ;
832829
833830 let mut ret_buffer = format ! ( "{FILE_SIGNATURE}\n \n " ) ;
834831
@@ -844,51 +841,13 @@ pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -
844841 if create_struct. has_code ( ) {
845842 ret_buffer. push ( '\n' ) ;
846843 ret_buffer. push_str ( create_struct. code ( ) ) ;
847- if config. options . default_impl {
848- ret_buffer. push ( '\n' ) ;
849- ret_buffer. push_str (
850- build_default_impl_fn (
851- & StructType :: format ( & StructType :: Create , & struct_name) ,
852- create_struct
853- . table
854- . columns
855- . iter ( )
856- . filter ( not_generated)
857- . map ( |col| NameTypNullable {
858- name : col. column_name . to_string ( ) ,
859- typ : col. ty . as_str ( ) ,
860- nullable : col. is_nullable ,
861- } ) ,
862- )
863- . as_str ( ) ,
864- ) ;
865- }
866844 }
867845
868846 let update_struct = Struct :: new ( StructType :: Update , table, config) ;
869847
870848 if update_struct. has_code ( ) {
871849 ret_buffer. push ( '\n' ) ;
872850 ret_buffer. push_str ( update_struct. code ( ) ) ;
873- /*if config.options.default_impl {
874- ret_buffer.push('\n');
875- ret_buffer.push_str(
876- build_default_impl_fn(
877- &StructType::format(&StructType::Update, &struct_name),
878- update_struct
879- .table
880- .columns
881- .iter()
882- .filter(not_generated)
883- .map(|col| NameTypNullable {
884- name: col.column_name.to_string(),
885- typ: col.ty.as_str(),
886- nullable: col.is_nullable,
887- }),
888- )
889- .as_str(),
890- );
891- }*/
892851 }
893852
894853 // third, push functions - if enabled
@@ -897,20 +856,5 @@ pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -
897856 ret_buffer. push_str ( build_table_fns ( table, config, create_struct, update_struct) . as_str ( ) ) ;
898857 }
899858
900- if config. options . default_impl {
901- ret_buffer. push ( '\n' ) ;
902- ret_buffer. push_str (
903- build_default_impl_fn (
904- & struct_name,
905- table. columns . iter ( ) . map ( |col| NameTypNullable {
906- name : col. name . to_string ( ) . to_owned ( ) ,
907- typ : col. ty . as_str ( ) ,
908- nullable : col. is_nullable ,
909- } ) ,
910- )
911- . as_str ( ) ,
912- ) ;
913- }
914-
915859 ret_buffer
916860}
0 commit comments