use std::{error::Error, fmt, future::Future};
pub trait ManageConnection: Send + Sync + 'static {
type Connection: 'static;
type Error: Error + 'static;
fn connect(&self) -> impl Future<Output = Result<Self::Connection, Self::Error>>;
fn is_valid(
&self,
conn: &mut Self::Connection,
) -> impl Future<Output = Result<(), Self::Error>>;
fn has_broken(&self, conn: &mut Self::Connection) -> bool;
}
#[derive(Debug)]
pub enum RunError<E> {
User(E),
TimedOut,
Closed,
}
impl<E: fmt::Display> fmt::Display for RunError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RunError::User(e) => write!(f, "{e}"),
RunError::TimedOut => f.write_str("timed out waiting for a pooled connection"),
RunError::Closed => f.write_str("the connection pool is closed"),
}
}
}
impl<E: Error + 'static> Error for RunError<E> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
RunError::User(e) => Some(e),
RunError::TimedOut | RunError::Closed => None,
}
}
}