Skip to content

Commit 842bdaf

Browse files
committed
Pull connect params out to its own module
1 parent b76ac7e commit 842bdaf

4 files changed

Lines changed: 111 additions & 105 deletions

File tree

src/lib.rs

Lines changed: 4 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,10 @@ use bufstream::BufStream;
5353
use md5::Md5;
5454
use std::cell::{Cell, RefCell};
5555
use std::collections::{VecDeque, HashMap};
56-
use std::error::Error as StdError;
5756
use std::fmt;
5857
use std::io as std_io;
5958
use std::io::prelude::*;
6059
use std::mem;
61-
use std::path::PathBuf;
6260
use std::result;
6361
use std::sync::Arc;
6462
use std::time::Duration;
@@ -68,10 +66,10 @@ use io::{TlsStream, TlsHandshake};
6866
use message::{Frontend, Backend, RowDescriptionEntry};
6967
use message::{WriteMessage, ReadMessage};
7068
use notification::{Notifications, Notification};
69+
use params::{ConnectParams, IntoConnectParams, UserInfo};
7170
use rows::{Rows, LazyRows};
7271
use stmt::{Statement, Column};
7372
use types::{IsNull, Kind, Type, SessionInfo, Oid, Other, WrongType, ToSql, FromSql, Field};
74-
use url::Url;
7573
use transaction::{Transaction, IsolationLevel};
7674

7775
#[macro_use]
@@ -85,6 +83,7 @@ mod url;
8583
pub mod error;
8684
pub mod io;
8785
pub mod notification;
86+
pub mod params;
8887
pub mod rows;
8988
pub mod stmt;
9089
pub mod transaction;
@@ -97,104 +96,6 @@ const TYPEINFO_COMPOSITE_QUERY: &'static str = "__typeinfo_composite";
9796
/// A type alias of the result returned by many methods.
9897
pub type Result<T> = result::Result<T, Error>;
9998

