summaryrefslogtreecommitdiffstats
path: root/libimagtodo
diff options
context:
space:
mode:
authormario-kr <mario-krehl@gmx.de>2016-06-16 10:14:43 +0200
committerGitHub <noreply@github.com>2016-06-16 10:14:43 +0200
commit4b8bf877c1c2f3c228ff60bb6de89dc469cbbc34 (patch)
tree6d44bacb626baaf65166d4229f798e548f704009 /libimagtodo
parente42c1e61f3e9c876bdb7133da84fad0ed2750e2a (diff)
parent84581c73ca33a750be75a2d8536e30370eb04be9 (diff)
Merge pull request #6 from rscheme/implement_read
Implement Read
Diffstat (limited to 'libimagtodo')
-rw-r--r--libimagtodo/src/read.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/libimagtodo/src/read.rs b/libimagtodo/src/read.rs
index e69de29b..9129fb60 100644
--- a/libimagtodo/src/read.rs
+++ b/libimagtodo/src/read.rs
@@ -0,0 +1,56 @@
+
+use libimagstore::storeid::{StoreIdIterator, StoreId};
+use libimagstore::store::Store;
+use error::{TodoError, TodoErrorKind};
+use task::Task;
+
+use std::result::Result as RResult;
+
+pub type Result<T> = RResult<T, TodoError>;
+
+
+pub fn all_todos(store: &Store) -> Result<TaskIterator> {
+
+ store.retrieve_for_module("uuid")
+ .map(|iter| TaskIterator::new(store, iter))
+ .map_err(|e| TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e))))
+}
+
+trait FromStoreId {
+ fn from_storeid<'a>(&'a Store, StoreId) -> Result<Task<'a>>;
+}
+
+impl<'a> FromStoreId for Task<'a> {
+
+ fn from_storeid<'b>(store: &'b Store, id: StoreId) -> Result<Task<'b>> {
+ match store.retrieve(id) {
+ Err(e) => Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e)))),
+ Ok(c) => Ok(Task::new( c )),
+ }
+ }
+}
+
+pub struct TaskIterator<'a> {
+ store: &'a Store,
+ iditer: StoreIdIterator,
+}
+
+impl<'a> TaskIterator<'a> {
+
+ pub fn new(store: &'a Store, iditer: StoreIdIterator) -> TaskIterator<'a> {
+ TaskIterator {
+ store: store,
+ iditer: iditer,
+ }
+ }
+}
+
+impl<'a> Iterator for TaskIterator<'a> {
+ type Item = Result<Task<'a>>;
+
+ fn next(&mut self) -> Option<Result<Task<'a>>> {
+ self.iditer
+ .next()
+ .map(|id| Task::from_storeid(self.store, id))
+ }
+}