introduce ConversionError in error.rs and add trait for task_hookrs::task::Task to convert it to a libimagtodo::task::Task

This commit is contained in:
mario 2016-05-09 12:11:13 +02:00
parent df6f41942b
commit 26ec9710cc
3 changed files with 75 additions and 3 deletions

60
libimagtodo/src/error.rs Normal file
View file

@ -0,0 +1,60 @@
use std::error::Error;
use std::clone::Clone;
use std::fmt::Error as FmtError;
use std::fmt::{Debug, Display, Formatter};
use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TodoErrorKind {
ConversionError,
}
fn todo_error_type_as_str(e: &TodoErrorKind) -> &'static str {
match e {
&TodoErrorKind::ConversionError => "Conversion Error",
}
}
impl Display for TodoErrorKind {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
try!(write!(fmt, "{}", todo_error_type_as_str(self)));
Ok(())
}
}
#[derive(Debug)]
pub struct TodoError {
err_type : TodoErrorKind,
cause : Option<Box<Error>>,
}
impl TodoError {
pub fn new(errtype : TodoErrorKind, cause : Option<Box<Error>>) -> TodoError {
TodoError {
err_type : errtype,
cause : cause,
}
}
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)
}
}

View file

@ -6,6 +6,7 @@ extern crate task_hookrs;
module_entry_path_mod!("todo", "0.1.0");
pub mod error;
pub mod task;
pub mod delete;
pub mod read;

View file

@ -5,12 +5,18 @@ use task_hookrs::task::Task as TTask;
use libimagstore::store::FileLockEntry;
use error::{TodoError, TodoErrorKind};
pub trait IntoTask<'a> {
fn into_filelockentry(self) -> Result<Task<'a>, TodoError>;
}
#[derive(Debug)]
pub struct Task<'a> {
flentry : FileLockEntry<'a>,
uuid : Uuid,
//uuid : Uuid,
}
/*
impl<'a> From<TTask> for Task<'a> {
fn from(ttask : TTask) -> Task<'a> {
Task {
@ -19,4 +25,9 @@ impl<'a> From<TTask> for Task<'a> {
}
}
}
*/
impl<'a> IntoTask<'a> for TTask {
fn into_filelockentry(self) -> Result<Task<'a>, TodoError> {
Err(TodoError::new(TodoErrorKind::ConversionError, None))
}
}