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)
}
/// 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();

View file

@ -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))
}));
},
}
}