diff --git a/lib/domain/libimagdiary/src/diary.rs b/lib/domain/libimagdiary/src/diary.rs index 15ee28ba..ea4b2491 100644 --- a/lib/domain/libimagdiary/src/diary.rs +++ b/lib/domain/libimagdiary/src/diary.rs @@ -94,13 +94,27 @@ impl Diary for Store { .chain_err(|| DEK::StoreReadError) } + /// get the id of the youngest entry + /// + /// TODO: We collect internally here. We shouldn't do that. Solution unclear. fn get_youngest_entry_id(&self, diary_name: &str) -> Option> { + use error::DiaryError as DE; + match Diary::entries(self, diary_name) { Err(e) => Some(Err(e)), Ok(entries) => { - entries - .map(|e| DiaryId::from_storeid(&e)) - .sorted_by(|a, b| { + let mut sorted_entries = vec![]; + + for entry in entries { + let entry = match entry { + Ok(e) => DiaryId::from_storeid(&e), + Err(e) => return Some(Err(e).map_err(DE::from)), + }; + + sorted_entries.push(entry); + } + + sorted_entries.into_iter().sorted_by(|a, b| { match (a, b) { (&Ok(ref a), &Ok(ref b)) => { let a : NaiveDateTime = a.clone().into(); diff --git a/lib/domain/libimagdiary/src/iter.rs b/lib/domain/libimagdiary/src/iter.rs index 09f9114e..6ebc5230 100644 --- a/lib/domain/libimagdiary/src/iter.rs +++ b/lib/domain/libimagdiary/src/iter.rs @@ -103,16 +103,17 @@ impl Filter for DiaryEntryIterator { } impl Iterator for DiaryEntryIterator { - type Item = StoreId; + type Item = Result; fn next(&mut self) -> Option { loop { match self.iter.next() { - None => return None, - Some(s) => { + None => return None, + Some(Err(e)) => return Some(Err(e).map_err(DE::from)), + Some(Ok(s)) => { debug!("Next element: {:?}", s); if Filter::filter(self, &s) { - return Some(s) + return Some(Ok(s)) } else { continue } @@ -141,16 +142,19 @@ impl Iterator for DiaryNameIterator { fn next(&mut self) -> Option { while let Some(next) = self.0.next() { - if next.is_in_collection(&["diary"]) { - return Some(next - .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)) - })) + match next { + Err(e) => return Some(Err(e).map_err(DE::from)), + Ok(next) => if next.is_in_collection(&["diary"]) { + return Some(next + .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)) + })); + }, } }