rusty_postgres/
lib.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
pub use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
pub use postgres::types::ToSql;
pub use postgres::{
    tls::{MakeTlsConnect, TlsConnect},
    Error, NoTls, Socket,
};
pub use rand;
pub use rand::Rng;
pub use serde_json;
pub use uuid::Uuid;

pub use rand::distributions::Alphanumeric;
pub use serde::{Deserialize, Serialize};

#[cfg(feature = "async")]
pub use tokio;

#[cfg(feature = "async")]
use tokio_postgres::Connection;

#[cfg(feature = "async")]
pub use tokio::io;

#[cfg(feature = "async")]
pub use tokio_postgres::types::ToSql as AsyncToSql;

#[cfg(feature = "async")]
pub use tokio_postgres::{
    tls::{MakeTlsConnect as AsyncMakeTlsConnect, TlsConnect as AsyncTlsConnect},
    Error as AsyncError, NoTls as AsyncNoTls, Socket as AsyncSocket,
};

#[cfg(feature = "async")]
pub use tokio::fs::File;

#[cfg(feature = "async")]
pub use tokio::io::AsyncWriteExt;

#[cfg(feature = "async")]
pub use tokio_postgres::GenericClient;

#[cfg(feature = "async")]
pub use tokio::fs::DirBuilder;

#[cfg(feature = "async")]
pub use tokio_postgres::Client;

#[macro_use]
pub mod method;

#[cfg(not(feature = "async"))]
pub struct Client;

#[cfg(not(feature = "async"))]
impl Client {
    pub fn connect<T>(client: &str, tls_mode: T) -> Result<postgres::Client, Error>
    where
        T: MakeTlsConnect<Socket> + 'static + Send,
        T::TlsConnect: Send,
        T::Stream: Send,
        <T::TlsConnect as TlsConnect<Socket>>::Future: Send,
    {
        let client = postgres::Client::connect(client, tls_mode);
        client
    }
}

#[cfg(feature = "async")]
pub struct TokioClient;

#[cfg(feature = "async")]
impl TokioClient {
    pub async fn connect<T>(client: &str, tls_mode: T) -> Result<tokio_postgres::Client, Error>
    where
        T: AsyncMakeTlsConnect<Socket> + 'static + Send,
        T::TlsConnect: Send,
        T::Stream: Send,
        <T::TlsConnect as TlsConnect<Socket>>::Future: Send,
    {
        let client = tokio_postgres::connect(client, tls_mode);
        match client.await {
            Ok((client, connection)) => {
                tokio::spawn(async move {
                    if let Err(err) = connection.await {
                        eprintln!("{}", err);
                    }
                });
                Ok(client)
            }
            Err(err) => Err(err),
        }
    }
}