summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2018-04-30 17:28:54 +0200
committerMatthias Beyer <mail@beyermatthias.de>2018-05-01 17:44:00 +0200
commitbf0bef058d11380d8ba8bc4ecdf086bf6e2e2c23 (patch)
treefe15159d2d08a615588ac5b0d9f3a47b5371c99a
parent8f03b4a71ab173041e2ab740c2a268bfd0666d2d (diff)
downloadimag-bf0bef058d11380d8ba8bc4ecdf086bf6e2e2c23.zip
imag-bf0bef058d11380d8ba8bc4ecdf086bf6e2e2c23.tar.gz
Refactor libimagdiary to fit new store iterator interface
-rw-r--r--lib/domain/libimagdiary/src/diary.rs20
-rw-r--r--lib/domain/libimagdiary/src/iter.rs32
2 files changed, 35 insertions, 17 deletions
diff --git a/lib/domain/libimagdiary/src/diary.rs b/lib/domain/libimagdiary/src/diary.rs
index 15ee28b..ea4b249 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<Result<DiaryId>> {
+ 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 09f9114..6ebc523 100644
--- a/lib/domain/libimagdiary/src/iter.rs
+++ b/lib/domain/libimagdiary/src/iter.rs
@@ -103,16 +103,17 @@ impl Filter<StoreId> for DiaryEntryIterator {
}
impl Iterator for DiaryEntryIterator {
- type Item = StoreId;
+ type Item = Result<StoreId>;
fn next(&mut self) -> Option<Self::Item> {
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<Self::Item> {
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))
+ }));
+ },
}
}