Start using if let

This commit is contained in:
Steven Fackler 2014-10-13 21:12:25 -07:00
parent abd60ef1cf
commit 774fd93d80

View File

@ -51,7 +51,7 @@
//! } //! }
//! ``` //! ```
#![doc(html_root_url="http://www.rust-ci.org/sfackler/rust-postgres/doc")] #![doc(html_root_url="http://www.rust-ci.org/sfackler/rust-postgres/doc")]
#![feature(macro_rules, struct_variant, phase, unsafe_destructor, slicing_syntax)] #![feature(macro_rules, struct_variant, phase, unsafe_destructor, slicing_syntax, if_let)]
#![warn(missing_doc)] #![warn(missing_doc)]
extern crate collections; extern crate collections;
@ -382,10 +382,7 @@ impl InnerPostgresConnection {
.. ..
} = params; } = params;
let user = match user { let user = try!(user.ok_or(MissingUser));
Some(user) => user,
None => return Err(MissingUser),
};
let mut conn = InnerPostgresConnection { let mut conn = InnerPostgresConnection {
stream: BufferedStream::new(stream), stream: BufferedStream::new(stream),
@ -447,9 +444,8 @@ impl InnerPostgresConnection {
loop { loop {
match try_desync!(self, self.stream.read_message()) { match try_desync!(self, self.stream.read_message()) {
NoticeResponse { fields } => { NoticeResponse { fields } => {
match PostgresDbError::new_raw(fields) { if let Ok(err) = PostgresDbError::new_raw(fields) {
Ok(err) => self.notice_handler.handle(err), self.notice_handler.handle(err);
Err(()) => {}
} }
} }
NotificationResponse { pid, channel, payload } => { NotificationResponse { pid, channel, payload } => {
@ -471,19 +467,13 @@ impl InnerPostgresConnection {
match try_pg_conn!(self.read_message_()) { match try_pg_conn!(self.read_message_()) {
AuthenticationOk => return Ok(()), AuthenticationOk => return Ok(()),
AuthenticationCleartextPassword => { AuthenticationCleartextPassword => {
let pass = match user.password { let pass = try!(user.password.ok_or(MissingPassword));
Some(pass) => pass,
None => return Err(MissingPassword)
};
try_pg_conn!(self.write_messages([PasswordMessage { try_pg_conn!(self.write_messages([PasswordMessage {
password: pass[], password: pass[],
}])); }]));
} }
AuthenticationMD5Password { salt } => { AuthenticationMD5Password { salt } => {
let pass = match user.password { let pass = try!(user.password.ok_or(MissingPassword));
Some(pass) => pass,
None => return Err(MissingPassword)
};
let hasher = Hasher::new(MD5); let hasher = Hasher::new(MD5);
hasher.update(pass.as_bytes()); hasher.update(pass.as_bytes());
hasher.update(user.user.as_bytes()); hasher.update(user.user.as_bytes());
@ -651,9 +641,8 @@ impl InnerPostgresConnection {
} }
fn get_type_name(&mut self, oid: Oid) -> PostgresResult<String> { fn get_type_name(&mut self, oid: Oid) -> PostgresResult<String> {
match self.unknown_types.find(&oid) { if let Some(name) = self.unknown_types.find(&oid) {
Some(name) => return Ok(name.clone()), return Ok(name.clone());
None => {}
} }
let name = try!(self.quick_query(format!("SELECT typname FROM pg_type \ let name = try!(self.quick_query(format!("SELECT typname FROM pg_type \
WHERE oid={}", oid)[])) WHERE oid={}", oid)[]))
@ -1169,8 +1158,7 @@ impl<'conn> PostgresStatement<'conn> {
} }
let mut values = vec![]; let mut values = vec![];
for (param, ty) in params.iter().zip(self.param_types.iter()) { for (param, ty) in params.iter().zip(self.param_types.iter()) {
let value = try!(param.to_sql(ty)); values.push(try!(param.to_sql(ty)));
values.push(value);
}; };
try_pg!(conn.write_messages([ try_pg!(conn.write_messages([
@ -1427,9 +1415,8 @@ impl<'stmt> PostgresRows<'stmt> {
fn try_next(&mut self) -> Option<PostgresResult<PostgresRow<'stmt>>> { fn try_next(&mut self) -> Option<PostgresResult<PostgresRow<'stmt>>> {
if self.data.is_empty() && self.more_rows { if self.data.is_empty() && self.more_rows {
match self.execute() { if let Err(err) = self.execute() {
Ok(()) => {} return Some(Err(err));
Err(err) => return Some(Err(err))
} }
} }
@ -1476,10 +1463,7 @@ impl<'stmt> PostgresRow<'stmt> {
/// Returns an `Error` value if the index does not reference a column or /// Returns an `Error` value if the index does not reference a column or
/// the return type is not compatible with the Postgres type. /// the return type is not compatible with the Postgres type.
pub fn get_opt<I, T>(&self, idx: I) -> PostgresResult<T> where I: RowIndex, T: FromSql { pub fn get_opt<I, T>(&self, idx: I) -> PostgresResult<T> where I: RowIndex, T: FromSql {
let idx = match idx.idx(self.stmt) { let idx = try!(idx.idx(self.stmt).ok_or(PgInvalidColumn));
Some(idx) => idx,
None => return Err(PgInvalidColumn)
};
FromSql::from_sql(&self.stmt.result_desc[idx].ty, &self.data[idx]) FromSql::from_sql(&self.stmt.result_desc[idx].ty, &self.data[idx])
} }