Skip to content

Commit 9891f59

Browse files
committed
Switch over to new format + clean up matches
1 parent 7990809 commit 9891f59

2 files changed

Lines changed: 44 additions & 43 deletions

File tree

src/lib.rs

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,20 @@ macro_rules! match_read_message(
4343
)
4444
)
4545

46+
macro_rules! match_read_message_or_fail(
47+
($conn:expr, { $($($p:pat)|+ => $e:expr),+ }) => (
48+
match_read_message!($conn, {
49+
$(
50+
$($p)|+ => $e
51+
),+ ,
52+
resp => fail2!("Bad response: {}", resp.to_str())
53+
})
54+
)
55+
)
56+
4657
fn handle_notice_response(fields: ~[(u8, ~str)]) {
4758
let err = PostgresDbError::new(fields);
48-
info!("%s: %s", err.severity, err.message);
59+
info2!("{}: {}", err.severity, err.message);
4960
}
5061

5162
#[deriving(ToStr)]
@@ -123,7 +134,7 @@ impl PostgresConnection {
123134
pub fn connect(url: &str) -> PostgresConnection {
124135
match PostgresConnection::try_connect(url) {
125136
Ok(conn) => conn,
126-
Err(err) => fail!("Failed to connect: %s", err.to_str())
137+
Err(err) => fail2!("Failed to connect: {}", err.to_str())
127138
}
128139
}
129140

@@ -176,12 +187,11 @@ impl PostgresConnection {
176187
}
177188
178189
loop {
179-
match_read_message!(conn, {
190+
match_read_message_or_fail!(conn, {
180191
ParameterStatus { parameter, value } =>
181192
info!("Parameter %s = %s", parameter, value),
182193
BackendKeyData {_} => (),
183-
ReadyForQuery {_} => break,
184-
resp => fail!("Bad response: %?", resp.to_str())
194+
ReadyForQuery {_} => break
185195
})
186196
}
187197
@@ -211,7 +221,7 @@ impl PostgresConnection {
211221
}
212222
213223
fn handle_auth(&self, user: UserInfo) -> Option<PostgresConnectError> {
214-
match_read_message!(self, {
224+
match_read_message_or_fail!(self, {
215225
AuthenticationOk => return None,
216226
AuthenticationCleartextPassword => {
217227
let pass = match user.pass {
@@ -237,23 +247,21 @@ impl PostgresConnection {
237247
self.write_message(&PasswordMessage {
238248
password: output.as_slice()
239249
});
240-
},
241-
resp => fail!("Bad response: %?", resp.to_str())
250+
}
242251
})
243252
244-
match_read_message!(self, {
253+
match_read_message_or_fail!(self, {
245254
AuthenticationOk => None,
246255
ErrorResponse { fields } =>
247-
Some(DbError(PostgresDbError::new(fields))),
248-
resp => fail!("Bad response: %?", resp.to_str())
256+
Some(DbError(PostgresDbError::new(fields)))
249257
})
250258
}
251259
252260
pub fn prepare<'a>(&'a self, query: &str) -> PostgresStatement<'a> {
253261
match self.try_prepare(query) {
254262
Ok(stmt) => stmt,
255-
Err(err) => fail!("Error preparing \"%s\": %s", query,
256-
err.to_str())
263+
Err(err) => fail2!("Error preparing \"{}\": {}", query,
264+
err.to_str())
257265
}
258266
}
259267
@@ -276,24 +284,21 @@ impl PostgresConnection {
276284
},
277285
&Sync]);
278286
279-
match_read_message!(self, {
287+
match_read_message_or_fail!(self, {
280288
ParseComplete => (),
281289
ErrorResponse { fields } => {
282290
self.wait_for_ready();
283291
return Err(PostgresDbError::new(fields));
284-
},
285-
resp => fail!("Bad response: %?", resp.to_str())
292+
}
286293
})
287294
288-
let param_types = match_read_message!(self, {
289-
ParameterDescription { types } => types,
290-
resp => fail!("Bad response: %?", resp.to_str())
295+
let param_types = match_read_message_or_fail!(self, {
296+
ParameterDescription { types } => types
291297
});
292298
293-
let result_desc = match_read_message!(self, {
299+
let result_desc = match_read_message_or_fail!(self, {
294300
RowDescription { descriptions } => descriptions,
295-
NoData => ~[],
296-
resp => fail!("Bad response: %?", resp.to_str())
301+
NoData => ~[]
297302
});
298303
299304
self.wait_for_ready();
@@ -328,7 +333,7 @@ impl PostgresConnection {
328333
pub fn update(&self, query: &str, params: &[&ToSql]) -> uint {
329334
match self.try_update(query, params) {
330335
Ok(res) => res,
331-
Err(err) => fail!("Error running update: %s", err.to_str())
336+
Err(err) => fail2!("Error running update: {}", err.to_str())
332337
}
333338
}
334339
@@ -346,16 +351,15 @@ impl PostgresConnection {
346351
match_read_message!(self, {
347352
ReadyForQuery {_} => break,
348353
ErrorResponse { fields } =>
349-
fail!("Error: %s", PostgresDbError::new(fields).to_str()),
354+
fail2!("Error: {}", PostgresDbError::new(fields).to_str()),
350355
_ => ()
351356
})
352357
}
353358
}
354359
355360
fn wait_for_ready(&self) {
356-
match_read_message!(self, {
357-
ReadyForQuery {_} => (),
358-
resp => fail!("Bad response: %?", resp.to_str())
361+
match_read_message_or_fail!(self, {
362+
ReadyForQuery {_} => ()
359363
})
360364
}
361365
}
@@ -461,20 +465,19 @@ impl<'self> PostgresStatement<'self> {
461465
},
462466
&Sync]);
463467
464-
match_read_message!(self.conn, {
468+
match_read_message_or_fail!(self.conn, {
465469
BindComplete => None,
466470
ErrorResponse { fields } => {
467471
self.conn.wait_for_ready();
468472
Some(PostgresDbError::new(fields))
469-
},
470-
resp => fail!("Bad response: %?", resp.to_str())
473+
}
471474
})
472475
}
473476
474477
pub fn update(&self, params: &[&ToSql]) -> uint {
475478
match self.try_update(params) {
476479
Ok(count) => count,
477-
Err(err) => fail!("Error running update: %s", err.to_str())
480+
Err(err) => fail2!("Error running update: {}", err.to_str())
478481
}
479482
}
480483
@@ -489,7 +492,7 @@ impl<'self> PostgresStatement<'self> {
489492
490493
let num;
491494
loop {
492-
match_read_message!(self.conn, {
495+
match_read_message_or_fail!(self.conn, {
493496
CommandComplete { tag } => {
494497
let s = tag.split_iter(' ').last().unwrap();
495498
num = match FromStr::from_str(s) {
@@ -507,8 +510,7 @@ impl<'self> PostgresStatement<'self> {
507510
ErrorResponse { fields } => {
508511
self.conn.wait_for_ready();
509512
return Err(PostgresDbError::new(fields));
510-
},
511-
resp => fail!("Bad response: %?", resp.to_str())
513+
}
512514
})
513515
}
514516
self.conn.wait_for_ready();
@@ -520,7 +522,7 @@ impl<'self> PostgresStatement<'self> {
520522
-> PostgresResult<'self> {
521523
match self.try_query(params) {
522524
Ok(result) => result,
523-
Err(err) => fail!("Error running query: %s", err.to_str())
525+
Err(err) => fail2!("Error running query: {}", err.to_str())
524526
}
525527
}
526528
@@ -535,13 +537,12 @@ impl<'self> PostgresStatement<'self> {
535537
536538
let mut data = ~[];
537539
loop {
538-
match_read_message!(self.conn, {
540+
match_read_message_or_fail!(self.conn, {
539541
EmptyQueryResponse |
540542
CommandComplete {_} => {
541543
break;
542544
},
543-
DataRow { row } => data.push(row),
544-
resp => fail!("Bad response: %?", resp.to_str())
545+
DataRow { row } => data.push(row)
545546
})
546547
}
547548
self.conn.wait_for_ready();
@@ -618,7 +619,7 @@ impl<'self> RowIndex for &'self str {
618619
fn idx(&self, stmt: &PostgresStatement) -> uint {
619620
match stmt.find_col_named(*self) {
620621
Some(idx) => idx,
621-
None => fail!("No column with name %s", *self)
622+
None => fail2!("No column with name {}", *self)
622623
}
623624
}
624625
}

src/message.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub trait WriteMessage {
120120

121121
impl<W: Writer> WriteMessage for W {
122122
fn write_message(&mut self, message: &FrontendMessage) {
123-
debug!("Writing message %?", message);
123+
debug2!("Writing message {:?}", message);
124124
let mut buf = MemWriter::new();
125125
let mut ident = None;
126126

@@ -268,10 +268,10 @@ impl<R: Reader> ReadMessage for R {
268268
't' => read_parameter_description(&mut buf),
269269
'T' => read_row_description(&mut buf),
270270
'Z' => ReadyForQuery { state: buf.read_u8_() },
271-
ident => fail!("Unknown message identifier `%c`", ident)
271+
ident => fail2!("Unknown message identifier `{}`", ident)
272272
};
273273
assert!(buf.eof());
274-
debug!("Read message %?", ret);
274+
debug2!("Read message {:?}", ret);
275275
ret
276276
}
277277
}
@@ -310,7 +310,7 @@ fn read_auth_message(buf: &mut MemReader) -> BackendMessage {
310310
0 => AuthenticationOk,
311311
3 => AuthenticationCleartextPassword,
312312
5 => AuthenticationMD5Password { salt: buf.read_bytes(4) },
313-
val => fail!("Unknown Authentication identifier `%?`", val)
313+
val => fail2!("Unknown Authentication identifier `{}`", val)
314314
}
315315
}
316316

0 commit comments

Comments
 (0)