Rewrite: Diary::get_youngest_entry_id()

Rewrite to collect not so often internally. This removes one collect()
from the implementation. One is still there in the sorted_by() call,
though.

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-03-09 12:30:36 +01:00
parent 205c2b09b6
commit ca2f130995

View file

@ -99,24 +99,14 @@ impl Diary for Store {
} }
/// get the id of the youngest entry /// 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>> {
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) => {
let mut sorted_entries = vec![]; entries.map(|ent| {
ent.and_then(|id| DiaryId::from_storeid(&id))
for entry in entries { })
let entry = match entry { .sorted_by(|a, b| {
Ok(e) => DiaryId::from_storeid(&e),
Err(e) => return Some(Err(e)),
};
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();