forked from rust-postgres/rust-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.rs
More file actions
50 lines (41 loc) · 1.26 KB
/
Copy pathbuilder.rs
File metadata and controls
50 lines (41 loc) · 1.26 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
use futures::sync::oneshot;
use futures::Future;
use log::error;
use std::str::FromStr;
use tokio_postgres::{Error, MakeTlsMode, Socket, TlsMode};
use crate::{Client, RUNTIME};
pub struct Builder(tokio_postgres::Builder);
impl Default for Builder {
fn default() -> Builder {
Builder(tokio_postgres::Builder::default())
}
}
impl Builder {
pub fn new() -> Builder {
Builder(tokio_postgres::Builder::new())
}
pub fn param(&mut self, key: &str, value: &str) -> &mut Builder {
self.0.param(key, value);
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 Builder {
type Err = Error;
fn from_str(s: &str) -> Result<Builder, Error> {
s.parse().map(Builder)
}
}