use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DatabaseEngine {
PostgreSQL,
MySQL,
MariaDB,
SQLite,
}
impl DatabaseEngine {
pub fn detect_from_url(url: &str) -> Result<Self, String> {
if url.starts_with("postgres://") || url.starts_with("postgresql://") {
Ok(DatabaseEngine::PostgreSQL)
} else if url.starts_with("mysql://") {
Ok(DatabaseEngine::MySQL)
} else if url.starts_with("mariadb://") {
Ok(DatabaseEngine::MariaDB)
} else if url.starts_with("sqlite://") {
Ok(DatabaseEngine::SQLite)
} else {
Err(format!("Unsupported database URL: {}", url))
}
}
pub fn name(&self) -> &'static str {
match self {
DatabaseEngine::PostgreSQL => "PostgreSQL",
DatabaseEngine::MySQL => "MySQL",
DatabaseEngine::MariaDB => "MariaDB",
DatabaseEngine::SQLite => "SQLite",
}
}
}
pub(super) fn verify_database_driver(engine: &DatabaseEngine) -> Result<(), String> {
match engine {
DatabaseEngine::PostgreSQL => {
#[cfg(not(feature = "postgres"))]
return Err("PostgreSQL driver not enabled.\n\n\
To fix this, add the 'postgres' feature to runique in your Cargo.toml:\n\n\
[dependencies]\n\
runique = { version = \"0.1\", features = [\"postgres\"] }\n\n\
Or enable all databases:\n\
runique = { version = \"0.1\", features = [\"all-databases\"] }"
.to_string());
#[cfg(feature = "postgres")]
Ok(())
}
DatabaseEngine::MySQL => {
#[cfg(not(feature = "mysql"))]
return Err("MySQL driver not enabled.\n\n\
To fix this, add the 'mysql' feature to runique in your Cargo.toml:\n\n\
[dependencies]\n\
runique = { version = \"0.1\", features = [\"mysql\"] }\n\n\
Or enable all databases:\n\
runique = { version = \"0.1\", features = [\"all-databases\"] }"
.to_string());
#[cfg(feature = "mysql")]
Ok(())
}
DatabaseEngine::MariaDB => {
#[cfg(not(feature = "mariadb"))]
return Err("MariaDB driver not enabled.\n\n\
To fix this, add the 'mariadb' feature to runique in your Cargo.toml:\n\n\
[dependencies]\n\
runique = { version = \"0.1\", features = [\"mariadb\"] }\n\n\
Note: MariaDB uses the MySQL driver.\n\n\
Or enable all databases:\n\
runique = { version = \"0.1\", features = [\"all-databases\"] }"
.to_string());
#[cfg(feature = "mariadb")]
Ok(())
}
DatabaseEngine::SQLite => {
#[cfg(not(feature = "sqlite"))]
return Err(
"To fix this, add the 'sqlite' feature to runique in your Cargo.toml:\n\n\
[dependencies]\n\
runique = { version = \"1.xx\", features = [\"sqlite\"] }
Note: Sqlite uses the Sqlite driver.\n\n\
Or enable all databases:\n\
runique = { version = \"0.1\", features = [\"all-databases\"]"
.to_string(),
);
#[cfg(feature = "sqlite")]
Ok(())
}
}
}