use std::convert::Infallible;
use crate::{CreatePoolError, Manager, Pool, PoolBuilder, PoolConfig, Runtime};
#[derive(Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Config {
pub url: Option<String>,
pub pool: Option<PoolConfig>,
#[cfg_attr(feature = "serde", serde(skip))]
pub connection_properties: lapin::ConnectionProperties,
}
pub(crate) struct ConnProps<'a>(pub(crate) &'a lapin::ConnectionProperties);
impl std::fmt::Debug for ConnProps<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ConnectionProperties")
.field("locale", &self.0.locale)
.field("client_properties", &self.0.client_properties)
.finish_non_exhaustive()
}
}
impl std::fmt::Debug for Config {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Config")
.field("url", &self.url)
.field("pool", &self.pool)
.field(
"connection_properties",
&ConnProps(&self.connection_properties),
)
.finish()
}
}
impl Config {
pub fn create_pool(&self, runtime: Option<Runtime>) -> Result<Pool, CreatePoolError> {
self.builder(runtime)
.build()
.map_err(CreatePoolError::Build)
}
pub fn builder(&self, runtime: Option<Runtime>) -> PoolBuilder {
let url = self.get_url().to_string();
let pool_config = self.get_pool_config();
let conn_props = self.connection_properties.clone();
let conn_props = match runtime {
None => conn_props,
#[cfg(feature = "rt_tokio_1")]
Some(Runtime::Tokio1) => {
#[cfg(not(windows))]
let conn_props = conn_props.with_reactor(tokio_reactor_trait::Tokio::current());
conn_props.with_executor(tokio_executor_trait::Tokio::current())
}
#[cfg(feature = "rt_async-std_1")]
Some(Runtime::AsyncStd1) => conn_props
.with_executor(async_executor_trait::AsyncStd)
.with_reactor(async_reactor_trait::AsyncIo),
#[allow(unreachable_patterns)]
_ => unreachable!(),
};
let mut builder = Pool::builder(Manager::new(url, conn_props)).config(pool_config);
if let Some(runtime) = runtime {
builder = builder.runtime(runtime)
}
builder
}
pub fn get_url(&self) -> &str {
self.url.as_deref().unwrap_or("amqp://127.0.0.1:5672/%2f")
}
#[must_use]
pub fn get_pool_config(&self) -> PoolConfig {
self.pool.unwrap_or_default()
}
}
pub type ConfigError = Infallible;