1- use crate :: parser:: { ParsedColumnMacro , ParsedTableMacro , FILE_SIGNATURE } ;
2- use crate :: { get_table_module_name, GenerationConfig , TableOptions } ;
31use heck:: ToPascalCase ;
42use indoc:: formatdoc;
5- use std :: borrow :: Cow ;
6- use std :: iter :: Map ;
7- use std :: slice :: Iter ;
3+
4+ use crate :: parser :: { ParsedColumnMacro , ParsedTableMacro , FILE_SIGNATURE } ;
5+ use crate :: { get_table_module_name , GenerationConfig , TableOptions } ;
86
97#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
108enum StructType {
@@ -87,7 +85,7 @@ pub struct StructField {
8785
8886impl StructField {
8987 /// Assemble the current options into a rust type, like `base_type: String, is_optional: true` to `Option<String>`
90- pub fn to_rust_type ( & self ) -> Cow < str > {
88+ pub fn to_rust_type ( & self ) -> std :: borrow :: Cow < str > {
9189 let mut rust_type = self . base_type . clone ( ) ;
9290
9391 // order matters!
@@ -773,18 +771,24 @@ fn default_for_type(typ: &str) -> &'static str {
773771 "bool" => "false" ,
774772 "String" => "String::new()" ,
775773 "&str" | "&'static str" => "\" \" " ,
776- _ => "Default::default()" ,
774+ "Cow<str>" => "Cow::Owned(String::new())" ,
775+ _ => {
776+ if typ. starts_with ( "Option<" ) {
777+ "None"
778+ } else {
779+ "Default::default()"
780+ }
781+ }
777782 }
778783}
779784
780785/// Generate default (insides of the `impl Default for StructName { fn default() -> Self {} }`)
781- fn build_default_impl_fn ( struct_name : & str , columns : & Vec < ParsedColumnMacro > ) -> String {
782- let column_name_type_nullable: Map <
783- Iter < ParsedColumnMacro > ,
784- fn ( & ParsedColumnMacro ) -> ( String , & str , bool ) ,
785- > = columns
786- . iter ( )
787- . map ( |col| ( col. name . to_string ( ) , col. ty . as_str ( ) , col. is_nullable ) ) ;
786+ fn build_default_impl_fn < ' a > (
787+ struct_name : & str ,
788+ columns : impl Iterator < Item = & ' a ParsedColumnMacro > ,
789+ ) -> String {
790+ let column_name_type_nullable =
791+ columns. map ( |col| ( col. name . to_string ( ) , col. ty . as_str ( ) , col. is_nullable ) ) ;
788792 let fields_to_defaults = column_name_type_nullable
789793 . map ( |( name, typ, nullable) | {
790794 format ! (
@@ -810,6 +814,7 @@ pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -
810814 // early to ensure the table options are set for the current table
811815 let struct_name = table. struct_name . to_string ( ) ;
812816 let table_options = config. table ( & table. name . to_string ( ) ) ;
817+ let generated_columns = table_options. get_autogenerated_columns ( ) ;
813818
814819 let mut ret_buffer = format ! ( "{FILE_SIGNATURE}\n \n " ) ;
815820
@@ -822,6 +827,10 @@ pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -
822827
823828 let create_struct = Struct :: new ( StructType :: Create , table, config) ;
824829
830+ let not_generated = |col : & & ParsedColumnMacro | -> bool {
831+ !generated_columns. contains ( & col. column_name . as_str ( ) )
832+ } ;
833+
825834 if create_struct. has_code ( ) {
826835 ret_buffer. push ( '\n' ) ;
827836 ret_buffer. push_str ( create_struct. code ( ) ) ;
@@ -830,7 +839,7 @@ pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -
830839 ret_buffer. push_str (
831840 build_default_impl_fn (
832841 & StructType :: format ( & StructType :: Create , & struct_name) ,
833- & create_struct. table . columns ,
842+ create_struct. table . columns . iter ( ) . filter ( not_generated ) ,
834843 )
835844 . as_str ( ) ,
836845 ) ;
@@ -853,7 +862,10 @@ pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -
853862
854863 if config. options . default_impl {
855864 ret_buffer. push ( '\n' ) ;
856- ret_buffer. push_str ( build_default_impl_fn ( & struct_name, & table. columns ) . as_str ( ) ) ;
865+ ret_buffer. push_str (
866+ build_default_impl_fn ( & struct_name, table. columns . iter ( ) . filter ( not_generated) )
867+ . as_str ( ) ,
868+ ) ;
857869 ret_buffer. push ( '\n' ) ;
858870 }
859871
0 commit comments