Skip to content

Commit 15f7997

Browse files
committed
Make a PostgresResult typedef
1 parent ec6fd52 commit 15f7997

File tree

2 files changed

+46
-44
lines changed

2 files changed

+46
-44
lines changed

src/lib.rs

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ mod test;
207207

208208
static DEFAULT_PORT: Port = 5432;
209209

210+
/// A typedef of the result returned by many methods.
211+
pub type PostgresResult<T> = Result<T, PostgresError>;
212+
210213
/// Trait for types that can handle Postgres notice messages
211214
pub trait PostgresNoticeHandler {
212215
/// Handle a Postgres notice message
@@ -412,8 +415,8 @@ impl Drop for InnerPostgresConnection {
412415
}
413416

414417
impl InnerPostgresConnection {
415-
fn connect(url: &str, ssl: &SslMode)
416-
-> Result<InnerPostgresConnection, PostgresConnectError> {
418+
fn connect(url: &str, ssl: &SslMode) -> Result<InnerPostgresConnection,
419+
PostgresConnectError> {
417420
let Url {
418421
host,
419422
port,
@@ -511,8 +514,8 @@ impl InnerPostgresConnection {
511514
}
512515
}
513516

514-
fn handle_auth(&mut self, user: UserInfo) ->
515-
Result<(), PostgresConnectError> {
517+
fn handle_auth(&mut self, user: UserInfo)
518+
-> Result<(), PostgresConnectError> {
516519
match try_pg_conn!(self.read_message()) {
517520
AuthenticationOk => return Ok(()),
518521
AuthenticationCleartextPassword => {
@@ -565,7 +568,7 @@ impl InnerPostgresConnection {
565568
}
566569

567570
fn prepare<'a>(&mut self, query: &str, conn: &'a PostgresConnection)
568-
-> Result<PostgresStatement<'a>, PostgresError> {
571+
-> PostgresResult<PostgresStatement<'a>> {
569572
let stmt_name = format!("statement_{}", self.next_stmt_id);
570573
self.next_stmt_id += 1;
571574

@@ -626,7 +629,7 @@ impl InnerPostgresConnection {
626629
}
627630

628631
fn set_type_names<'a, I: Iterator<&'a mut PostgresType>>(&mut self, mut it: I)
629-
-> Result<(), PostgresError> {
632+
-> PostgresResult<()> {
630633
for ty in it {
631634
match *ty {
632635
PgUnknownType { oid, ref mut name } =>
@@ -637,7 +640,7 @@ impl InnerPostgresConnection {
637640
Ok(())
638641
}
639642

640-
fn get_type_name(&mut self, oid: Oid) -> Result<~str, PostgresError> {
643+
fn get_type_name(&mut self, oid: Oid) -> PostgresResult<~str> {
641644
match self.unknown_types.find(&oid) {
642645
Some(name) => return Ok(name.clone()),
643646
None => {}
@@ -653,15 +656,15 @@ impl InnerPostgresConnection {
653656
self.desynchronized
654657
}
655658

656-
fn wait_for_ready(&mut self) -> Result<(), PostgresError> {
659+
fn wait_for_ready(&mut self) -> PostgresResult<()> {
657660
match try_pg!(self.read_message()) {
658661
ReadyForQuery { .. } => Ok(()),
659662
_ => unreachable!()
660663
}
661664
}
662665

663666
fn quick_query(&mut self, query: &str)
664-
-> Result<Vec<Vec<Option<~str>>>, PostgresError> {
667+
-> PostgresResult<Vec<Vec<Option<~str>>>> {
665668
check_desync!(self);
666669
try_pg!(self.write_messages([Query { query: query }]));
667670

@@ -683,7 +686,7 @@ impl InnerPostgresConnection {
683686
Ok(result)
684687
}
685688

686-
fn finish_inner(&mut self) -> Result<(), PostgresError> {
689+
fn finish_inner(&mut self) -> PostgresResult<()> {
687690
check_desync!(self);
688691
Ok(try_pg!(self.write_messages([Terminate])))
689692
}
@@ -762,7 +765,7 @@ impl PostgresConnection {
762765
/// Err(err) => fail!("Error preparing statement: {}", err)
763766
/// };
764767
pub fn prepare<'a>(&'a self, query: &str)
765-
-> Result<PostgresStatement<'a>, PostgresError> {
768+
-> PostgresResult<PostgresStatement<'a>> {
766769
self.conn.borrow_mut().prepare(query, self)
767770
}
768771

@@ -793,7 +796,7 @@ impl PostgresConnection {
793796
/// # }
794797
/// ```
795798
pub fn transaction<'a>(&'a self)
796-
-> Result<PostgresTransaction<'a>, PostgresError> {
799+
-> PostgresResult<PostgresTransaction<'a>> {
797800
check_desync!(self);
798801
try!(self.quick_query("BEGIN"));
799802
Ok(PostgresTransaction {
@@ -811,7 +814,7 @@ impl PostgresConnection {
811814
///
812815
/// On success, returns the number of rows modified or 0 if not applicable.
813816
pub fn execute(&self, query: &str, params: &[&ToSql])
814-
-> Result<uint, PostgresError> {
817+
-> PostgresResult<uint> {
815818
self.prepare(query).and_then(|stmt| stmt.execute(params))
816819
}
817820

@@ -837,18 +840,18 @@ impl PostgresConnection {
837840
/// Functionally equivalent to the `Drop` implementation for
838841
/// `PostgresConnection` except that it returns any error encountered to
839842
/// the caller.
840-
pub fn finish(self) -> Result<(), PostgresError> {
843+
pub fn finish(self) -> PostgresResult<()> {
841844
let mut conn = self.conn.borrow_mut();
842845
conn.finished = true;
843846
conn.finish_inner()
844847
}
845848

846849
fn quick_query(&self, query: &str)
847-
-> Result<Vec<Vec<Option<~str>>>, PostgresError> {
850+
-> PostgresResult<Vec<Vec<Option<~str>>>> {
848851
self.conn.borrow_mut().quick_query(query)
849852
}
850853

851-
fn wait_for_ready(&self) -> Result<(), PostgresError> {
854+
fn wait_for_ready(&self) -> PostgresResult<()> {
852855
self.conn.borrow_mut().wait_for_ready()
853856
}
854857

@@ -893,7 +896,7 @@ impl<'conn> Drop for PostgresTransaction<'conn> {
893896
}
894897

895898
impl<'conn> PostgresTransaction<'conn> {
896-
fn finish_inner(&mut self) -> Result<(), PostgresError> {
899+
fn finish_inner(&mut self) -> PostgresResult<()> {
897900
if task::failing() || !self.commit.get() {
898901
if self.nested {
899902
try!(self.conn.quick_query("ROLLBACK TO sp"));
@@ -914,19 +917,19 @@ impl<'conn> PostgresTransaction<'conn> {
914917
impl<'conn> PostgresTransaction<'conn> {
915918
/// Like `PostgresConnection::prepare`.
916919
pub fn prepare<'a>(&'a self, query: &str)
917-
-> Result<PostgresStatement<'a>, PostgresError> {
920+
-> PostgresResult<PostgresStatement<'a>> {
918921
self.conn.prepare(query)
919922
}
920923

921924
/// Like `PostgresConnection::execute`.
922925
pub fn execute(&self, query: &str, params: &[&ToSql])
923-
-> Result<uint, PostgresError> {
926+
-> PostgresResult<uint> {
924927
self.conn.execute(query, params)
925928
}
926929

927930
/// Like `PostgresConnection::transaction`.
928931
pub fn transaction<'a>(&'a self)
929-
-> Result<PostgresTransaction<'a>, PostgresError> {
932+
-> PostgresResult<PostgresTransaction<'a>> {
930933
check_desync!(self.conn);
931934
try!(self.conn.quick_query("SAVEPOINT sp"));
932935
Ok(PostgresTransaction {
@@ -966,7 +969,7 @@ impl<'conn> PostgresTransaction<'conn> {
966969
///
967970
/// Functionally equivalent to the `Drop` implementation of
968971
/// `PostgresTransaction` except that it returns any error to the caller.
969-
pub fn finish(mut self) -> Result<(), PostgresError> {
972+
pub fn finish(mut self) -> PostgresResult<()> {
970973
self.finished = true;
971974
self.finish_inner()
972975
}
@@ -981,8 +984,8 @@ impl<'conn> PostgresTransaction<'conn> {
981984
stmt: &'stmt PostgresStatement,
982985
params: &[&ToSql],
983986
row_limit: uint)
984-
-> Result<PostgresLazyRows<'trans, 'stmt>,
985-
PostgresError> {
987+
-> PostgresResult<PostgresLazyRows
988+
<'trans, 'stmt>> {
986989
if self.conn as *PostgresConnection != stmt.conn as *PostgresConnection {
987990
return Err(PgWrongConnection);
988991
}
@@ -1020,7 +1023,7 @@ impl<'conn> Drop for PostgresStatement<'conn> {
10201023
}
10211024

10221025
impl<'conn> PostgresStatement<'conn> {
1023-
fn finish_inner(&mut self) -> Result<(), PostgresError> {
1026+
fn finish_inner(&mut self) -> PostgresResult<()> {
10241027
check_desync!(self.conn);
10251028
try_pg!(self.conn.write_messages([
10261029
Close {
@@ -1042,7 +1045,7 @@ impl<'conn> PostgresStatement<'conn> {
10421045
}
10431046

10441047
fn inner_execute(&self, portal_name: &str, row_limit: uint, params: &[&ToSql])
1045-
-> Result<(), PostgresError> {
1048+
-> PostgresResult<()> {
10461049
if self.param_types.len() != params.len() {
10471050
return Err(PgWrongParamCount {
10481051
expected: self.param_types.len(),
@@ -1086,7 +1089,7 @@ impl<'conn> PostgresStatement<'conn> {
10861089
}
10871090

10881091
fn lazy_query<'a>(&'a self, row_limit: uint, params: &[&ToSql])
1089-
-> Result<PostgresRows<'a>, PostgresError> {
1092+
-> PostgresResult<PostgresRows<'a>> {
10901093
let id = self.next_portal_id.get();
10911094
self.next_portal_id.set(id + 1);
10921095
let portal_name = format!("{}_portal_{}", self.name, id);
@@ -1133,7 +1136,7 @@ impl<'conn> PostgresStatement<'conn> {
11331136
/// Ok(count) => println!("{} row(s) updated", count),
11341137
/// Err(err) => println!("Error executing query: {}", err)
11351138
/// }
1136-
pub fn execute(&self, params: &[&ToSql]) -> Result<uint, PostgresError> {
1139+
pub fn execute(&self, params: &[&ToSql]) -> PostgresResult<uint> {
11371140
check_desync!(self.conn);
11381141
try!(self.inner_execute("", 0, params));
11391142

@@ -1186,7 +1189,7 @@ impl<'conn> PostgresStatement<'conn> {
11861189
/// }
11871190
/// ```
11881191
pub fn query<'a>(&'a self, params: &[&ToSql])
1189-
-> Result<PostgresRows<'a>, PostgresError> {
1192+
-> PostgresResult<PostgresRows<'a>> {
11901193
check_desync!(self.conn);
11911194
self.lazy_query(0, params)
11921195
}
@@ -1195,7 +1198,7 @@ impl<'conn> PostgresStatement<'conn> {
11951198
///
11961199
/// Functionally identical to the `Drop` implementation of the
11971200
/// `PostgresStatement` except that it returns any error to the caller.
1198-
pub fn finish(mut self) -> Result<(), PostgresError> {
1201+
pub fn finish(mut self) -> PostgresResult<()> {
11991202
self.finished.set(true);
12001203
self.finish_inner()
12011204
}
@@ -1234,7 +1237,7 @@ impl<'stmt> Drop for PostgresRows<'stmt> {
12341237
}
12351238

12361239
impl<'stmt> PostgresRows<'stmt> {
1237-
fn finish_inner(&mut self) -> Result<(), PostgresError> {
1240+
fn finish_inner(&mut self) -> PostgresResult<()> {
12381241
check_desync!(self.stmt.conn);
12391242
try_pg!(self.stmt.conn.write_messages([
12401243
Close {
@@ -1255,7 +1258,7 @@ impl<'stmt> PostgresRows<'stmt> {
12551258
Ok(())
12561259
}
12571260

1258-
fn read_rows(&mut self) -> Result<(), PostgresError> {
1261+
fn read_rows(&mut self) -> PostgresResult<()> {
12591262
loop {
12601263
match try_pg!(self.stmt.conn.read_message()) {
12611264
EmptyQueryResponse |
@@ -1274,7 +1277,7 @@ impl<'stmt> PostgresRows<'stmt> {
12741277
self.stmt.conn.wait_for_ready()
12751278
}
12761279

1277-
fn execute(&mut self) -> Result<(), PostgresError> {
1280+
fn execute(&mut self) -> PostgresResult<()> {
12781281
try_pg!(self.stmt.conn.write_messages([
12791282
Execute {
12801283
portal: self.name,
@@ -1290,13 +1293,12 @@ impl<'stmt> PostgresRows<'stmt> {
12901293
///
12911294
/// Functionally identical to the `Drop` implementation on `PostgresRows`
12921295
/// except that it returns any error to the caller.
1293-
pub fn finish(mut self) -> Result<(), PostgresError> {
1296+
pub fn finish(mut self) -> PostgresResult<()> {
12941297
self.finished = true;
12951298
self.finish_inner()
12961299
}
12971300

1298-
fn try_next(&mut self) -> Option<Result<PostgresRow<'stmt>,
1299-
PostgresError>> {
1301+
fn try_next(&mut self) -> Option<PostgresResult<PostgresRow<'stmt>>> {
13001302
if self.data.is_empty() && self.more_rows {
13011303
match self.execute() {
13021304
Ok(()) => {}
@@ -1344,8 +1346,7 @@ impl<'stmt> PostgresRow<'stmt> {
13441346
///
13451347
/// Returns an `Error` value if the index does not reference a column or
13461348
/// the return type is not compatible with the Postgres type.
1347-
pub fn get<I: RowIndex, T: FromSql>(&self, idx: I)
1348-
-> Result<T, PostgresError> {
1349+
pub fn get<I: RowIndex, T: FromSql>(&self, idx: I) -> PostgresResult<T> {
13491350
let idx = match idx.idx(self.stmt) {
13501351
Some(idx) => idx,
13511352
None => return Err(PgInvalidColumn)
@@ -1442,14 +1443,14 @@ pub struct PostgresLazyRows<'trans, 'stmt> {
14421443

14431444
impl<'trans, 'stmt> PostgresLazyRows<'trans, 'stmt> {
14441445
/// Like `PostgresRows::finish`.
1445-
pub fn finish(self) -> Result<(), PostgresError> {
1446+
pub fn finish(self) -> PostgresResult<()> {
14461447
self.result.finish()
14471448
}
14481449
}
14491450

1450-
impl<'trans, 'stmt> Iterator<Result<PostgresRow<'stmt>, PostgresError>>
1451+
impl<'trans, 'stmt> Iterator<PostgresResult<PostgresRow<'stmt>>>
14511452
for PostgresLazyRows<'trans, 'stmt> {
1452-
fn next(&mut self) -> Option<Result<PostgresRow<'stmt>, PostgresError>> {
1453+
fn next(&mut self) -> Option<PostgresResult<PostgresRow<'stmt>>> {
14531454
self.result.try_next()
14541455
}
14551456

src/pool.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ use std::cast;
44
use sync::{Arc, Mutex};
55

66
use {PostgresNotifications,
7+
PostgresResult,
78
PostgresCancelData,
89
PostgresConnection,
910
PostgresStatement,
1011
PostgresTransaction,
1112
SslMode};
12-
use error::{PostgresConnectError, PostgresError};
13+
use error::PostgresConnectError;
1314
use types::ToSql;
1415

1516
struct InnerConnectionPool {
@@ -130,19 +131,19 @@ impl Drop for PooledPostgresConnection {
130131
impl PooledPostgresConnection {
131132
/// Like `PostgresConnection::prepare`.
132133
pub fn prepare<'a>(&'a self, query: &str)
133-
-> Result<PostgresStatement<'a>, PostgresError> {
134+
-> PostgresResult<PostgresStatement<'a>> {
134135
self.conn.get_ref().prepare(query)
135136
}
136137

137138
/// Like `PostgresConnection::execute`.
138139
pub fn execute(&self, query: &str, params: &[&ToSql])
139-
-> Result<uint, PostgresError> {
140+
-> PostgresResult<uint> {
140141
self.conn.get_ref().execute(query, params)
141142
}
142143

143144
/// Like `PostgresConnection::transaction`.
144145
pub fn transaction<'a>(&'a self)
145-
-> Result<PostgresTransaction<'a>, PostgresError> {
146+
-> PostgresResult<PostgresTransaction<'a>> {
146147
self.conn.get_ref().transaction()
147148
}
148149

0 commit comments

Comments
 (0)