Skip to content

Commit 62d0f1b

Browse files
committed
fix: throw error if "connection_type" is a empty string
similar to "diesel_backend"
1 parent 1fe5b10 commit 62d0f1b

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/global.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -482,21 +482,39 @@ impl<'a> GenerationConfig<'a> {
482482
}
483483
}
484484

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

489-
const VALID_BACKENDS: [&str; 3] = [
490-
"diesel::pg::Pg",
491-
"diesel::sqlite::Sqlite",
492-
"diesel::mysql::Mysql",
488+
// 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
489+
490+
#[cfg(feature = "advanced-queries")]
491+
{
492+
const VALID_BACKENDS: [&str; 3] = [
493+
"diesel::pg::Pg",
494+
"diesel::sqlite::Sqlite",
495+
"diesel::mysql::Mysql",
496+
];
497+
498+
if config.diesel_backend.is_empty() {
499+
return Err(Error::new(ErrorEnum::InvalidGenerationConfig(format!(
500+
"Invalid diesel_backend '{}', please use one of the following: {:?}; or, a custom diesel backend type (a struct which implements `diesel::backend::Backend`).",
501+
&config.diesel_backend,
502+
VALID_BACKENDS.join(", ")
503+
))));
504+
}
505+
}
506+
507+
const KNOWN_CONNECTIONS: [&str; 4] = [
508+
"diesel::pg::PgConnection",
509+
"diesel::sqlite::SqliteConnection",
510+
"diesel::mysql::MysqlConnection",
511+
"diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<_>>",
493512
];
494513

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

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ pub fn generate_files(
112112
output_models_dir: &Path,
113113
config: GenerationConfig,
114114
) -> Result<Vec<FileChange>> {
115-
#[cfg(feature = "advanced-queries")]
116115
global::validate_config(&config)?;
117116

118117
let generated = generate_code(

0 commit comments

Comments
 (0)