use std::error::Error;
#[derive(Debug, Default)]
#[cfg_attr(
feature = "sqlx-toml",
derive(serde::Deserialize),
serde(default, rename_all = "kebab-case", deny_unknown_fields)
)]
pub struct Config {
pub mysql: MySqlConfig,
pub postgres: PgConfig,
pub sqlite: SqliteConfig,
pub external: ExternalDriverConfig,
}
#[derive(Debug, Default)]
#[cfg_attr(
feature = "sqlx-toml",
derive(serde::Deserialize),
serde(default, rename_all = "kebab-case", deny_unknown_fields)
)]
pub struct MySqlConfig {
}
#[derive(Debug, Default)]
#[cfg_attr(
feature = "sqlx-toml",
derive(serde::Deserialize),
serde(default, rename_all = "kebab-case", deny_unknown_fields)
)]
pub struct PgConfig {
}
#[derive(Debug, Default)]
#[cfg_attr(
feature = "sqlx-toml",
derive(serde::Deserialize),
serde(default, rename_all = "kebab-case", deny_unknown_fields)
)]
pub struct SqliteConfig {
pub unsafe_load_extensions: Vec<String>,
}
#[derive(Debug, Default)]
#[cfg_attr(feature = "sqlx-toml", derive(serde::Deserialize), serde(transparent))]
pub struct ExternalDriverConfig {
#[cfg(feature = "sqlx-toml")]
by_name: std::collections::BTreeMap<String, toml::Table>,
}
pub type TryParseError = Box<dyn Error + Send + Sync + 'static>;
impl ExternalDriverConfig {
#[cfg(feature = "sqlx-toml")]
pub fn try_parse<T: serde::de::DeserializeOwned>(
&self,
name: &str,
) -> Result<Option<T>, TryParseError> {
let Some(config) = self.by_name.get(name) else {
return Ok(None);
};
Ok(Some(config.clone().try_into()?))
}
#[cfg(not(feature = "sqlx-toml"))]
pub fn try_parse<T>(&self, _name: &str) -> Result<Option<T>, TryParseError> {
Ok(None)
}
}