Skip to content

Commit a9ec725

Browse files
committed
More cleanup
1 parent 5157a00 commit a9ec725

File tree

4 files changed

+39
-35
lines changed

4 files changed

+39
-35
lines changed

src/error.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ macro_rules! make_errors(
2424
$($code => $error),+
2525
);
2626

27-
impl FromStr for PostgresSqlState {
28-
fn from_str(s: &str) -> Option<PostgresSqlState> {
29-
Some(match STATE_MAP.find(&s) {
27+
impl PostgresSqlState {
28+
#[doc(hidden)]
29+
pub fn from_code(s: &str) -> PostgresSqlState {
30+
match STATE_MAP.find(&s) {
3031
Some(state) => state.clone(),
3132
None => UnknownSqlState(s.to_owned())
32-
})
33+
}
3334
}
3435
}
3536
)
@@ -362,9 +363,9 @@ pub enum PostgresConnectError {
362363
/// The URL was missing a user
363364
MissingUser,
364365
/// DNS lookup failed
365-
DnsError,
366+
DnsError(IoError),
366367
/// There was an error opening a socket to the server
367-
SocketError,
368+
SocketError(IoError),
368369
/// An error from the Postgres server itself
369370
PgConnectDbError(PostgresDbError),
370371
/// A password was required but not provided in the URL
@@ -385,9 +386,9 @@ impl fmt::Show for PostgresConnectError {
385386
match *self {
386387
InvalidUrl => fmt.buf.write_str("Invalid URL"),
387388
MissingUser => fmt.buf.write_str("User missing in URL"),
388-
DnsError => fmt.buf.write_str("DNS lookup failed"),
389-
SocketError =>
390-
fmt.buf.write_str("Unable to open connection to server"),
389+
DnsError(ref err) => write!(fmt.buf, "DNS lookup failed: {}", err),
390+
SocketError(ref err) =>
391+
write!(fmt.buf, "Unable to open connection to server: {}", err),
391392
PgConnectDbError(ref err) => err.fmt(fmt),
392393
MissingPassword =>
393394
fmt.buf.write_str("The server requested a password but none \
@@ -397,8 +398,10 @@ impl fmt::Show for PostgresConnectError {
397398
authentication method"),
398399
NoSslSupport =>
399400
fmt.buf.write_str("The server does not support SSL"),
400-
SslError(ref err) => err.fmt(fmt),
401-
PgConnectStreamError(ref err) => err.fmt(fmt),
401+
SslError(ref err) =>
402+
write!(fmt.buf, "Error initiating SSL session: {}", err),
403+
PgConnectStreamError(ref err) =>
404+
write!(fmt.buf, "Error communicating with server: {}", err),
402405
}
403406
}
404407
}
@@ -477,7 +480,7 @@ impl PostgresDbError {
477480
let mut map: HashMap<u8, ~str> = fields.move_iter().collect();
478481
PostgresDbError {
479482
severity: map.pop(&('S' as u8)).unwrap(),
480-
code: FromStr::from_str(map.pop(&('C' as u8)).unwrap()).unwrap(),
483+
code: PostgresSqlState::from_code(map.pop(&('C' as u8)).unwrap()),
481484
message: map.pop(&('M' as u8)).unwrap(),
482485
detail: map.pop(&('D' as u8)),
483486
hint: map.pop(&('H' as u8)),

src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ pub struct PostgresCancelData {
268268
/// `PostgresConnection::cancel_data`. The object can cancel any query made on
269269
/// that connection.
270270
///
271+
/// Only the host and port of the URL are used.
272+
///
271273
/// # Example
272274
///
273275
/// ```rust,no_run
@@ -311,17 +313,18 @@ fn open_socket(host: &str, port: Port)
311313
-> Result<TcpStream, PostgresConnectError> {
312314
let addrs = match net::get_host_addresses(host) {
313315
Ok(addrs) => addrs,
314-
Err(_) => return Err(DnsError)
316+
Err(err) => return Err(DnsError(err))
315317
};
316318

319+
let mut err = None;
317320
for &addr in addrs.iter() {
318321
match TcpStream::connect(SocketAddr { ip: addr, port: port }) {
319322
Ok(socket) => return Ok(socket),
320-
Err(_) => {}
323+
Err(e) => err = Some(e)
321324
}
322325
}
323326

324-
Err(SocketError)
327+
Err(SocketError(err.unwrap()))
325328
}
326329

327330
fn initialize_stream(host: &str, port: Port, ssl: &SslMode)
@@ -1356,7 +1359,7 @@ impl<'stmt> Container for PostgresRow<'stmt> {
13561359
}
13571360
}
13581361

1359-
impl<'stmt, I: RowIndex, T: FromSql> Index<I, T> for PostgresRow<'stmt> {
1362+
impl<'stmt, I: RowIndex+Clone, T: FromSql> Index<I, T> for PostgresRow<'stmt> {
13601363
/// Retreives the contents of a field of the row.
13611364
///
13621365
/// A field can be accessed by the name or index of its column, though
@@ -1387,7 +1390,7 @@ impl<'stmt, I: RowIndex, T: FromSql> Index<I, T> for PostgresRow<'stmt> {
13871390
}
13881391

13891392
/// A trait implemented by types that can index into columns of a row.
1390-
pub trait RowIndex: Clone {
1393+
pub trait RowIndex {
13911394
/// Returns the index of the appropriate column, or `None` if no such
13921395
/// column exists.
13931396
fn idx(&self, stmt: &PostgresStatement) -> Option<uint>;

src/test.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ fn test_plaintext_pass_no_pass() {
897897
let ret = PostgresConnection::connect("postgres://pass_user@localhost/postgres", &NoSsl);
898898
match ret {
899899
Err(MissingPassword) => (),
900-
Err(err) => fail!("Unexpected error {}", err.to_str()),
900+
Err(err) => fail!("Unexpected error {}", err),
901901
_ => fail!("Expected error")
902902
}
903903
}
@@ -907,7 +907,7 @@ fn test_plaintext_pass_wrong_pass() {
907907
let ret = PostgresConnection::connect("postgres://pass_user:asdf@localhost/postgres", &NoSsl);
908908
match ret {
909909
Err(PgConnectDbError(PostgresDbError { code: InvalidPassword, .. })) => (),
910-
Err(err) => fail!("Unexpected error {}", err.to_str()),
910+
Err(err) => fail!("Unexpected error {}", err),
911911
_ => fail!("Expected error")
912912
}
913913
}
@@ -922,7 +922,7 @@ fn test_md5_pass_no_pass() {
922922
let ret = PostgresConnection::connect("postgres://md5_user@localhost/postgres", &NoSsl);
923923
match ret {
924924
Err(MissingPassword) => (),
925-
Err(err) => fail!("Unexpected error {}", err.to_str()),
925+
Err(err) => fail!("Unexpected error {}", err),
926926
_ => fail!("Expected error")
927927
}
928928
}
@@ -932,7 +932,7 @@ fn test_md5_pass_wrong_pass() {
932932
let ret = PostgresConnection::connect("postgres://md5_user:asdf@localhost/postgres", &NoSsl);
933933
match ret {
934934
Err(PgConnectDbError(PostgresDbError { code: InvalidPassword, .. })) => (),
935-
Err(err) => fail!("Unexpected error {}", err.to_str()),
935+
Err(err) => fail!("Unexpected error {}", err),
936936
_ => fail!("Expected error")
937937
}
938938
}
@@ -941,8 +941,8 @@ fn test_md5_pass_wrong_pass() {
941941
fn test_dns_failure() {
942942
let ret = PostgresConnection::connect("postgres://postgres@asdfasdfasdf", &NoSsl);
943943
match ret {
944-
Err(DnsError) => (),
945-
Err(err) => fail!("Unexpected error {}", err.to_str()),
944+
Err(DnsError(_)) => (),
945+
Err(err) => fail!("Unexpected error {}", err),
946946
_ => fail!("Expected error")
947947
}
948948
}

src/types/range.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,20 +184,18 @@ impl<S: BoundSided, T: Clone> Clone for RangeBound<S, T> {
184184
}
185185

186186
impl<S: BoundSided, T: fmt::Show> fmt::Show for RangeBound<S, T> {
187-
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
187+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
188188
let chars = match self.type_ {
189189
Inclusive => ['[', ']'],
190190
Exclusive => ['(', ')'],
191191
};
192192

193193
match BoundSided::side(None::<S>) {
194194
Lower => {
195-
try!(formatter.buf.write_char(chars[0]));
196-
self.value.fmt(formatter)
195+
write!(fmt.buf, "{}{}", chars[0], self.value)
197196
}
198197
Upper => {
199-
try!(self.value.fmt(formatter));
200-
formatter.buf.write_char(chars[1])
198+
write!(fmt.buf, "{}{}", self.value, chars[1])
201199
}
202200
}
203201
}
@@ -270,18 +268,18 @@ enum InnerRange<T> {
270268
}
271269

272270
impl<T: fmt::Show> fmt::Show for Range<T> {
273-
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
271+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
274272
match self.inner {
275-
Empty => formatter.buf.write_str("empty"),
273+
Empty => fmt.buf.write_str("empty"),
276274
Normal(ref lower, ref upper) => {
277275
match *lower {
278-
Some(ref bound) => try!(bound.fmt(formatter)),
279-
None => try!(formatter.buf.write_char('(')),
276+
Some(ref bound) => try!(bound.fmt(fmt)),
277+
None => try!(fmt.buf.write_char('(')),
280278
}
281-
try!(formatter.buf.write_char(','));
279+
try!(fmt.buf.write_char(','));
282280
match *upper {
283-
Some(ref bound) => bound.fmt(formatter),
284-
None => formatter.buf.write_char(')'),
281+
Some(ref bound) => bound.fmt(fmt),
282+
None => fmt.buf.write_char(')'),
285283
}
286284
}
287285
}

0 commit comments

Comments
 (0)