Use From for convertin codes to error kinds

This commit is contained in:
Ivan Ukhov 2015-06-08 18:19:15 -04:00
parent 15539354b4
commit d036ddc071
2 changed files with 11 additions and 10 deletions

View File

@ -1,4 +1,3 @@
use libc::c_int;
use raw; use raw;
use std::convert::{From, Into}; use std::convert::{From, Into};
use std::fmt::{self, Display, Formatter}; use std::fmt::{self, Display, Formatter};
@ -19,10 +18,12 @@ macro_rules! declare(
Unknown, Unknown,
} }
pub fn kind_from_code(code: c_int) -> ErrorKind { impl From<isize> for ErrorKind {
match code { fn from(code: isize) -> ErrorKind {
$(raw::$right => ErrorKind::$left,)* match code as ::libc::c_int {
_ => ErrorKind::Unknown, $(raw::$right => ErrorKind::$left,)*
_ => ErrorKind::Unknown,
}
} }
} }
); );
@ -105,7 +106,7 @@ pub fn last(raw: *mut raw::sqlite3) -> Option<Error> {
return None; return None;
} }
Some(Error { Some(Error {
kind: kind_from_code(code), kind: ErrorKind::from(code as isize),
message: Some(c_str_to_string!(message)), message: Some(c_str_to_string!(message)),
}) })
} }
@ -113,13 +114,13 @@ pub fn last(raw: *mut raw::sqlite3) -> Option<Error> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{ErrorKind, kind_from_code}; use super::ErrorKind;
#[test] #[test]
fn fmt() { fn fmt() {
assert_eq!(format!("{}", ErrorKind::OK), assert_eq!(format!("{}", ErrorKind::OK),
String::from("SQLite result code 0")); String::from("SQLite result code 0"));
assert_eq!(format!("{}", kind_from_code(777)), assert_eq!(format!("{}", ErrorKind::from(777)),
String::from("an unknown SQLite result code")); String::from("an unknown SQLite result code"));
} }
} }

View File

@ -14,7 +14,7 @@ macro_rules! failure(
($database:expr, $code:expr) => ( ($database:expr, $code:expr) => (
match ::error::last($database) { match ::error::last($database) {
Some(error) => return Err(error), Some(error) => return Err(error),
None => return Err(::Error::from(::error::kind_from_code($code))), None => return Err(::Error::from(::ErrorKind::from($code as isize))),
} }
); );
); );
@ -29,7 +29,7 @@ macro_rules! success(
($result:expr) => ( ($result:expr) => (
match $result { match $result {
::raw::SQLITE_OK => {}, ::raw::SQLITE_OK => {},
code => return Err(::Error::from(::error::kind_from_code(code))), code => return Err(::Error::from(::ErrorKind::from(code as isize))),
} }
); );
); );