get_tasks returns a HashMap

This commit is contained in:
daladim 2021-02-26 17:55:57 +01:00
parent 6b1d5635c6
commit 4dd0bea78b

View file

@ -1,8 +1,10 @@
use std::convert::TryFrom; use std::convert::TryFrom;
use std::error::Error; use std::error::Error;
use std::collections::HashMap;
use url::Url; use url::Url;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};
use crate::task::Task; use crate::task::Task;
use crate::task::TaskId; use crate::task::TaskId;
@ -91,7 +93,7 @@ impl Calendar {
/// Returns the list of tasks that this calendar contains /// Returns the list of tasks that this calendar contains
/// Pass a `completed` flag to filter only the completed (or non-completed) tasks /// Pass a `completed` flag to filter only the completed (or non-completed) tasks
pub fn get_tasks(&self, completed: Option<bool>) -> Vec<&Task> { pub fn get_tasks(&self, completed: Option<bool>) -> HashMap<TaskId, &Task> {
self.get_tasks_modified_since(None, completed) self.get_tasks_modified_since(None, completed)
} }
@ -108,9 +110,20 @@ impl Calendar {
/// Returns the tasks that have been last-modified after `since` /// Returns the tasks that have been last-modified after `since`
/// Pass a `completed` flag to filter only the completed (or non-completed) tasks /// Pass a `completed` flag to filter only the completed (or non-completed) tasks
fn get_tasks_modified_since(&self, _since: Option<std::time::SystemTime>, _completed: Option<bool>) -> Vec<&Task> { pub fn get_tasks_modified_since(&self, since: Option<DateTime<Utc>>, _completed: Option<bool>) -> HashMap<TaskId, &Task> {
self.tasks let mut map = HashMap::new();
.iter()
.collect() for task in &self.tasks {
match since {
None => (),
Some(since) => if task.last_modified() < since {
continue;
},
}
map.insert(task.id().clone(), task);
}
map
} }
} }