From efd72d65a11112bbe8182a27ad8ba7d1ee0bbc84 Mon Sep 17 00:00:00 2001 From: mario Date: Wed, 4 May 2016 14:37:25 +0200 Subject: [PATCH 1/5] added dependency to libimagstore --- libimagtodo/Cargo.toml | 4 ++++ libimagtodo/src/lib.rs | 6 ++++++ libimagtodo/src/task.rs | 9 +++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/libimagtodo/Cargo.toml b/libimagtodo/Cargo.toml index e1621658..afc6c4f9 100644 --- a/libimagtodo/Cargo.toml +++ b/libimagtodo/Cargo.toml @@ -5,3 +5,7 @@ authors = ["mario "] [dependencies] task-hookrs = { git = "https://github.com/matthiasbeyer/task-hookrs.git" } + +[dependencies.libimagstore] +path = "../libimagstore" + diff --git a/libimagtodo/src/lib.rs b/libimagtodo/src/lib.rs index 59b0070f..4149a420 100644 --- a/libimagtodo/src/lib.rs +++ b/libimagtodo/src/lib.rs @@ -1,3 +1,9 @@ + +extern crate task_hookrs; +#[macro_use] extern crate libimagstore; + +module_entry_path_mod!("todo", "0.1.0"); + pub mod task; pub mod delete; pub mod read; diff --git a/libimagtodo/src/task.rs b/libimagtodo/src/task.rs index a6eec643..0dfa9c6f 100644 --- a/libimagtodo/src/task.rs +++ b/libimagtodo/src/task.rs @@ -1,8 +1,13 @@ -extern crate task_hookrs; +use std::ops::Deref; -use self::task_hookrs::task::Task as TTask; +use task_hookrs::task::Task as TTask; + +use libimagstore::store::FileLockEntry; pub struct Task { uuid : str, } +impl Deref for Task { + +} From 0f77c9d3d88724dbe5949bfc3c9d8e37c0009bd6 Mon Sep 17 00:00:00 2001 From: mario Date: Wed, 4 May 2016 15:00:48 +0200 Subject: [PATCH 2/5] experiment with Store/FileLockEntry --- libimagtodo/Cargo.toml | 1 + libimagtodo/src/lib.rs | 1 + libimagtodo/src/task.rs | 15 +++++++++++---- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/libimagtodo/Cargo.toml b/libimagtodo/Cargo.toml index afc6c4f9..07059ac9 100644 --- a/libimagtodo/Cargo.toml +++ b/libimagtodo/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["mario "] [dependencies] +semver = "0.2" task-hookrs = { git = "https://github.com/matthiasbeyer/task-hookrs.git" } [dependencies.libimagstore] diff --git a/libimagtodo/src/lib.rs b/libimagtodo/src/lib.rs index 4149a420..fdd29389 100644 --- a/libimagtodo/src/lib.rs +++ b/libimagtodo/src/lib.rs @@ -1,3 +1,4 @@ +extern crate semver; extern crate task_hookrs; #[macro_use] extern crate libimagstore; diff --git a/libimagtodo/src/task.rs b/libimagtodo/src/task.rs index 0dfa9c6f..5ff6bc5b 100644 --- a/libimagtodo/src/task.rs +++ b/libimagtodo/src/task.rs @@ -4,10 +4,17 @@ use task_hookrs::task::Task as TTask; use libimagstore::store::FileLockEntry; -pub struct Task { - uuid : str, +#[derive(Debug)] +pub struct Task<'a> { + flentry : FileLockEntry<'a>, } -impl Deref for Task { - +impl<'a> From for Task<'a> { + fn from(ttask : TTask) -> Task<'a> { + Task { + flentry : { + } + } + } } + From df6f41942be3e525c1578bad1a90fc8fbf17a802 Mon Sep 17 00:00:00 2001 From: Mario Krehl Date: Sun, 8 May 2016 10:59:21 +0200 Subject: [PATCH 3/5] added uuid crate and type --- libimagtodo/Cargo.toml | 1 + libimagtodo/src/lib.rs | 1 + libimagtodo/src/task.rs | 2 ++ 3 files changed, 4 insertions(+) diff --git a/libimagtodo/Cargo.toml b/libimagtodo/Cargo.toml index 07059ac9..9cdc711c 100644 --- a/libimagtodo/Cargo.toml +++ b/libimagtodo/Cargo.toml @@ -6,6 +6,7 @@ authors = ["mario "] [dependencies] semver = "0.2" task-hookrs = { git = "https://github.com/matthiasbeyer/task-hookrs.git" } +uuid = "0.2.0" [dependencies.libimagstore] path = "../libimagstore" diff --git a/libimagtodo/src/lib.rs b/libimagtodo/src/lib.rs index fdd29389..9913f7b4 100644 --- a/libimagtodo/src/lib.rs +++ b/libimagtodo/src/lib.rs @@ -1,4 +1,5 @@ extern crate semver; +extern crate uuid; extern crate task_hookrs; #[macro_use] extern crate libimagstore; diff --git a/libimagtodo/src/task.rs b/libimagtodo/src/task.rs index 5ff6bc5b..41f71cee 100644 --- a/libimagtodo/src/task.rs +++ b/libimagtodo/src/task.rs @@ -1,5 +1,6 @@ use std::ops::Deref; +use uuid::Uuid; use task_hookrs::task::Task as TTask; use libimagstore::store::FileLockEntry; @@ -7,6 +8,7 @@ use libimagstore::store::FileLockEntry; #[derive(Debug)] pub struct Task<'a> { flentry : FileLockEntry<'a>, + uuid : Uuid, } impl<'a> From for Task<'a> { From 26ec9710ccd27fd5f29c2ff8917b881ea32ed655 Mon Sep 17 00:00:00 2001 From: mario Date: Mon, 9 May 2016 12:11:13 +0200 Subject: [PATCH 4/5] 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 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)) + } +} From 2f999caeef95d0aab38645cd986eda15929830f0 Mon Sep 17 00:00:00 2001 From: mario Date: Mon, 9 May 2016 17:22:40 +0200 Subject: [PATCH 5/5] implement the task struct, trait for task_hookrs::task::Task, and some minor error handling --- libimagtodo/Cargo.toml | 2 +- libimagtodo/src/error.rs | 13 ++++++--- libimagtodo/src/lib.rs | 1 + libimagtodo/src/task.rs | 58 +++++++++++++++++++++++++++++----------- 4 files changed, 55 insertions(+), 19 deletions(-) diff --git a/libimagtodo/Cargo.toml b/libimagtodo/Cargo.toml index 9cdc711c..dc1f6d8b 100644 --- a/libimagtodo/Cargo.toml +++ b/libimagtodo/Cargo.toml @@ -7,7 +7,7 @@ authors = ["mario "] semver = "0.2" task-hookrs = { git = "https://github.com/matthiasbeyer/task-hookrs.git" } uuid = "0.2.0" +toml = "0.1.28" [dependencies.libimagstore] path = "../libimagstore" - diff --git a/libimagtodo/src/error.rs b/libimagtodo/src/error.rs index 4dafc389..d4be3a4d 100644 --- a/libimagtodo/src/error.rs +++ b/libimagtodo/src/error.rs @@ -4,14 +4,21 @@ use std::fmt::Error as FmtError; use std::fmt::{Debug, Display, Formatter}; use std::fmt; +/// 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 #[derive(Clone, Copy, Debug, PartialEq)] pub enum TodoErrorKind { ConversionError, + StoreError, } +/// Maps a TodoErrorKind to a String fn todo_error_type_as_str(e: &TodoErrorKind) -> &'static str { match e { &TodoErrorKind::ConversionError => "Conversion Error", + &TodoErrorKind::StoreError => "Store Error", } } @@ -22,6 +29,7 @@ impl Display for TodoErrorKind { } } +/// Error struct for the imag-todo module #[derive(Debug)] pub struct TodoError { err_type : TodoErrorKind, @@ -29,12 +37,14 @@ pub struct TodoError { } impl TodoError { + /// Creates a new TodoError, with TodoErrorKind errtype and an optional cause pub fn new(errtype : TodoErrorKind, cause : Option>) -> TodoError { TodoError { err_type : errtype, cause : cause, } } + /// Returns the error type (TodoErrorKind) pub fn err_type(&self) -> TodoErrorKind { self.err_type.clone() } @@ -55,6 +65,3 @@ impl Error for TodoError { self.cause.as_ref().map(|e| &**e) } } - - - diff --git a/libimagtodo/src/lib.rs b/libimagtodo/src/lib.rs index f1eaf3a3..2f78a6c4 100644 --- a/libimagtodo/src/lib.rs +++ b/libimagtodo/src/lib.rs @@ -1,5 +1,6 @@ extern crate semver; extern crate uuid; +extern crate toml; extern crate task_hookrs; #[macro_use] extern crate libimagstore; diff --git a/libimagtodo/src/task.rs b/libimagtodo/src/task.rs index 66517528..8c3b0886 100644 --- a/libimagtodo/src/task.rs +++ b/libimagtodo/src/task.rs @@ -1,33 +1,61 @@ use std::ops::Deref; +use toml::Value; -use uuid::Uuid; use task_hookrs::task::Task as TTask; -use libimagstore::store::FileLockEntry; +use libimagstore::store::{FileLockEntry, Store}; +use libimagstore::storeid::IntoStoreId; +use module_path::ModuleEntryPath; use error::{TodoError, TodoErrorKind}; -pub trait IntoTask<'a> { - fn into_filelockentry(self) -> Result, TodoError>; -} - +/// Task struct containing a `FileLockEntry` #[derive(Debug)] pub struct Task<'a> { flentry : FileLockEntry<'a>, - //uuid : Uuid, } -/* -impl<'a> From for Task<'a> { - fn from(ttask : TTask) -> Task<'a> { + +impl<'a> Task<'a> { + /// Concstructs a new `Task` with a `FileLockEntry` + pub fn new(fle : FileLockEntry<'a>) -> Task<'a> { Task { - flentry : { - } + flentry : fle } } } -*/ + +/// A trait to get a `libimagtodo::task::Task` out of the implementing object. +/// This Task struct is merely a wrapper for a `FileLockEntry`, therefore the function name +/// `into_filelockentry`. +pub trait IntoTask<'a> { + /// # Usage + /// ```ignore + /// use std::io::stdin; + /// + /// use task_hookrs::task::Task; + /// use task_hookrs::import::import; + /// use libimagstore::store::{Store, FileLockEntry}; + /// + /// if let Ok(task_hookrs_task) = import(stdin()) { + /// // Store is given at runtime + /// let task = task_hookrs_task.into_filelockentry(store); + /// println!("Task with uuid: {}", task.flentry.get_header().get("todo.uuid")); + /// } + /// ``` + fn into_filelockentry(self, store : &'a Store) -> Result, TodoError>; +} impl<'a> IntoTask<'a> for TTask { - fn into_filelockentry(self) -> Result, TodoError> { - Err(TodoError::new(TodoErrorKind::ConversionError, None)) + fn into_filelockentry(self, store : &'a Store) -> Result, TodoError> { + let uuid = self.uuid(); + let store_id = ModuleEntryPath::new(format!("taskwarrior/{}", uuid)).into_storeid(); + match store.retrieve(store_id) { + Err(e) => Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e)))), + Ok(mut fle) => { + match fle.get_header_mut().set("todo.uuid", Value::String(format!("{}", uuid))) { + Err(e) => Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e)))), + Ok(_) => Ok(Task { flentry : fle }) + } + }, + } } }