Skip to content

Commit

Permalink
fix: throw error if "connection_type" is a empty string
Browse files Browse the repository at this point in the history
similar to "diesel_backend"
  • Loading branch information
hasezoey committed Aug 21, 2024
1 parent 3795b52 commit 6b451c2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
36 changes: 27 additions & 9 deletions src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,21 +482,39 @@ impl<'a> GenerationConfig<'a> {
}
}

#[cfg(feature = "advanced-queries")]
pub fn validate_config(config: &GenerationConfig) -> crate::Result<()> {
use crate::error::{Error, ErrorEnum};

const VALID_BACKENDS: [&str; 3] = [
"diesel::pg::Pg",
"diesel::sqlite::Sqlite",
"diesel::mysql::Mysql",
// NOTE: the following arrays should likely be joined at compile-time instead of at runtime, but rust does not provide such a thing in std

#[cfg(feature = "advanced-queries")]
{
const VALID_BACKENDS: [&str; 3] = [
"diesel::pg::Pg",
"diesel::sqlite::Sqlite",
"diesel::mysql::Mysql",
];

if config.diesel_backend.is_empty() {
return Err(Error::new(ErrorEnum::InvalidGenerationConfig(format!(
"Invalid diesel_backend '{}', please use one of the following: {:?}; or, a custom diesel backend type (a struct which implements `diesel::backend::Backend`).",
&config.diesel_backend,
VALID_BACKENDS.join(", ")
))));
}
}

const KNOWN_CONNECTIONS: [&str; 4] = [
"diesel::pg::PgConnection",
"diesel::sqlite::SqliteConnection",
"diesel::mysql::MysqlConnection",
"diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<_>>",
];

if config.diesel_backend.is_empty() {
if config.connection_type.is_empty() {
return Err(Error::new(ErrorEnum::InvalidGenerationConfig(format!(
"Invalid diesel_backend '{}', please use one of the following: {:?}; or, a custom diesel backend type (a struct which implements `diesel::backend::Backend`).",
&config.diesel_backend,
VALID_BACKENDS.join(", ")
"option \"connection_type\" cannot be empty. Known Connections types are:\n[{}]",
KNOWN_CONNECTIONS.join(", ")
))));
}

Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ pub fn generate_files(
output_models_dir: &Path,
config: GenerationConfig,
) -> Result<Vec<FileChange>> {
#[cfg(feature = "advanced-queries")]
global::validate_config(&config)?;

let generated = generate_code(
Expand Down

0 comments on commit 6b451c2

Please sign in to comment.