deadpool_amqprs/
config.rs

1use std::convert::Infallible;
2
3use amqprs::connection::OpenConnectionArguments;
4use deadpool::Runtime;
5
6use crate::{Manager, Pool, PoolBuilder, PoolConfig};
7
8/// Configuration object.
9///
10/// # Example
11///
12/// ```rs
13/// use deadpool_amqprs::Config;
14/// use amqprs::connection::OpenConnectionArguments;
15///
16/// let config = Config::new_with_con_args(OpenConnectionArguments::default())
17///
18/// let pool = config.create_pool();
19///
20/// // Do things with `pool`.
21/// ```
22#[derive(Clone, Default)]
23pub struct Config {
24    /// The [`OpenConnectionArguments`] passed to [`amqprs::connection::Connection::open`].
25    pub con_args: OpenConnectionArguments,
26    /// The [`PoolConfig`] passed to deadpool.
27    pub pool_config: Option<PoolConfig>,
28}
29
30impl Config {
31    /// Creates a new config with [`OpenConnectionArguments`] and optionally [`PoolConfig`].
32    #[must_use]
33    pub const fn new(con_args: OpenConnectionArguments, pool_config: Option<PoolConfig>) -> Self {
34        Self {
35            con_args,
36            pool_config,
37        }
38    }
39
40    /// Creates a new config with only [`OpenConnectionArguments`]
41    #[must_use]
42    pub const fn new_with_con_args(con_args: OpenConnectionArguments) -> Self {
43        Self {
44            con_args,
45            pool_config: None,
46        }
47    }
48
49    /// Creates a new pool with the current config.
50    ///
51    /// # Info
52    ///
53    /// Unlike other `deadpool-*` libs, `deadpool-amqprs` does not require user to pass [`deadpool::Runtime`],
54    /// because amqprs is built on top of `tokio`, meaning one can only use `tokio` with it.
55    #[must_use]
56    pub fn create_pool(&self) -> Pool {
57        self.builder()
58            .build()
59            .expect("`PoolBuilder::build` errored when it shouldn't")
60    }
61
62    /// Returns a [`PoolBuilder`] using the current config.
63    ///
64    /// # Info
65    ///
66    /// Unlike other `deadpool-*` libs, `deadpool-amqprs` does not require user to pass [`deadpool::Runtime`],
67    /// because amqprs is built on top of `tokio`, meaning one can only use `tokio` with it.
68    pub fn builder(&self) -> PoolBuilder {
69        Pool::builder(Manager::new(self.con_args.clone()))
70            .config(self.pool_config.unwrap_or_default())
71            .runtime(Runtime::Tokio1)
72    }
73}
74
75impl std::fmt::Debug for Config {
76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        f.debug_struct("Config")
78            .field("pool_config", &self.pool_config)
79            .finish_non_exhaustive()
80    }
81}
82
83pub type ConfigError = Infallible;