use super::DbError;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Backend {
#[cfg(feature = "model-sqlite")]
Sqlite,
#[cfg(feature = "model-postgres")]
Postgres,
#[cfg(feature = "model-mysql")]
MySql,
}
impl Backend {
pub fn parse(s: &str) -> Result<Backend, DbError> {
match s.to_ascii_lowercase().as_str() {
#[cfg(feature = "model-sqlite")]
"sqlite" => Ok(Backend::Sqlite),
#[cfg(feature = "model-postgres")]
"postgres" | "postgresql" => Ok(Backend::Postgres),
#[cfg(feature = "model-mysql")]
"mysql" => Ok(Backend::MySql),
other => Err(DbError::new(format!(
"unknown or not-compiled-in database backend '{}' (compiled in: {})",
other,
Backend::compiled_in_names(),
))),
}
}
pub fn name(&self) -> &'static str {
match self {
#[cfg(feature = "model-sqlite")]
Backend::Sqlite => "sqlite",
#[cfg(feature = "model-postgres")]
Backend::Postgres => "postgres",
#[cfg(feature = "model-mysql")]
Backend::MySql => "mysql",
}
}
fn compiled_in_names() -> String {
let mut names: Vec<&str> = Vec::new();
#[cfg(feature = "model-sqlite")]
names.push("sqlite");
#[cfg(feature = "model-postgres")]
names.push("postgres");
#[cfg(feature = "model-mysql")]
names.push("mysql");
names.join(", ")
}
pub(crate) fn unambiguous_default() -> Option<Backend> {
let candidates: &[Backend] = &[
#[cfg(feature = "model-sqlite")]
Backend::Sqlite,
#[cfg(feature = "model-postgres")]
Backend::Postgres,
#[cfg(feature = "model-mysql")]
Backend::MySql,
];
if candidates.len() == 1 { Some(candidates[0]) } else { None }
}
}