diff options
Diffstat (limited to 'libimagtodo')
-rw-r--r-- | libimagtodo/Cargo.toml | 2 | ||||
-rw-r--r-- | libimagtodo/src/error.rs | 13 | ||||
-rw-r--r-- | libimagtodo/src/lib.rs | 1 | ||||
-rw-r--r-- | 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 <mario-krehl@gmx.de>"] 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<Box<Error>>) -> 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<Task<'a>, TodoError>; -} - +/// Task struct containing a `FileLockEntry` #[derive(Debug)] pub struct Task<'a> { flentry : FileLockEntry<'a>, - //uuid : Uuid, } -/* -impl<'a> From<TTask> 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<Task<'a>, TodoError>; +} impl<'a> IntoTask<'a> for TTask { - fn into_filelockentry(self) -> Result<Task<'a>, TodoError> { - Err(TodoError::new(TodoErrorKind::ConversionError, None)) + fn into_filelockentry(self, store : &'a Store) -> Result<Task<'a>, 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 }) + } + }, + } } } |