22
33#![ warn( missing_docs) ]
44
5- # [ doc ( no_inline ) ]
5+ pub use stream :: AsyncStream ;
66pub use tokio_postgres:: * ;
77
8- #[ doc( inline) ]
9- pub use tls:: { connect_with, Connection , TlsConfig } ;
10-
118use std:: io;
9+ use stream:: connect_stream;
10+ use tokio_postgres:: tls:: { NoTls , NoTlsStream , TlsConnect } ;
11+ use tokio_postgres:: { Client , Connection } ;
12+
13+ /// Connect to postgres server.
14+ ///
15+ /// ```rust
16+ /// use async_postgres::connect;
17+ ///
18+ /// use std::error::Error;
19+ /// use async_std::task::spawn;
20+ ///
21+ /// async fn play() -> Result<(), Box<dyn Error>> {
22+ /// let url = "host=localhost user=postgres";
23+ /// let (client, conn) = connect(url.parse()?).await?;
24+ /// spawn(conn);
25+ /// let row = client.query_one("SELECT * FROM user WHERE id=$1", &[&0]).await?;
26+ /// let value: &str = row.get(0);
27+ /// println!("value: {}", value);
28+ /// Ok(())
29+ /// }
30+ /// ```
31+ #[ inline]
32+ pub async fn connect (
33+ config : Config ,
34+ ) -> io:: Result < ( Client , Connection < AsyncStream , NoTlsStream > ) > {
35+ connect_tls ( config, NoTls ) . await
36+ }
1237
13- /// Connect to postgres server with default tls config .
38+ /// Connect to postgres server with a tls connector .
1439///
1540/// ```rust
1641/// use async_postgres::connect;
@@ -20,7 +45,7 @@ use std::io;
2045///
2146/// async fn play() -> Result<(), Box<dyn Error>> {
2247/// let url = "host=localhost user=postgres";
23- /// let (client, conn) = connect(& url.parse()?).await?;
48+ /// let (client, conn) = connect(url.parse()?).await?;
2449/// spawn(conn);
2550/// let row = client.query_one("SELECT * FROM user WHERE id=$1", &[&0]).await?;
2651/// let value: &str = row.get(0);
@@ -29,9 +54,18 @@ use std::io;
2954/// }
3055/// ```
3156#[ inline]
32- pub async fn connect ( config : & Config ) -> io:: Result < ( Client , Connection ) > {
33- connect_with ( config, TlsConfig :: default ( ) ) . await
57+ pub async fn connect_tls < T > (
58+ config : Config ,
59+ tls : T ,
60+ ) -> io:: Result < ( Client , Connection < AsyncStream , T :: Stream > ) >
61+ where
62+ T : TlsConnect < AsyncStream > ,
63+ {
64+ let stream = connect_stream ( & config) . await ?;
65+ config
66+ . connect_raw ( stream, tls)
67+ . await
68+ . map_err ( |err| io:: Error :: new ( io:: ErrorKind :: Other , err) )
3469}
3570
3671mod stream;
37- mod tls;
0 commit comments