Refactor libimagdiary to fit new store iterator interface

This commit is contained in:
Matthias Beyer 2018-04-30 17:28:54 +02:00
parent 8f03b4a71a
commit bf0bef058d
2 changed files with 35 additions and 17 deletions

View file

@ -94,13 +94,27 @@ impl Diary for Store {
.chain_err(|| DEK::StoreReadError) .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>> { fn get_youngest_entry_id(&self, diary_name: &str) -> Option<Result<DiaryId>> {
use error::DiaryError as DE;
match Diary::entries(self, diary_name) { match Diary::entries(self, diary_name) {
Err(e) => Some(Err(e)), Err(e) => Some(Err(e)),
Ok(entries) => { Ok(entries) => {
entries let mut sorted_entries = vec![];
.map(|e| DiaryId::from_storeid(&e))
.sorted_by(|a, b| { 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) { match (a, b) {
(&Ok(ref a), &Ok(ref b)) => { (&Ok(ref a), &Ok(ref b)) => {
let a : NaiveDateTime = a.clone().into(); let a : NaiveDateTime = a.clone().into();

View file

@ -103,16 +103,17 @@ impl Filter<StoreId> for DiaryEntryIterator {
} }
impl Iterator for DiaryEntryIterator { impl Iterator for DiaryEntryIterator {
type Item = StoreId; type Item = Result<StoreId>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
loop { loop {
match self.iter.next() { match self.iter.next() {
None => return None, None => return None,
Some(s) => { Some(Err(e)) => return Some(Err(e).map_err(DE::from)),
Some(Ok(s)) => {
debug!("Next element: {:?}", s); debug!("Next element: {:?}", s);
if Filter::filter(self, &s) { if Filter::filter(self, &s) {
return Some(s) return Some(Ok(s))
} else { } else {
continue continue
} }
@ -141,16 +142,19 @@ impl Iterator for DiaryNameIterator {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
while let Some(next) = self.0.next() { while let Some(next) = self.0.next() {
if next.is_in_collection(&["diary"]) { match next {
return Some(next Err(e) => return Some(Err(e).map_err(DE::from)),
.to_str() Ok(next) => if next.is_in_collection(&["diary"]) {
.chain_err(|| DEK::DiaryNameFindingError) return Some(next
.and_then(|s| { .to_str()
s.split("diary/") .chain_err(|| DEK::DiaryNameFindingError)
.nth(1) .and_then(|s| {
.and_then(|n| n.split("/").nth(0).map(String::from)) s.split("diary/")
.ok_or(DE::from_kind(DEK::DiaryNameFindingError)) .nth(1)
})) .and_then(|n| n.split("/").nth(0).map(String::from))
.ok_or(DE::from_kind(DEK::DiaryNameFindingError))
}));
},
} }
} }