rusty_postgres/
lib.rs

1pub use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
2pub use postgres::types::ToSql;
3pub use postgres::{
4    tls::{MakeTlsConnect, TlsConnect},
5    Error, NoTls, Socket,
6};
7pub use rand;
8pub use rand::Rng;
9pub use serde_json;
10pub use uuid::Uuid;
11
12pub use rand::distributions::Alphanumeric;
13pub use serde::{Deserialize, Serialize};
14
15#[cfg(feature = "async")]
16pub use tokio;
17
18#[cfg(feature = "async")]
19use tokio_postgres::Connection;
20
21#[cfg(feature = "async")]
22pub use tokio::io;
23
24#[cfg(feature = "async")]
25pub use tokio_postgres::types::ToSql as AsyncToSql;
26
27#[cfg(feature = "async")]
28pub use tokio_postgres::{
29    tls::{MakeTlsConnect as AsyncMakeTlsConnect, TlsConnect as AsyncTlsConnect},
30    Error as AsyncError, NoTls as AsyncNoTls, Socket as AsyncSocket,
31};
32
33#[cfg(feature = "async")]
34pub use tokio::fs::File;
35
36#[cfg(feature = "async")]
37pub use tokio::io::AsyncWriteExt;
38
39#[cfg(feature = "async")]
40pub use tokio_postgres::GenericClient;
41
42#[cfg(feature = "async")]
43pub use tokio::fs::DirBuilder;
44
45#[cfg(feature = "async")]
46pub use tokio_postgres::Client;
47
48#[macro_use]
49pub mod method;
50
51#[cfg(not(feature = "async"))]
52pub struct Client;
53
54#[cfg(not(feature = "async"))]
55impl Client {
56    pub fn connect<T>(client: &str, tls_mode: T) -> Result<postgres::Client, Error>
57    where
58        T: MakeTlsConnect<Socket> + 'static + Send,
59        T::TlsConnect: Send,
60        T::Stream: Send,
61        <T::TlsConnect as TlsConnect<Socket>>::Future: Send,
62    {
63        let client = postgres::Client::connect(client, tls_mode);
64        client
65    }
66}
67
68#[cfg(feature = "async")]
69pub struct TokioClient;
70
71#[cfg(feature = "async")]
72impl TokioClient {
73    pub async fn connect<T>(client: &str, tls_mode: T) -> Result<tokio_postgres::Client, Error>
74    where
75        T: AsyncMakeTlsConnect<Socket> + 'static + Send,
76        T::TlsConnect: Send,
77        T::Stream: Send,
78        <T::TlsConnect as TlsConnect<Socket>>::Future: Send,
79    {
80        let client = tokio_postgres::connect(client, tls_mode);
81        match client.await {
82            Ok((client, connection)) => {
83                tokio::spawn(async move {
84                    if let Err(err) = connection.await {
85                        eprintln!("{}", err);
86                    }
87                });
88                Ok(client)
89            }
90            Err(err) => Err(err),
91        }
92    }
93}