From 26ec9710ccd27fd5f29c2ff8917b881ea32ed655 Mon Sep 17 00:00:00 2001 From: mario Date: Mon, 9 May 2016 12:11:13 +0200 Subject: introduce ConversionError in error.rs and add trait for task_hookrs::task::Task to convert it to a libimagtodo::task::Task --- libimagtodo/src/error.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ libimagtodo/src/lib.rs | 1 + libimagtodo/src/task.rs | 17 +++++++++++--- 3 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 libimagtodo/src/error.rs (limited to 'libimagtodo') diff --git a/libimagtodo/src/error.rs b/libimagtodo/src/error.rs new file mode 100644 index 00000000..4dafc389 --- /dev/null +++ b/libimagtodo/src/error.rs @@ -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>, +} + +impl TodoError { + pub fn new(errtype : TodoErrorKind, cause : Option>) -> 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) + } +} + + + diff --git a/libimagtodo/src/lib.rs b/libimagtodo/src/lib.rs index 9913f7b4..f1eaf3a3 100644 --- a/libimagtodo/src/lib.rs +++ b/libimagtodo/src/lib.rs @@ -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; diff --git a/libimagtodo/src/task.rs b/libimagtodo/src/task.rs index 41f71cee..66517528 100644 --- a/libimagtodo/src/task.rs +++ b/libimagtodo/src/task.rs @@ -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, TodoError>; +} + #[derive(Debug)] pub struct Task<'a> { flentry : FileLockEntry<'a>, - uuid : Uuid, + //uuid : Uuid, } - +/* impl<'a> From for Task<'a> { fn from(ttask : TTask) -> Task<'a> { Task { @@ -19,4 +25,9 @@ impl<'a> From for Task<'a> { } } } - +*/ +impl<'a> IntoTask<'a> for TTask { + fn into_filelockentry(self) -> Result, TodoError> { + Err(TodoError::new(TodoErrorKind::ConversionError, None)) + } +} -- cgit v1.2.3