|
| 1 | +use std::path::{Path, PathBuf}; |
| 2 | + |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | + |
| 5 | +/// Supported naming cases. |
| 6 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 7 | +pub enum NameCase { |
| 8 | + Snake, |
| 9 | + Camel, |
| 10 | + Pascal, |
| 11 | +} |
| 12 | + |
| 13 | +impl NameCase { |
| 14 | + /// Returns true when snake case. |
| 15 | + pub fn is_snake(self) -> bool { |
| 16 | + matches!(self, NameCase::Snake) |
| 17 | + } |
| 18 | + |
| 19 | + /// Returns true when camel case. |
| 20 | + pub fn is_camel(self) -> bool { |
| 21 | + matches!(self, NameCase::Camel) |
| 22 | + } |
| 23 | + |
| 24 | + /// Returns true when pascal case. |
| 25 | + pub fn is_pascal(self) -> bool { |
| 26 | + matches!(self, NameCase::Pascal) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/// Top-level vespertide configuration. |
| 31 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 32 | +pub struct VespertideConfig { |
| 33 | + pub models_dir: PathBuf, |
| 34 | + pub migrations_dir: PathBuf, |
| 35 | + pub table_naming_case: NameCase, |
| 36 | + pub column_naming_case: NameCase, |
| 37 | +} |
| 38 | + |
| 39 | +impl Default for VespertideConfig { |
| 40 | + fn default() -> Self { |
| 41 | + Self { |
| 42 | + models_dir: PathBuf::from("models"), |
| 43 | + migrations_dir: PathBuf::from("migrations"), |
| 44 | + table_naming_case: NameCase::Snake, |
| 45 | + column_naming_case: NameCase::Snake, |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl VespertideConfig { |
| 51 | + pub fn new() -> Self { |
| 52 | + Self::default() |
| 53 | + } |
| 54 | + |
| 55 | + /// Path where model definitions are stored. |
| 56 | + pub fn models_dir(&self) -> &Path { |
| 57 | + &self.models_dir |
| 58 | + } |
| 59 | + |
| 60 | + /// Path where migrations are stored. |
| 61 | + pub fn migrations_dir(&self) -> &Path { |
| 62 | + &self.migrations_dir |
| 63 | + } |
| 64 | + |
| 65 | + /// Naming case for table names (flattened). |
| 66 | + pub fn table_case(&self) -> NameCase { |
| 67 | + self.table_naming_case |
| 68 | + } |
| 69 | + |
| 70 | + /// Naming case for column names (flattened). |
| 71 | + pub fn column_case(&self) -> NameCase { |
| 72 | + self.column_naming_case |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +#[cfg(test)] |
| 77 | +mod tests { |
| 78 | + use super::*; |
| 79 | + |
| 80 | + #[test] |
| 81 | + fn default_values_are_snake_and_standard_paths() { |
| 82 | + let cfg = VespertideConfig::default(); |
| 83 | + assert_eq!(cfg.models_dir, PathBuf::from("models")); |
| 84 | + assert_eq!(cfg.migrations_dir, PathBuf::from("migrations")); |
| 85 | + assert!(cfg.table_case().is_snake()); |
| 86 | + assert!(cfg.column_case().is_snake()); |
| 87 | + } |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn overrides_work_via_struct_update() { |
| 91 | + let cfg = VespertideConfig { |
| 92 | + models_dir: PathBuf::from("custom_models"), |
| 93 | + migrations_dir: PathBuf::from("custom_migrations"), |
| 94 | + table_naming_case: NameCase::Camel, |
| 95 | + column_naming_case: NameCase::Pascal, |
| 96 | + }; |
| 97 | + |
| 98 | + assert_eq!(cfg.models_dir(), Path::new("custom_models")); |
| 99 | + assert_eq!(cfg.migrations_dir(), Path::new("custom_migrations")); |
| 100 | + assert!(cfg.table_case().is_camel()); |
| 101 | + assert!(cfg.column_case().is_pascal()); |
| 102 | + } |
| 103 | +} |
0 commit comments