100-
/// Specifies the target server to connect to.
101-
#[derive(Clone, Debug)]
102-
pub enum ConnectTarget {
103-
/// Connect via TCP to the specified host.
104-
Tcp(String),
105-
/// Connect via a Unix domain socket in the specified directory.
106-
///
107-
/// Unix sockets are only supported on Unixy platforms (i.e. not Windows).
108-
Unix(PathBuf),
109-
}
110-
111-
/// Authentication information.
112-
#[derive(Clone, Debug)]
113-
pub struct UserInfo {
114-
/// The username.
115-
pub user: String,
116-
/// An optional password.
117-
pub password: Option<String>,
118-
}
119-
120-
/// Information necessary to open a new connection to a Postgres server.
121-
#[derive(Clone, Debug)]
122-
pub struct ConnectParams {
123-
/// The target server.
124-
pub target: ConnectTarget,
125-
/// The target port.
126-
///
127-
/// Defaults to 5432 if not specified.
128-
pub port: Option<u16>,
129-
/// The user to login as.
130-
///
131-
/// `Connection::connect` requires a user but `cancel_query` does not.
132-
pub user: Option<UserInfo>,
133-
/// The database to connect to.
134-
///
135-
/// Defaults the value of `user`.
136-
pub database: Option<String>,
137-
/// Runtime parameters to be passed to the Postgres backend.
138-
pub options: Vec<(String, String)>,
139-
}
140-
141-
/// A trait implemented by types that can be converted into a `ConnectParams`.
142-
pub trait IntoConnectParams {
143-
/// Converts the value of `self` into a `ConnectParams`.
144-
fn into_connect_params(self) -> result::Result<ConnectParams, Box<StdError + Sync + Send>>;
145-
}
146-
147-
impl IntoConnectParams for ConnectParams {
148-
fn into_connect_params(self) -> result::Result<ConnectParams, Box<StdError + Sync + Send>> {
149-
Ok(self)
150-
}
151-
}
152-
153-
impl<'a> IntoConnectParams for &'a str {
154-
fn into_connect_params(self) -> result::Result<ConnectParams, Box<StdError + Sync + Send>> {
155-
match Url::parse(self) {
156-
Ok(url) => url.into_connect_params(),
157-
Err(err) => Err(err.into()),
158-
}
159-
}
160-
}
161-
162-
impl IntoConnectParams for Url {
163-
fn into_connect_params(self) -> result::Result<ConnectParams, Box<StdError + Sync + Send>> {
164-
let Url { host, port, user, path: url::Path { mut path, query: options, .. }, .. } = self;
165-
166-
let maybe_path = try!(url::decode_component(&host));
167-
let target = if maybe_path.starts_with('/') {
168-
ConnectTarget::Unix(PathBuf::from(maybe_path))
169-
} else {
170-
ConnectTarget::Tcp(host)
171-
};
172-
173-
let user = user.map(|url::UserInfo { user, pass }| {
174-
UserInfo {
175-
user: user,
176-
password: pass,
177-
}
178-
});
179-
180-
let database = if path.is_empty() {
181-
None
182-
} else {
183-
// path contains the leading /
184-
path.remove(0);
185-
Some(path)
186-
};
187-
188-
Ok(ConnectParams {
189-
target: target,
190-
port: port,
191-
user: user,
192-
database: database,
193-
options: options,
194-
})
195-
}
196-
}
197-
19899
/// Trait for types that can handle Postgres notice messages
199100
///
200101
/// It is implemented for all `Send + FnMut(DbError)` closures.
@@ -993,7 +894,8 @@ impl Connection {
993894
/// ```
994895
///
995896
/// ```rust,no_run
996-
/// use postgres::{Connection, UserInfo, ConnectParams, TlsMode, ConnectTarget};
897+
/// use postgres::{Connection, TlsMode};
898+
/// use postgres::params::{UserInfo, ConnectParams, ConnectTarget};
997899
/// # use std::path::PathBuf;
998900
///
999901
/// # #[cfg(unix)]

src/params.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//! Connection parameters
2+
use std::error::Error;
3+
use std::path::PathBuf;
4+
5+
use url::{self, Url};
6+
7+
/// Specifies the target server to connect to.
8+
#[derive(Clone, Debug)]
9+
pub enum ConnectTarget {
10+
/// Connect via TCP to the specified host.
11+
Tcp(String),
12+
/// Connect via a Unix domain socket in the specified directory.
13+
///
14+
/// Unix sockets are only supported on Unixy platforms (i.e. not Windows).
15+
Unix(PathBuf),
16+
}
17+
18+
/// Authentication information.
19+
#[derive(Clone, Debug)]
20+
pub struct UserInfo {
21+
/// The username.
22+
pub user: String,
23+
/// An optional password.
24+
pub password: Option<String>,
25+
}
26+
27+
/// Information necessary to open a new connection to a Postgres server.
28+
#[derive(Clone, Debug)]
29+
pub struct ConnectParams {
30+
/// The target server.
31+
pub target: ConnectTarget,
32+
/// The target port.
33+
///
34+
/// Defaults to 5432 if not specified.
35+
pub port: Option<u16>,
36+
/// The user to login as.
37+
///
38+
/// `Connection::connect` requires a user but `cancel_query` does not.
39+
pub user: Option<UserInfo>,
40+
/// The database to connect to.
41+
///
42+
/// Defaults the value of `user`.
43+
pub database: Option<String>,
44+
/// Runtime parameters to be passed to the Postgres backend.
45+
pub options: Vec<(String, String)>,
46+
}
47+
48+
/// A trait implemented by types that can be converted into a `ConnectParams`.
49+
pub trait IntoConnectParams {
50+
/// Converts the value of `self` into a `ConnectParams`.
51+
fn into_connect_params(self) -> Result<ConnectParams, Box<Error + Sync + Send>>;
52+
}
53+
54+
impl IntoConnectParams for ConnectParams {
55+
fn into_connect_params(self) -> Result<ConnectParams, Box<Error + Sync + Send>> {
56+
Ok(self)
57+
}
58+
}
59+
60+
impl<'a> IntoConnectParams for &'a str {
61+
fn into_connect_params(self) -> Result<ConnectParams, Box<Error + Sync + Send>> {
62+
match Url::parse(self) {
63+
Ok(url) => url.into_connect_params(),
64+
Err(err) => Err(err.into()),
65+
}
66+
}
67+
}
68+
69+
impl IntoConnectParams for Url {
70+
fn into_connect_params(self) -> Result<ConnectParams, Box<Error + Sync + Send>> {
71+
let Url { host, port, user, path: url::Path { mut path, query: options, .. }, .. } = self;
72+
73+
let maybe_path = try!(url::decode_component(&host));
74+
let target = if maybe_path.starts_with('/') {
75+
ConnectTarget::Unix(PathBuf::from(maybe_path))
76+
} else {
77+
ConnectTarget::Tcp(host)
78+
};
79+
80+
let user = user.map(|url::UserInfo { user, pass }| {
81+
UserInfo {
82+
user: user,
83+
password: pass,
84+
}
85+
});
86+
87+
let database = if path.is_empty() {
88+
None
89+
} else {
90+
// path contains the leading /
91+
path.remove(0);
92+
Some(path)
93+
};
94+
95+
Ok(ConnectParams {
96+
target: target,
97+
port: port,
98+
user: user,
99+
database: database,
100+
options: options,
101+
})
102+
}
103+
}

src/priv_io.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use std::os::unix::io::{AsRawFd, RawFd};
1212
#[cfg(windows)]
1313
use std::os::windows::io::{AsRawSocket, RawSocket};
1414

15-
use {TlsMode, ConnectParams, ConnectTarget};
15+
use TlsMode;
16+
use params::{ConnectParams, ConnectTarget};
1617
use error::ConnectError;
1718
use io::TlsStream;
1819
use message::{self, WriteMessage};

tests/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ extern crate security_framework;
88

99
use std::thread;
1010
use std::io;
11-
use std::io::prelude::*;
1211
use std::time::Duration;
1312

14-
use postgres::{HandleNotice, Connection, GenericConnection, TlsMode, IntoConnectParams};
13+
use postgres::{HandleNotice, Connection, GenericConnection, TlsMode};
1514
use postgres::transaction::{self, IsolationLevel};
1615
use postgres::error::{Error, ConnectError, DbError};
1716
use postgres::types::{Oid, Type, Kind, WrongType};
@@ -24,6 +23,7 @@ use postgres::error::SqlState::{SyntaxError,
2423
use postgres::error::ErrorPosition::Normal;
2524
use postgres::rows::RowIndex;
2625
use postgres::notification::Notification;
26+
use postgres::params::IntoConnectParams;
2727

2828
macro_rules! or_panic {
2929
($e:expr) => (

0 commit comments

Comments
 (0)