136 lines
3.4 KiB
Rust
Raw Normal View History

2015-06-12 14:23:18 -04:00
use ffi;
2015-05-29 14:04:39 -04:00
use std::convert::{From, Into};
2015-07-04 08:42:03 -04:00
use std::{error, fmt};
2015-05-29 14:04:39 -04:00
/// An error.
#[derive(Debug)]
pub struct Error {
pub kind: ErrorKind,
pub message: Option<String>,
}
2015-05-29 14:04:39 -04:00
macro_rules! declare(
($($left:ident => $right:ident,)*) => (
/// An error kind.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ErrorKind {
2015-06-12 14:23:18 -04:00
$($left = ffi::$right as isize,)*
Unknown,
}
impl From<isize> for ErrorKind {
fn from(code: isize) -> ErrorKind {
match code as ::libc::c_int {
2015-06-12 14:23:18 -04:00
$(ffi::$right => ErrorKind::$left,)*
_ => ErrorKind::Unknown,
}
}
}
);
);
declare!(
Abort => SQLITE_ABORT,
Authorization => SQLITE_AUTH,
Busy => SQLITE_BUSY,
CantOpen => SQLITE_CANTOPEN,
Constraint => SQLITE_CONSTRAINT,
Corruption => SQLITE_CORRUPT,
Done => SQLITE_DONE,
Empty => SQLITE_EMPTY,
Error => SQLITE_ERROR,
Format => SQLITE_FORMAT,
Full => SQLITE_FULL,
Internal => SQLITE_INTERNAL,
Interruption => SQLITE_INTERRUPT,
IOError => SQLITE_IOERR,
Locked => SQLITE_LOCKED,
Mismatch => SQLITE_MISMATCH,
Misuse => SQLITE_MISUSE,
NoLargeFileSupport => SQLITE_NOLFS,
NoMemory => SQLITE_NOMEM,
NotDatabase => SQLITE_NOTADB,
NotFound => SQLITE_NOTFOUND,
Notice => SQLITE_NOTICE,
OK => SQLITE_OK,
Permission => SQLITE_PERM,
Protocol => SQLITE_PROTOCOL,
Range => SQLITE_RANGE,
ReadOnly => SQLITE_READONLY,
Row => SQLITE_ROW,
Schema => SQLITE_SCHEMA,
TooBig => SQLITE_TOOBIG,
Warning => SQLITE_WARNING,
);
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 { kind: ErrorKind::Unknown, message: Some(message.into()) }
2015-05-29 14:04:39 -04:00
}
}
impl From<ErrorKind> for Error {
2015-05-29 14:04:39 -04:00
#[inline]
fn from(kind: ErrorKind) -> Error {
Error { kind: kind, message: None }
2015-05-29 14:04:39 -04:00
}
}
2015-07-04 08:42:03 -04:00
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self.message {
2015-07-04 08:42:03 -04:00
Some(ref message) => message.fmt(formatter),
_ => self.kind.fmt(formatter),
}
}
}
2015-07-04 08:42:03 -04:00
impl error::Error for Error {
fn description(&self) -> &str {
match self.message {
Some(ref message) => message,
_ => "an SQLite error",
}
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match *self {
ErrorKind::Unknown => write!(formatter, "an unknown SQLite result code"),
_ => write!(formatter, "SQLite result code {}", *self as isize),
}
}
}
2015-06-08 17:43:31 -04:00
2015-06-12 14:23:18 -04:00
pub fn last(raw: *mut ffi::sqlite3) -> Option<Error> {
2015-06-08 17:43:31 -04:00
unsafe {
2015-06-12 14:23:18 -04:00
let code = ffi::sqlite3_errcode(raw);
if code == ffi::SQLITE_OK {
2015-06-08 17:43:31 -04:00
return None;
}
2015-06-12 14:23:18 -04:00
let message = ffi::sqlite3_errmsg(raw);
2015-06-08 17:43:31 -04:00
if message.is_null() {
return None;
}
Some(Error {
kind: ErrorKind::from(code as isize),
2015-06-08 17:43:31 -04:00
message: Some(c_str_to_string!(message)),
})
}
}
#[cfg(test)]
mod tests {
use super::ErrorKind;
#[test]
fn fmt() {
assert_eq!(format!("{}", ErrorKind::OK),
String::from("SQLite result code 0"));
assert_eq!(format!("{}", ErrorKind::from(777)),
String::from("an unknown SQLite result code"));
}
}