Skip to content

Commit

Permalink
refactor(code): only push struct code, if it actually is not empty
Browse files Browse the repository at this point in the history
thereby reduces amount of new-lines
  • Loading branch information
hasezoey committed Oct 29, 2023
1 parent ccc2b6a commit 1e0b4d6
Show file tree
Hide file tree
Showing 15 changed files with 14 additions and 30 deletions.
20 changes: 14 additions & 6 deletions src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ impl<'a> Struct<'a> {
obj
}

pub fn has_code(&self) -> bool {
self.rendered_code.is_some()
}

/// Get the rendered code, or a empty string
pub fn code(&self) -> &str {
self.rendered_code.as_deref().unwrap_or_default()
Expand Down Expand Up @@ -618,12 +622,16 @@ pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -
let update_struct = Struct::new(StructType::Update, table, config);
let create_struct = Struct::new(StructType::Create, table, config);

let mut structs = String::new();
structs.push_str(read_struct.code());
structs.push('\n');
structs.push_str(create_struct.code());
structs.push('\n');
structs.push_str(update_struct.code());
let mut structs = String::from(read_struct.code());
if create_struct.has_code() {
structs.push('\n');
structs.push_str(create_struct.code());
}

if update_struct.has_code() {
structs.push('\n');
structs.push_str(update_struct.code());
}

let functions = if table_options.get_fns() {
build_table_fns(table, config, create_struct, update_struct)
Expand Down
1 change: 0 additions & 1 deletion test/autogenerated_all/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub struct Todos {
pub created_at: chrono::NaiveDateTime,
}


#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub struct CreateTableA {
}



#[derive(Debug, Serialize)]
pub struct PaginationResult<T> {
pub items: Vec<T>,
Expand Down
1 change: 0 additions & 1 deletion test/custom_model_path/models/tableA/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub struct CreateTableA {
}



#[derive(Debug, Serialize)]
pub struct PaginationResult<T> {
pub items: Vec<T>,
Expand Down
1 change: 0 additions & 1 deletion test/manual_primary_keys/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub struct CreateTodos {
}



#[derive(Debug, Serialize)]
pub struct PaginationResult<T> {
pub items: Vec<T>,
Expand Down
2 changes: 0 additions & 2 deletions test/once_common_structs/models/table1/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ pub struct Table1 {
}




impl Table1 {

pub fn create(db: &mut ConnectionType) -> QueryResult<Self> {
Expand Down
2 changes: 0 additions & 2 deletions test/once_common_structs/models/table2/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ pub struct Table2 {
}




impl Table2 {

pub fn create(db: &mut ConnectionType) -> QueryResult<Self> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub struct Table1 {
}




impl Table1 {

pub fn create(db: &mut ConnectionType) -> QueryResult<Self> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub struct Table2 {
}




impl Table2 {

pub fn create(db: &mut ConnectionType) -> QueryResult<Self> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub struct Table1 {
}




impl Table1 {

pub fn create(db: &mut ConnectionType) -> QueryResult<Self> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub struct Table2 {
}




impl Table2 {

pub fn create(db: &mut ConnectionType) -> QueryResult<Self> {
Expand Down
2 changes: 0 additions & 2 deletions test/once_connection_type/models/table1/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub struct Table1 {
}




#[derive(Debug, Serialize)]
pub struct PaginationResult<T> {
pub items: Vec<T>,
Expand Down
2 changes: 0 additions & 2 deletions test/once_connection_type/models/table2/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub struct Table2 {
}




#[derive(Debug, Serialize)]
pub struct PaginationResult<T> {
pub items: Vec<T>,
Expand Down
2 changes: 0 additions & 2 deletions test/single_model_file/models/table1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ pub struct Table1 {
}




#[derive(Debug, Serialize)]
pub struct PaginationResult<T> {
pub items: Vec<T>,
Expand Down
2 changes: 0 additions & 2 deletions test/single_model_file/models/table2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ pub struct Table2 {
}




#[derive(Debug, Serialize)]
pub struct PaginationResult<T> {
pub items: Vec<T>,
Expand Down

0 comments on commit 1e0b4d6

Please sign in to comment.