Skip to content

Commit 867c512

Browse files
committed
[src/code.rs] Optimise string allocations ; avoid impl Default on update ; use StructType::format
1 parent 797b323 commit 867c512

File tree

1 file changed

+9
-26
lines changed

1 file changed

+9
-26
lines changed

src/code.rs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,8 @@ fn build_imports(table: &ParsedTableMacro, config: &GenerationConfig) -> String
764764
}
765765

766766
/// Get default for type
767-
fn default_for_type(typ: String) -> &'static str {
768-
match typ.as_str() {
767+
fn default_for_type(typ: &str) -> &'static str {
768+
match typ {
769769
"i8" | "u8" | "i16" | "u16" | "i32" | "u32" | "i64" | "u64" | "i128" | "u128" | "isize"
770770
| "usize" => "0",
771771
"f32" | "f64" => "0.0",
@@ -779,19 +779,12 @@ fn default_for_type(typ: String) -> &'static str {
779779

780780
/// Generate default (insides of the `impl Default for StructName { fn default() -> Self {} }`)
781781
fn build_default_impl_fn(struct_name: &str, columns: &Vec<ParsedColumnMacro>) -> String {
782-
let mut buffer = String::with_capacity(struct_name.len() + columns.len() * 4);
783-
buffer.push_str(&format!(
784-
"impl Default for {struct_name} {{\n fn default() -> Self {{\n",
785-
struct_name = struct_name
786-
));
787782
let column_name_type_nullable: Map<
788783
Iter<ParsedColumnMacro>,
789-
fn(&ParsedColumnMacro) -> (String, String, bool),
784+
fn(&ParsedColumnMacro) -> (String, &str, bool),
790785
> = columns
791786
.iter()
792-
.map(|col| (col.name.to_string(), col.ty.to_string(), col.is_nullable));
793-
794-
buffer.push_str(" Self {\n");
787+
.map(|col| (col.name.to_string(), col.ty.as_str(), col.is_nullable));
795788
let fields_to_defaults = column_name_type_nullable
796789
.map(|(name, typ, nullable)| {
797790
format!(
@@ -806,9 +799,10 @@ fn build_default_impl_fn(struct_name: &str, columns: &Vec<ParsedColumnMacro>) ->
806799
})
807800
.collect::<Vec<String>>()
808801
.join(",\n");
809-
buffer.push_str(fields_to_defaults.as_str());
810-
buffer.push_str("\n }\n }\n}");
811-
buffer
802+
format!(
803+
"impl Default for {struct_name} {{\n fn default() -> Self {{\n Self {{\n{f2d}\n }}\n }}\n}}",
804+
struct_name = struct_name, f2d=fields_to_defaults.as_str()
805+
)
812806
}
813807

814808
/// Generate a full file for a given diesel table
@@ -835,7 +829,7 @@ pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -
835829
ret_buffer.push('\n');
836830
ret_buffer.push_str(
837831
build_default_impl_fn(
838-
&format!("Create{struct_name}"),
832+
&StructType::format(&StructType::Create, &struct_name),
839833
&create_struct.table.columns,
840834
)
841835
.as_str(),
@@ -849,17 +843,6 @@ pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -
849843
if update_struct.has_code() {
850844
ret_buffer.push('\n');
851845
ret_buffer.push_str(update_struct.code());
852-
if config.options.default_impl {
853-
ret_buffer.push('\n');
854-
ret_buffer.push_str(
855-
build_default_impl_fn(
856-
&format!("Update{struct_name}"),
857-
&update_struct.table.columns,
858-
)
859-
.as_str(),
860-
);
861-
}
862-
ret_buffer.push('\n');
863846
}
864847

865848
// third, push functions - if enabled

0 commit comments

Comments
 (0)