From a76a0c86ace65769803436f6daec0d69bd08df7b Mon Sep 17 00:00:00 2001 From: daladim Date: Sun, 28 Feb 2021 11:01:40 +0100 Subject: [PATCH] Calendar keeps track of deleted items --- src/calendar.rs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/calendar.rs b/src/calendar.rs index 143de29..c5645c9 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -1,6 +1,7 @@ use std::convert::TryFrom; use std::error::Error; use std::collections::HashMap; +use std::collections::BTreeMap; use url::Url; use serde::{Deserialize, Serialize}; @@ -56,6 +57,7 @@ pub struct Calendar { supported_components: SupportedComponents, tasks: Vec, + deleted_tasks: BTreeMap, TaskId>, } impl Calendar { @@ -64,6 +66,7 @@ impl Calendar { Self { name, url, supported_components, tasks: Vec::new(), + deleted_tasks: BTreeMap::new(), } } @@ -89,6 +92,7 @@ impl Calendar { pub fn delete_task(&mut self, task_id: &TaskId) { self.tasks.retain(|t| t.id() != task_id); + self.deleted_tasks.insert(Utc::now(), task_id.clone()); } /// Returns the list of tasks that this calendar contains @@ -97,17 +101,6 @@ impl Calendar { self.get_tasks_modified_since(None, completed) } - /// Returns a particular task - pub fn get_task_by_id_mut(&mut self, id: &TaskId) -> Option<&mut Task> { - for task in &mut self.tasks { - if task.id() == id { - return Some(task); - } - } - return None; - } - - /// Returns the tasks that have been last-modified after `since` /// Pass a `completed` flag to filter only the completed (or non-completed) tasks pub fn get_tasks_modified_since(&self, since: Option>, _completed: Option) -> HashMap { @@ -126,4 +119,21 @@ impl Calendar { map } + + /// Returns the tasks that have been deleted after `since` + pub fn get_tasks_deleted_since(&self, since: DateTime) -> Vec { + self.deleted_tasks.range(since..) + .map(|(_key, value)| value.clone()) + .collect() + } + + /// Returns a particular task + pub fn get_task_by_id_mut(&mut self, id: &TaskId) -> Option<&mut Task> { + for task in &mut self.tasks { + if task.id() == id { + return Some(task); + } + } + return None; + } }