1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::convert::Infallible;
#[cfg(feature = "rt_async-std_1")]
use async_amqp::LapinAsyncStdExt as _;
#[cfg(feature = "rt_tokio_1")]
use tokio_amqp::LapinTokioExt as _;
use crate::{CreatePoolError, Manager, Pool, PoolBuilder, PoolConfig, Runtime};
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde_1::Deserialize, serde_1::Serialize))]
#[cfg_attr(feature = "serde", serde(crate = "serde_1"))]
pub struct Config {
pub url: Option<String>,
pub pool: Option<PoolConfig>,
#[cfg_attr(feature = "serde", serde(skip))]
pub connection_properties: lapin::ConnectionProperties,
}
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) => conn_props.with_tokio(),
#[cfg(feature = "rt_async-std_1")]
Some(Runtime::AsyncStd1) => conn_props.with_async_std(),
};
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;