Refactor libimagdiary to fit new store iterator interface
This commit is contained in:
parent
8f03b4a71a
commit
bf0bef058d
2 changed files with 35 additions and 17 deletions
|
@ -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();
|
||||||
|
|
|
@ -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,7 +142,9 @@ 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 {
|
||||||
|
Err(e) => return Some(Err(e).map_err(DE::from)),
|
||||||
|
Ok(next) => if next.is_in_collection(&["diary"]) {
|
||||||
return Some(next
|
return Some(next
|
||||||
.to_str()
|
.to_str()
|
||||||
.chain_err(|| DEK::DiaryNameFindingError)
|
.chain_err(|| DEK::DiaryNameFindingError)
|
||||||
|
@ -150,7 +153,8 @@ impl Iterator for DiaryNameIterator {
|
||||||
.nth(1)
|
.nth(1)
|
||||||
.and_then(|n| n.split("/").nth(0).map(String::from))
|
.and_then(|n| n.split("/").nth(0).map(String::from))
|
||||||
.ok_or(DE::from_kind(DEK::DiaryNameFindingError))
|
.ok_or(DE::from_kind(DEK::DiaryNameFindingError))
|
||||||
}))
|
}));
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue