-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.rs
More file actions
61 lines (54 loc) · 1.81 KB
/
config.rs
File metadata and controls
61 lines (54 loc) · 1.81 KB
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
use deadpool_postgres::{Config, ManagerConfig, Pool, RecyclingMethod, Runtime};
use tokio_postgres::NoTls;
use crate::error::PoolError;
#[derive(Default)]
pub struct ConfigBuilder<'b> {
host: Option<&'b str>,
password: Option<&'b str>,
user: Option<&'b str>,
database: Option<&'b str>,
recycling_method: RecyclingMethod,
runtime: Option<Runtime>,
}
impl<'b> ConfigBuilder<'b> {
pub fn host(mut self, host: &'b str) -> Self {
self.host = Some(host);
self
}
pub fn password(mut self, password: &'b str) -> Self {
self.password = Some(password);
self
}
pub fn user(mut self, user: &'b str) -> Self {
self.user = Some(user);
self
}
pub fn database(mut self, database: &'b str) -> Self {
self.database = Some(database);
self
}
pub fn recycling_method(mut self, recycling_method: RecyclingMethod) -> Self {
self.recycling_method = recycling_method;
self
}
pub fn runtime(mut self, runtime: Runtime) -> Self {
self.runtime = Some(runtime);
self
}
pub async fn build(self) -> Result<Pool, PoolError> {
let mut pg_config = Config::new();
pg_config.host = self.host.map(str::to_string);
pg_config.password = self.password.map(str::to_string);
pg_config.user = self.user.map(str::to_string);
pg_config.dbname = self.database.map(str::to_string);
pg_config.manager = Some(ManagerConfig {
recycling_method: self.recycling_method,
});
let pool = pg_config
.create_pool(self.runtime, NoTls)
.map_err(|_| PoolError::Creation)?;
// Try to connect to database to test if the database exist
let _ = pool.get().await.map_err(|_| PoolError::Connection)?;
Ok(pool)
}
}