diff options
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)) + } + +} + |