use std::fmt;
use futures_util::{FutureExt, future::BoxFuture};
use crate::AsyncConnection;
#[cfg(feature = "bb8")]
pub mod bb8;
#[cfg(feature = "deadpool")]
pub mod deadpool;
#[derive(thiserror::Error, Debug)]
pub enum PoolError {
#[error(transparent)]
ConnectionError(#[from] diesel::result::ConnectionError),
#[error(transparent)]
QueryError(#[from] diesel::result::Error),
#[error("Connection is broken")]
DisconnectionError,
}
pub type SetupCallback<C> =
Box<dyn Fn(&str) -> BoxFuture<diesel::ConnectionResult<C>> + Send + Sync>;
#[non_exhaustive]
pub struct ManagerConfig<C> {
pub custom_setup: SetupCallback<C>,
}
impl<C> Default for ManagerConfig<C>
where
C: AsyncConnection + 'static,
{
fn default() -> Self {
Self {
custom_setup: Box::new(|url| C::establish(url).boxed()),
}
}
}
pub struct AsyncDieselConnectionManager<C> {
connection_url: String,
manager_config: ManagerConfig<C>,
}
impl<C> fmt::Debug for AsyncDieselConnectionManager<C> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"AsyncDieselConnectionManager<{}>",
std::any::type_name::<C>()
)
}
}
impl<C> AsyncDieselConnectionManager<C>
where
C: AsyncConnection + 'static,
{
#[must_use]
pub fn new(connection_url: impl Into<String>) -> Self
where
C: AsyncConnection + 'static,
{
Self::new_with_config(connection_url, Default::default())
}
#[must_use]
pub fn new_with_config(
connection_url: impl Into<String>,
manager_config: ManagerConfig<C>,
) -> Self {
Self {
connection_url: connection_url.into(),
manager_config,
}
}
}