use std::fmt::{Display, Formatter};
#[derive(Debug)]
pub enum Error {
InvalidUrl(String),
PoolClosed,
PoolExhausted,
PoolTimedOut,
Unsupported(String),
#[cfg(feature = "mariadb")]
Mysql(quex_driver::mysql::Error),
#[cfg(feature = "postgres")]
Postgres(quex_driver::postgres::Error),
#[cfg(feature = "sqlite")]
Sqlite(quex_driver::sqlite::Error),
}
impl Error {
pub(crate) fn invalid_url(message: impl Into<String>) -> Self {
Self::InvalidUrl(message.into())
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidUrl(message) => write!(f, "invalid database URL: {message}"),
Self::PoolClosed => f.write_str("pool is closed"),
Self::PoolExhausted => f.write_str("pool is at capacity"),
Self::PoolTimedOut => f.write_str("timed out waiting for a pool connection"),
Self::Unsupported(message) => f.write_str(message),
#[cfg(feature = "mariadb")]
Self::Mysql(err) => Display::fmt(err, f),
#[cfg(feature = "postgres")]
Self::Postgres(err) => Display::fmt(err, f),
#[cfg(feature = "sqlite")]
Self::Sqlite(err) => Display::fmt(err, f),
}
}
}
impl std::error::Error for Error {}
#[cfg(feature = "mariadb")]
impl From<quex_driver::mysql::Error> for Error {
fn from(value: quex_driver::mysql::Error) -> Self {
Self::Mysql(value)
}
}
#[cfg(feature = "postgres")]
impl From<quex_driver::postgres::Error> for Error {
fn from(value: quex_driver::postgres::Error) -> Self {
Self::Postgres(value)
}
}
#[cfg(feature = "sqlite")]
impl From<quex_driver::sqlite::Error> for Error {
fn from(value: quex_driver::sqlite::Error) -> Self {
Self::Sqlite(value)
}
}
pub type Result<T> = std::result::Result<T, Error>;