2016-05-09 10:11:13 +00:00
|
|
|
use std::error::Error;
|
|
|
|
use std::clone::Clone;
|
|
|
|
use std::fmt::Error as FmtError;
|
2016-06-30 17:06:26 +00:00
|
|
|
use std::fmt::{Display, Formatter};
|
2016-05-09 10:11:13 +00:00
|
|
|
|
2016-05-09 15:22:40 +00:00
|
|
|
/// Enum of Error Types, as of now we have two:
|
|
|
|
/// * ConversionError: for Errors concerning conversion failures from task_hookrs::task::Task to
|
|
|
|
/// libimagtodo::task::Task. unused.
|
|
|
|
/// * StoreError: For Errors thrown by functions of the Store/structs relates to the Store
|
2016-05-09 10:11:13 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
pub enum TodoErrorKind {
|
|
|
|
ConversionError,
|
2016-05-09 15:22:40 +00:00
|
|
|
StoreError,
|
2016-05-09 10:11:13 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 15:22:40 +00:00
|
|
|
/// Maps a TodoErrorKind to a String
|
2016-05-09 10:11:13 +00:00
|
|
|
fn todo_error_type_as_str(e: &TodoErrorKind) -> &'static str {
|
|
|
|
match e {
|
|
|
|
&TodoErrorKind::ConversionError => "Conversion Error",
|
2016-05-09 15:22:40 +00:00
|
|
|
&TodoErrorKind::StoreError => "Store Error",
|
2016-05-09 10:11:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for TodoErrorKind {
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
|
|
|
try!(write!(fmt, "{}", todo_error_type_as_str(self)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:22:40 +00:00
|
|
|
/// Error struct for the imag-todo module
|
2016-05-09 10:11:13 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct TodoError {
|
|
|
|
err_type : TodoErrorKind,
|
|
|
|
cause : Option<Box<Error>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TodoError {
|
2016-05-09 15:22:40 +00:00
|
|
|
/// Creates a new TodoError, with TodoErrorKind errtype and an optional cause
|
2016-05-09 10:11:13 +00:00
|
|
|
pub fn new(errtype : TodoErrorKind, cause : Option<Box<Error>>) -> TodoError {
|
|
|
|
TodoError {
|
|
|
|
err_type : errtype,
|
|
|
|
cause : cause,
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 15:22:40 +00:00
|
|
|
/// Returns the error type (TodoErrorKind)
|
2016-05-09 10:11:13 +00:00
|
|
|
pub fn err_type(&self) -> TodoErrorKind {
|
|
|
|
self.err_type.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for TodoError {
|
|
|
|
fn fmt(&self, fmt : &mut Formatter) -> Result<(), FmtError> {
|
|
|
|
try!(write!(fmt, "[{}]", todo_error_type_as_str(&self.err_type.clone())));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for TodoError {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
todo_error_type_as_str(&self.err_type.clone())
|
|
|
|
}
|
|
|
|
fn cause(&self) -> Option<&Error> {
|
|
|
|
self.cause.as_ref().map(|e| &**e)
|
|
|
|
}
|
|
|
|
}
|