2015-05-29 14:15:01 -04:00
|
|
|
use raw;
|
2015-05-29 14:04:39 -04:00
|
|
|
use std::convert::{From, Into};
|
2015-06-01 11:20:44 -04:00
|
|
|
use std::fmt::{self, Display, Formatter};
|
2015-05-29 14:04:39 -04:00
|
|
|
|
2015-06-08 17:43:31 -04:00
|
|
|
use ResultCode;
|
2015-05-29 14:15:01 -04:00
|
|
|
|
2015-05-29 13:50:05 -04:00
|
|
|
/// An error.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Error {
|
|
|
|
pub code: ResultCode,
|
|
|
|
pub message: Option<String>,
|
|
|
|
}
|
2015-05-29 14:04:39 -04:00
|
|
|
|
|
|
|
impl<T> From<T> for Error where T: Into<String> {
|
|
|
|
#[inline]
|
|
|
|
fn from(message: T) -> Error {
|
|
|
|
Error {
|
|
|
|
code: ResultCode::Error,
|
|
|
|
message: Some(message.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ResultCode> for Error {
|
|
|
|
#[inline]
|
|
|
|
fn from(code: ResultCode) -> Error {
|
|
|
|
Error {
|
|
|
|
code: code,
|
|
|
|
message: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-01 11:20:44 -04:00
|
|
|
|
|
|
|
impl Display for Error {
|
|
|
|
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
|
|
|
|
match self.message {
|
|
|
|
Some(ref message) => Display::fmt(message, formatter),
|
|
|
|
None => Display::fmt(&self.code, formatter),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-08 17:43:31 -04:00
|
|
|
|
|
|
|
pub fn last(raw: *mut raw::sqlite3) -> Option<Error> {
|
|
|
|
unsafe {
|
|
|
|
let code = raw::sqlite3_errcode(raw);
|
|
|
|
if code == raw::SQLITE_OK {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let message = raw::sqlite3_errmsg(raw);
|
|
|
|
if message.is_null() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(Error {
|
|
|
|
code: ::result::code_from_raw(code),
|
|
|
|
message: Some(c_str_to_string!(message)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|