forked from sfackler/rust-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.rs
118 lines (98 loc) · 2.83 KB
/
config.rs
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
107
108
109
110
111
112
113
114
115
116
117
118
use futures::sync::oneshot;
use futures::Future;
use log::error;
use std::path::Path;
use std::str::FromStr;
use std::time::Duration;
use tokio_postgres::{Error, MakeTlsMode, Socket, TargetSessionAttrs, TlsMode};
use crate::{Client, RUNTIME};
#[derive(Debug, Clone, PartialEq)]
pub struct Config(tokio_postgres::Config);
impl Default for Config {
fn default() -> Config {
Config(tokio_postgres::Config::default())
}
}
impl Config {
pub fn new() -> Config {
Config(tokio_postgres::Config::new())
}
pub fn user(&mut self, user: &str) -> &mut Config {
self.0.user(user);
self
}
pub fn password<T>(&mut self, password: T) -> &mut Config
where
T: AsRef<[u8]>,
{
self.0.password(password);
self
}
pub fn dbname(&mut self, dbname: &str) -> &mut Config {
self.0.dbname(dbname);
self
}
pub fn options(&mut self, options: &str) -> &mut Config {
self.0.options(options);
self
}
pub fn application_name(&mut self, application_name: &str) -> &mut Config {
self.0.application_name(application_name);
self
}
pub fn host(&mut self, host: &str) -> &mut Config {
self.0.host(host);
self
}
#[cfg(unix)]
pub fn host_path<T>(&mut self, host: T) -> &mut Config
where
T: AsRef<Path>,
{
self.0.host_path(host);
self
}
pub fn port(&mut self, port: u16) -> &mut Config {
self.0.port(port);
self
}
pub fn connect_timeout(&mut self, connect_timeout: Duration) -> &mut Config {
self.0.connect_timeout(connect_timeout);
self
}
pub fn keepalives(&mut self, keepalives: bool) -> &mut Config {
self.0.keepalives(keepalives);
self
}
pub fn keepalives_idle(&mut self, keepalives_idle: Duration) -> &mut Config {
self.0.keepalives_idle(keepalives_idle);
self
}
pub fn target_session_attrs(
&mut self,
target_session_attrs: TargetSessionAttrs,
) -> &mut Config {
self.0.target_session_attrs(target_session_attrs);
self
}
pub fn connect<T>(&self, tls_mode: T) -> Result<Client, Error>
where
T: MakeTlsMode<Socket> + 'static + Send,
T::TlsMode: Send,
T::Stream: Send,
T::Future: Send,
<T::TlsMode as TlsMode<Socket>>::Future: Send,
{
let connect = self.0.connect(tls_mode);
let (client, connection) = oneshot::spawn(connect, &RUNTIME.executor()).wait()?;
let connection = connection.map_err(|e| error!("postgres connection error: {}", e));
RUNTIME.executor().spawn(connection);
Ok(Client::from(client))
}
}
impl FromStr for Config {
type Err = Error;
fn from_str(s: &str) -> Result<Config, Error> {
s.parse().map(Config)
}
}