use super::{Backend, DbError};
#[derive(Debug, Clone)]
pub struct DbConfig {
pub host: String,
pub port: u16,
pub user: String,
pub password: String,
pub database: String,
pub pool_size: u32,
pub backend: Backend,
}
impl DbConfig {
pub fn from_env() -> Result<Self, DbError> {
let backend = match std::env::var("RWS_DB_BACKEND") {
Ok(s) => Backend::parse(&s)?,
Err(_) => Backend::unambiguous_default().ok_or_else(|| {
DbError::new(
"RWS_DB_BACKEND must be set to \"sqlite\", \"postgres\", or \"mysql\" \
when more than one model-* feature is compiled in",
)
})?,
};
let host = std::env::var("RWS_DB_HOST").unwrap_or_else(|_| "localhost".into());
let port = std::env::var("RWS_DB_PORT")
.unwrap_or_else(|_| "5432".into())
.parse::<u16>()
.map_err(|e| DbError::new(format!("RWS_DB_PORT: {}", e)))?;
let user = std::env::var("RWS_DB_USER").unwrap_or_default();
let password = std::env::var("RWS_DB_PASSWORD").unwrap_or_default();
let database = std::env::var("RWS_DB_NAME")
.map_err(|_| DbError::new("RWS_DB_NAME environment variable is required"))?;
let pool_size = std::env::var("RWS_DB_POOL_SIZE")
.unwrap_or_else(|_| "10".into())
.parse::<u32>()
.map_err(|e| DbError::new(format!("RWS_DB_POOL_SIZE: {}", e)))?;
Ok(DbConfig { host, port, user, password, database, pool_size, backend })
}
pub(crate) fn to_url(&self) -> String {
match self.backend {
#[cfg(feature = "model-sqlite")]
Backend::Sqlite => format!("sqlite:{}", self.database),
#[cfg(feature = "model-postgres")]
Backend::Postgres => format!(
"postgres://{}:{}@{}:{}/{}",
self.user, self.password, self.host, self.port, self.database
),
#[cfg(feature = "model-mysql")]
Backend::MySql => format!(
"mysql://{}:{}@{}:{}/{}",
self.user, self.password, self.host, self.port, self.database
),
}
}
}