Skip to content

Commit a773f19

Browse files
committed
Some cleanup
1 parent 613ceec commit a773f19

File tree

2 files changed

+36
-42
lines changed

2 files changed

+36
-42
lines changed

src/lib.rs

+27-33
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub use stmt::{NormalPostgresStatement,
150150
RowIndex,
151151
TransactionalPostgresStatement};
152152

153-
macro_rules! if_ok_pg_conn(
153+
macro_rules! try_pg_conn(
154154
($e:expr) => (
155155
match $e {
156156
Ok(ok) => ok,
@@ -159,7 +159,7 @@ macro_rules! if_ok_pg_conn(
159159
)
160160
)
161161

162-
macro_rules! if_ok_pg(
162+
macro_rules! try_pg(
163163
($e:expr) => (
164164
match $e {
165165
Ok(ok) => ok,
@@ -168,7 +168,7 @@ macro_rules! if_ok_pg(
168168
)
169169
)
170170

171-
macro_rules! if_ok_desync(
171+
macro_rules! try_desync(
172172
($e:expr) => (
173173
match $e {
174174
Ok(ok) => ok,
@@ -297,12 +297,12 @@ pub fn cancel_query(url: &str, ssl: &SslMode, data: PostgresCancelData)
297297
Err(err) => return Err(err)
298298
};
299299

300-
if_ok_pg_conn!(socket.write_message(&CancelRequest {
300+
try_pg_conn!(socket.write_message(&CancelRequest {
301301
code: message::CANCEL_CODE,
302302
process_id: data.process_id,
303303
secret_key: data.secret_key
304304
}));
305-
if_ok_pg_conn!(socket.flush());
305+
try_pg_conn!(socket.flush());
306306

307307
Ok(())
308308
}
@@ -337,10 +337,10 @@ fn initialize_stream(host: &str, port: Port, ssl: &SslMode)
337337
&RequireSsl(ref ctx) => (true, ctx)
338338
};
339339

340-
if_ok_pg_conn!(socket.write_message(&SslRequest { code: message::SSL_CODE }));
341-
if_ok_pg_conn!(socket.flush());
340+
try_pg_conn!(socket.write_message(&SslRequest { code: message::SSL_CODE }));
341+
try_pg_conn!(socket.flush());
342342

343-
if if_ok_pg_conn!(socket.read_u8()) == 'N' as u8 {
343+
if try_pg_conn!(socket.read_u8()) == 'N' as u8 {
344344
if ssl_required {
345345
return Err(NoSslSupport);
346346
} else {
@@ -432,10 +432,7 @@ impl InnerPostgresConnection {
432432
None => DEFAULT_PORT
433433
};
434434

435-
let stream = match initialize_stream(host, port, ssl) {
436-
Ok(stream) => stream,
437-
Err(err) => return Err(err)
438-
};
435+
let stream = try!(initialize_stream(host, port, ssl));
439436

440437
let mut conn = InnerPostgresConnection {
441438
stream: BufferedStream::new(stream),
@@ -459,18 +456,15 @@ impl InnerPostgresConnection {
459456
path.shift_char();
460457
args.push((~"database", path));
461458
}
462-
if_ok_pg_conn!(conn.write_messages([StartupMessage {
459+
try_pg_conn!(conn.write_messages([StartupMessage {
463460
version: message::PROTOCOL_VERSION,
464461
parameters: args.as_slice()
465462
}]));
466463

467-
match conn.handle_auth(user) {
468-
Err(err) => return Err(err),
469-
Ok(()) => {}
470-
}
464+
try!(conn.handle_auth(user));
471465

472466
loop {
473-
match if_ok_pg_conn!(conn.read_message()) {
467+
match try_pg_conn!(conn.read_message()) {
474468
BackendKeyData { process_id, secret_key } => {
475469
conn.cancel_data.process_id = process_id;
476470
conn.cancel_data.secret_key = secret_key;
@@ -488,15 +482,15 @@ impl InnerPostgresConnection {
488482
fn write_messages(&mut self, messages: &[FrontendMessage]) -> IoResult<()> {
489483
assert!(!self.desynchronized);
490484
for message in messages.iter() {
491-
if_ok_desync!(self.stream.write_message(message));
485+
try_desync!(self.stream.write_message(message));
492486
}
493-
Ok(if_ok_desync!(self.stream.flush()))
487+
Ok(try_desync!(self.stream.flush()))
494488
}
495489

496490
fn read_message(&mut self) -> IoResult<BackendMessage> {
497491
assert!(!self.desynchronized);
498492
loop {
499-
match if_ok_desync!(self.stream.read_message()) {
493+
match try_desync!(self.stream.read_message()) {
500494
NoticeResponse { fields } =>
501495
self.notice_handler.handle(PostgresDbError::new(fields)),
502496
NotificationResponse { pid, channel, payload } =>
@@ -514,14 +508,14 @@ impl InnerPostgresConnection {
514508

515509
fn handle_auth(&mut self, user: UserInfo) ->
516510
Result<(), PostgresConnectError> {
517-
match if_ok_pg_conn!(self.read_message()) {
511+
match try_pg_conn!(self.read_message()) {
518512
AuthenticationOk => return Ok(()),
519513
AuthenticationCleartextPassword => {
520514
let pass = match user.pass {
521515
Some(pass) => pass,
522516
None => return Err(MissingPassword)
523517
};
524-
if_ok_pg_conn!(self.write_messages([PasswordMessage { password: pass }]));
518+
try_pg_conn!(self.write_messages([PasswordMessage { password: pass }]));
525519
}
526520
AuthenticationMD5Password { salt } => {
527521
let UserInfo { user, pass } = user;
@@ -537,7 +531,7 @@ impl InnerPostgresConnection {
537531
hasher.update(output.as_bytes());
538532
hasher.update(salt);
539533
let output = "md5" + hasher.final().to_hex();
540-
if_ok_pg_conn!(self.write_messages([PasswordMessage {
534+
try_pg_conn!(self.write_messages([PasswordMessage {
541535
password: output.as_slice()
542536
}]));
543537
}
@@ -550,7 +544,7 @@ impl InnerPostgresConnection {
550544
_ => unreachable!()
551545
}
552546

553-
match if_ok_pg_conn!(self.read_message()) {
547+
match try_pg_conn!(self.read_message()) {
554548
AuthenticationOk => Ok(()),
555549
ErrorResponse { fields } =>
556550
Err(PgConnectDbError(PostgresDbError::new(fields))),
@@ -569,7 +563,7 @@ impl InnerPostgresConnection {
569563
self.next_stmt_id += 1;
570564

571565
let types = [];
572-
if_ok_pg!(self.write_messages([
566+
try_pg!(self.write_messages([
573567
Parse {
574568
name: stmt_name,
575569
query: query,
@@ -581,7 +575,7 @@ impl InnerPostgresConnection {
581575
},
582576
Sync]));
583577

584-
match if_ok_pg!(self.read_message()) {
578+
match try_pg!(self.read_message()) {
585579
ParseComplete => {}
586580
ErrorResponse { fields } => {
587581
try!(self.wait_for_ready());
@@ -590,13 +584,13 @@ impl InnerPostgresConnection {
590584
_ => unreachable!()
591585
}
592586

593-
let mut param_types: Vec<PostgresType> = match if_ok_pg!(self.read_message()) {
587+
let mut param_types: Vec<PostgresType> = match try_pg!(self.read_message()) {
594588
ParameterDescription { types } =>
595589
types.iter().map(|ty| PostgresType::from_oid(*ty)).collect(),
596590
_ => unreachable!()
597591
};
598592

599-
let mut result_desc: Vec<ResultDescription> = match if_ok_pg!(self.read_message()) {
593+
let mut result_desc: Vec<ResultDescription> = match try_pg!(self.read_message()) {
600594
RowDescription { descriptions } =>
601595
descriptions.move_iter().map(|desc| {
602596
stmt::make_ResultDescription(desc)
@@ -653,7 +647,7 @@ impl InnerPostgresConnection {
653647
}
654648

655649
fn wait_for_ready(&mut self) -> Result<(), PostgresError> {
656-
match if_ok_pg!(self.read_message()) {
650+
match try_pg!(self.read_message()) {
657651
ReadyForQuery { .. } => Ok(()),
658652
_ => unreachable!()
659653
}
@@ -662,11 +656,11 @@ impl InnerPostgresConnection {
662656
fn quick_query(&mut self, query: &str)
663657
-> Result<Vec<Vec<Option<~str>>>, PostgresError> {
664658
check_desync!(self);
665-
if_ok_pg!(self.write_messages([Query { query: query }]));
659+
try_pg!(self.write_messages([Query { query: query }]));
666660

667661
let mut result = Vec::new();
668662
loop {
669-
match if_ok_pg!(self.read_message()) {
663+
match try_pg!(self.read_message()) {
670664
ReadyForQuery { .. } => break,
671665
DataRow { row } =>
672666
result.push(row.move_iter().map(|opt|
@@ -684,7 +678,7 @@ impl InnerPostgresConnection {
684678

685679
fn finish_inner(&mut self) -> Result<(), PostgresError> {
686680
check_desync!(self);
687-
Ok(if_ok_pg!(self.write_messages([Terminate])))
681+
Ok(try_pg!(self.write_messages([Terminate])))
688682
}
689683
}
690684

src/stmt.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,14 @@ impl<'conn> Drop for NormalPostgresStatement<'conn> {
153153
impl<'conn> NormalPostgresStatement<'conn> {
154154
fn finish_inner(&mut self) -> Result<(), PostgresError> {
155155
check_desync!(self.conn);
156-
if_ok_pg!(self.conn.write_messages([
156+
try_pg!(self.conn.write_messages([
157157
Close {
158158
variant: 'S' as u8,
159159
name: self.name.as_slice()
160160
},
161161
Sync]));
162162
loop {
163-
match if_ok_pg!(self.conn.read_message()) {
163+
match try_pg!(self.conn.read_message()) {
164164
ReadyForQuery { .. } => break,
165165
ErrorResponse { fields } => {
166166
try!(self.conn.wait_for_ready());
@@ -189,7 +189,7 @@ impl<'conn> NormalPostgresStatement<'conn> {
189189
desc.ty.result_format() as i16
190190
}).collect();
191191

192-
if_ok_pg!(self.conn.write_messages([
192+
try_pg!(self.conn.write_messages([
193193
Bind {
194194
portal: portal_name,
195195
statement: self.name.as_slice(),
@@ -203,7 +203,7 @@ impl<'conn> NormalPostgresStatement<'conn> {
203203
},
204204
Sync]));
205205

206-
match if_ok_pg!(self.conn.read_message()) {
206+
match try_pg!(self.conn.read_message()) {
207207
BindComplete => Ok(()),
208208
ErrorResponse { fields } => {
209209
try!(self.conn.wait_for_ready());
@@ -251,7 +251,7 @@ impl<'conn> PostgresStatement for NormalPostgresStatement<'conn> {
251251

252252
let num;
253253
loop {
254-
match if_ok_pg!(self.conn.read_message()) {
254+
match try_pg!(self.conn.read_message()) {
255255
DataRow { .. } => {}
256256
ErrorResponse { fields } => {
257257
try!(self.conn.wait_for_ready());
@@ -402,14 +402,14 @@ impl<'stmt> Drop for PostgresResult<'stmt> {
402402
impl<'stmt> PostgresResult<'stmt> {
403403
fn finish_inner(&mut self) -> Result<(), PostgresError> {
404404
check_desync!(self.stmt.conn);
405-
if_ok_pg!(self.stmt.conn.write_messages([
405+
try_pg!(self.stmt.conn.write_messages([
406406
Close {
407407
variant: 'P' as u8,
408408
name: self.name.as_slice()
409409
},
410410
Sync]));
411411
loop {
412-
match if_ok_pg!(self.stmt.conn.read_message()) {
412+
match try_pg!(self.stmt.conn.read_message()) {
413413
ReadyForQuery { .. } => break,
414414
ErrorResponse { fields } => {
415415
try!(self.stmt.conn.wait_for_ready());
@@ -423,7 +423,7 @@ impl<'stmt> PostgresResult<'stmt> {
423423

424424
fn read_rows(&mut self) -> Result<(), PostgresError> {
425425
loop {
426-
match if_ok_pg!(self.stmt.conn.read_message()) {
426+
match try_pg!(self.stmt.conn.read_message()) {
427427
EmptyQueryResponse |
428428
CommandComplete { .. } => {
429429
self.more_rows = false;
@@ -441,7 +441,7 @@ impl<'stmt> PostgresResult<'stmt> {
441441
}
442442

443443
fn execute(&mut self) -> Result<(), PostgresError> {
444-
if_ok_pg!(self.stmt.conn.write_messages([
444+
try_pg!(self.stmt.conn.write_messages([
445445
Execute {
446446
portal: self.name,
447447
max_rows: self.row_limit as i32

0 commit comments

Comments
 (0)