use crate::{SqlColdBackend, SqlColdError};
use signet_cold::ColdConnect;
#[derive(Debug, thiserror::Error)]
pub enum SqlConnectorError {
#[error("missing environment variable: {0}")]
MissingEnvVar(&'static str),
#[error("cold storage initialization failed: {0}")]
ColdInit(#[from] SqlColdError),
}
#[cfg(any(feature = "sqlite", feature = "postgres"))]
#[derive(Debug, Clone)]
pub struct SqlConnector {
url: String,
}
#[cfg(any(feature = "sqlite", feature = "postgres"))]
impl SqlConnector {
pub fn new(url: impl Into<String>) -> Self {
Self { url: url.into() }
}
pub fn url(&self) -> &str {
&self.url
}
pub fn from_env(env_var: &'static str) -> Result<Self, SqlConnectorError> {
let url = std::env::var(env_var).map_err(|_| SqlConnectorError::MissingEnvVar(env_var))?;
Ok(Self::new(url))
}
}
#[cfg(any(feature = "sqlite", feature = "postgres"))]
impl ColdConnect for SqlConnector {
type Cold = SqlColdBackend;
type Error = SqlColdError;
fn connect(&self) -> impl std::future::Future<Output = Result<Self::Cold, Self::Error>> + Send {
let url = self.url.clone();
async move { SqlColdBackend::connect(&url).await }
}
}