diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-03-19 19:22:00 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-03-25 16:30:12 +0100 |
commit | 115a8248a4a17f3275f94d750bac03d8b47cc046 (patch) | |
tree | ac9acf1f07ae187fd3a8ab2c7797d3941895fac5 /libimagnotes | |
parent | 831ff84b74b475c0297b47aecdf5270c3457a837 (diff) |
lib: Add Note::all_notes()
Diffstat (limited to 'libimagnotes')
-rw-r--r-- | libimagnotes/src/note.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/libimagnotes/src/note.rs b/libimagnotes/src/note.rs index 5105f7bc..2696fe15 100644 --- a/libimagnotes/src/note.rs +++ b/libimagnotes/src/note.rs @@ -5,6 +5,8 @@ use std::ops::{DerefMut, Deref}; use toml::Value; use libimagstore::storeid::IntoStoreId; +use libimagstore::storeid::StoreId; +use libimagstore::storeid::StoreIdIterator; use libimagstore::store::FileLockEntry; use libimagstore::store::Store; use libimagtag::tag::Tag; @@ -92,6 +94,12 @@ impl<'a> Note<'a> { .map(|entry| Note { entry: entry }) } + pub fn all_notes(store: &Store) -> Result<NoteIterator> { + store.retrieve_for_module("notes") + .map(|iter| NoteIterator::new(store, iter)) + .map_err(|e| NE::new(NEK::StoreReadError, Some(Box::new(e)))) + } + } impl<'a> Tagable for Note<'a> { @@ -122,3 +130,46 @@ impl<'a> Tagable for Note<'a> { } +trait FromStoreId { + fn from_storeid<'a>(&'a Store, StoreId) -> Result<Note<'a>>; +} + +impl<'a> FromStoreId for Note<'a> { + + fn from_storeid<'b>(store: &'b Store, id: StoreId) -> Result<Note<'b>> { + debug!("Loading note from storeid: '{:?}'", id); + match store.retrieve(id) { + Err(e) => Err(NE::new(NEK::StoreReadError, Some(Box::new(e)))), + Ok(entry) => Ok(Note { entry: entry }), + } + } + +} + +pub struct NoteIterator<'a> { + store: &'a Store, + iditer: StoreIdIterator, +} + +impl<'a> NoteIterator<'a> { + + pub fn new(store: &'a Store, iditer: StoreIdIterator) -> NoteIterator<'a> { + NoteIterator { + store: store, + iditer: iditer, + } + } + +} + +impl<'a> Iterator for NoteIterator<'a> { + type Item = Result<Note<'a>>; + + fn next(&mut self) -> Option<Result<Note<'a>>> { + self.iditer + .next() + .map(|id| Note::from_storeid(self.store, id)) + } + +} + |