diff options
Diffstat (limited to 'lib/domain/libimagdiary/src/iter.rs')
-rw-r--r-- | lib/domain/libimagdiary/src/iter.rs | 53 |
1 files changed, 43 insertions, 10 deletions
diff --git a/lib/domain/libimagdiary/src/iter.rs b/lib/domain/libimagdiary/src/iter.rs index 63bd0779..8d22aff8 100644 --- a/lib/domain/libimagdiary/src/iter.rs +++ b/lib/domain/libimagdiary/src/iter.rs @@ -21,21 +21,22 @@ use std::fmt::{Debug, Formatter, Error as FmtError}; use std::result::Result as RResult; use libimagstore::store::Store; +use libimagstore::store::FileLockEntry; use libimagstore::storeid::StoreIdIterator; +use libimagerror::trace::trace_error; use diaryid::DiaryId; use diaryid::FromStoreId; use is_in_diary::IsInDiary; -use entry::Entry as DiaryEntry; use error::DiaryErrorKind as DEK; +use error::DiaryError as DE; use error::ResultExt; use error::Result; -use libimagerror::trace::trace_error; /// A iterator for iterating over diary entries pub struct DiaryEntryIterator<'a> { store: &'a Store, - name: &'a str, + name: String, iter: StoreIdIterator, year: Option<i32>, @@ -54,7 +55,7 @@ impl<'a> Debug for DiaryEntryIterator<'a> { impl<'a> DiaryEntryIterator<'a> { - pub fn new(diaryname: &'a str, store: &'a Store, iter: StoreIdIterator) -> DiaryEntryIterator<'a> { + pub fn new(store: &'a Store, diaryname: String, iter: StoreIdIterator) -> DiaryEntryIterator<'a> { DiaryEntryIterator { store: store, name: diaryname, @@ -87,9 +88,9 @@ impl<'a> DiaryEntryIterator<'a> { } impl<'a> Iterator for DiaryEntryIterator<'a> { - type Item = Result<DiaryEntry<'a>>; + type Item = Result<FileLockEntry<'a>>; - fn next(&mut self) -> Option<Result<DiaryEntry<'a>>> { + fn next(&mut self) -> Option<Self::Item> { loop { let next = match self.iter.next() { Some(s) => s, @@ -97,7 +98,7 @@ impl<'a> Iterator for DiaryEntryIterator<'a> { }; debug!("Next element: {:?}", next); - if next.is_in_diary(self.name) { + if next.is_in_diary(&self.name) { debug!("Seems to be in diary: {:?}", next); let id = match DiaryId::from_storeid(&next) { Ok(i) => i, @@ -118,9 +119,7 @@ impl<'a> Iterator for DiaryEntryIterator<'a> { return Some(self .store .retrieve(next) - .map(|fle| DiaryEntry::new(fle)) - .chain_err(|| DEK::StoreReadError) - ); + .chain_err(|| DEK::StoreReadError)); } } else { debug!("Not in the requested diary ({}): {:?}", self.name, next); @@ -130,3 +129,37 @@ impl<'a> Iterator for DiaryEntryIterator<'a> { } + +/// Get diary names. +/// +/// # Warning +/// +/// Does _not_ run a `unique` on the iterator! +pub struct DiaryNameIterator(StoreIdIterator); + +impl DiaryNameIterator { + pub fn new(s: StoreIdIterator) -> DiaryNameIterator { + DiaryNameIterator(s) + } +} + +impl Iterator for DiaryNameIterator { + type Item = Result<String>; + + fn next(&mut self) -> Option<Self::Item> { + self.0 + .next() + .map(|s| { + s.to_str() + .chain_err(|| DEK::DiaryNameFindingError) + .and_then(|s| { + s.split("diary/") + .nth(1) + .and_then(|n| n.split("/").nth(0).map(String::from)) + .ok_or(DE::from_kind(DEK::DiaryNameFindingError)) + }) + }) + } + +} + |