Implement std::error::Error for Error

This commit is contained in:
Ivan Ukhov 2015-07-04 08:42:03 -04:00
parent fa4ceac00a
commit 626c022106

View File

@ -1,6 +1,6 @@
use ffi; use ffi;
use std::convert::{From, Into}; use std::convert::{From, Into};
use std::fmt::{self, Display, Formatter}; use std::{error, fmt};
/// An error. /// An error.
#[derive(Debug)] #[derive(Debug)]
@ -77,17 +77,26 @@ impl From<ErrorKind> for Error {
} }
} }
impl Display for Error { impl fmt::Display for Error {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self.message { match self.message {
Some(ref message) => Display::fmt(message, formatter), Some(ref message) => message.fmt(formatter),
None => Display::fmt(&self.kind, formatter), _ => self.kind.fmt(formatter),
} }
} }
} }
impl Display for ErrorKind { impl error::Error for Error {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { 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 { match *self {
ErrorKind::Unknown => write!(formatter, "an unknown SQLite result code"), ErrorKind::Unknown => write!(formatter, "an unknown SQLite result code"),
_ => write!(formatter, "SQLite result code {}", *self as isize), _ => write!(formatter, "SQLite result code {}", *self as isize),