Skip to content

Commit b69ff2a

Browse files
committed
fix: use consistent module and directory paths for tables
fixes mismatch between a tables imports and the directory structure
1 parent ce8d41b commit b69ff2a

File tree

19 files changed

+27
-15
lines changed

19 files changed

+27
-15
lines changed

src/code.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use heck::{ToPascalCase, ToSnakeCase};
1+
use heck::ToPascalCase;
22
use indoc::formatdoc;
33
use std::borrow::Cow;
44

55
use crate::parser::{ParsedColumnMacro, ParsedTableMacro, FILE_SIGNATURE};
6-
use crate::{GenerationConfig, TableOptions};
6+
use crate::{get_table_module_name, GenerationConfig, TableOptions};
77

88
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
99
enum StructType {
@@ -606,7 +606,7 @@ fn build_imports(table: &ParsedTableMacro, config: &GenerationConfig) -> String
606606
imports_vec.extend(table.foreign_keys.iter().map(|fk| {
607607
format!(
608608
"use {model_path}{foreign_table_name_model}::{singular_struct_name};",
609-
foreign_table_name_model = fk.0.to_string().to_snake_case().to_lowercase(),
609+
foreign_table_name_model = get_table_module_name(&fk.0.to_string()),
610610
singular_struct_name = fk.0.to_string().to_pascal_case(),
611611
model_path = config.model_path
612612
)

src/lib.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod parser;
1818
use error::IOErrorToError;
1919
pub use error::{Error, Result};
2020
use file::MarkedFile;
21+
use heck::ToSnakeCase;
2122
use parser::ParsedTableMacro;
2223
pub use parser::FILE_SIGNATURE;
2324
use std::collections::HashMap;
@@ -350,6 +351,14 @@ impl From<&MarkedFile> for FileChange {
350351
}
351352
}
352353

354+
/// Helper function for consistent table module name generation
355+
/// this is used for the rust module path name and for the filename
356+
///
357+
/// input: "tableA", output -> "table_a"
358+
fn get_table_module_name(table_name: &str) -> String {
359+
table_name.to_snake_case().to_lowercase()
360+
}
361+
353362
/// Generate all Models for a given diesel schema file
354363
///
355364
/// Models are saved to disk
@@ -412,11 +421,12 @@ pub fn generate_files(
412421
return Err(Error::other("Cannot have a table named \"common\" while having option \"once_common_structs\" enabled"));
413422
}
414423
let table_name = table.name.to_string();
424+
let table_filename = get_table_module_name(&table_name);
415425
let table_config = config.table(&table_name);
416426
let table_dir = if table_config.single_model_file {
417427
output_models_dir.to_owned()
418428
} else {
419-
output_models_dir.join(&table_name)
429+
output_models_dir.join(&table_filename)
420430
};
421431

422432
if !table_dir.exists() {
@@ -451,11 +461,12 @@ pub fn generate_files(
451461
file_changes.push(FileChange::from(&table_mod_rs));
452462
}
453463

454-
mod_rs.ensure_mod_stmt(&table.name.to_string());
464+
mod_rs.ensure_mod_stmt(&table_filename);
455465
}
456466

457467
// pass 2: delete code for removed tables
458468
for item in std::fs::read_dir(output_models_dir).attach_path_err(output_models_dir)? {
469+
// TODO: this does not work with "single-model-file"
459470
let item = item.attach_path_err(output_models_dir)?;
460471

461472
// check if item is a directory
@@ -482,9 +493,7 @@ pub fn generate_files(
482493
item.path()
483494
)))?;
484495
let found = generated.iter().find(|g| {
485-
g.name
486-
.to_string()
487-
.eq_ignore_ascii_case(associated_table_name)
496+
get_table_module_name(&g.name.to_string()).eq_ignore_ascii_case(associated_table_name)
488497
});
489498
if found.is_some() {
490499
continue;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
pub mod tableA;
2-
pub mod tableB;
1+
pub mod table_a;
2+
pub mod table_b;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
pub mod tableA;
2-
pub mod tableB;
1+
pub mod table_a;
2+
pub mod table_b;

0 commit comments

Comments
 (0)