From b12974043dd150adaf13ad33d76b49efe1356c15 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 6 Jul 2016 20:03:20 +0200 Subject: [PATCH] Remove the most ugly parts from the lib --- libimagtodo/src/read.rs | 6 ++---- libimagtodo/src/task.rs | 30 ++++++++++++++++-------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/libimagtodo/src/read.rs b/libimagtodo/src/read.rs index 57c326c9..f547ac30 100644 --- a/libimagtodo/src/read.rs +++ b/libimagtodo/src/read.rs @@ -5,7 +5,6 @@ use task::Task; use result::Result; pub fn get_todo_iterator(store: &Store) -> Result { - store.retrieve_for_module("todo/taskwarrior") .map(|iter| TaskIterator::new(store, iter)) .map_err(|e| TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e)))) @@ -38,14 +37,13 @@ impl<'a> TaskIterator<'a> { iditer: iditer, } } + } impl<'a> Iterator for TaskIterator<'a> { type Item = Result>; fn next(&mut self) -> Option>> { - self.iditer - .next() - .map(|id| Task::from_storeid(self.store, id)) + self.iditer.next().map(|id| Task::from_storeid(self.store, id)) } } diff --git a/libimagtodo/src/task.rs b/libimagtodo/src/task.rs index ba9c7e9e..277a5e40 100644 --- a/libimagtodo/src/task.rs +++ b/libimagtodo/src/task.rs @@ -17,18 +17,21 @@ pub struct Task<'a> { } impl<'a> Task<'a> { + /// Concstructs a new `Task` with a `FileLockEntry` pub fn new(fle : FileLockEntry<'a>) -> Task<'a> { Task { 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; @@ -44,25 +47,24 @@ pub trait IntoTask<'a> { /// } /// ``` fn into_filelockentry(self, store : &'a Store) -> Result>; + } + impl<'a> IntoTask<'a> for TTask { + fn into_filelockentry(self, store : &'a Store) -> Result> { - let uuid = self.uuid(); + let uuid = self.uuid(); let store_id = ModuleEntryPath::new(format!("taskwarrior/{}", uuid)).into_storeid(); + match store.retrieve(store_id) { - Err(e) => { - return Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e)))) - }, + Err(e) => return Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e)))), Ok(mut fle) => { { let mut header = fle.get_header_mut(); match header.read("todo") { Ok(None) => { - match header.set("todo", Value::Table(BTreeMap::new())) { - Ok(_) => { }, - Err(e) => { - return Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e)))) - } + if let Err(e) = header.set("todo", Value::Table(BTreeMap::new())) { + return Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e)))) } } Ok(Some(_)) => { } @@ -70,16 +72,16 @@ impl<'a> IntoTask<'a> for TTask { return Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e)))) } } - match header.set("todo.uuid", Value::String(format!("{}",uuid))) { - Ok(_) => { }, - Err(e) => { - return Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e)))) - } + + if let Err(e) = header.set("todo.uuid", Value::String(format!("{}",uuid))) { + return Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e)))) } } + // If none of the errors above have returned the function, everything is fine Ok(Task { flentry : fle } ) } } } + }