From fd0568dbcc0b699990ddddd8df3718e86a902982 Mon Sep 17 00:00:00 2001 From: daladim Date: Wed, 24 Mar 2021 09:04:23 +0100 Subject: [PATCH] Add `mark_for_deletion` --- src/calendar/cached_calendar.rs | 42 ++++++++++++++++++++++++++++++--- tests/sync.rs | 6 ++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/calendar/cached_calendar.rs b/src/calendar/cached_calendar.rs index 72cc752..a7a08a5 100644 --- a/src/calendar/cached_calendar.rs +++ b/src/calendar/cached_calendar.rs @@ -1,12 +1,11 @@ use std::collections::{HashMap, HashSet}; -use std::collections::BTreeMap; use std::error::Error; use serde::{Deserialize, Serialize}; use async_trait::async_trait; -use crate::traits::{PartialCalendar, CompleteCalendar}; -use crate::calendar::{CalendarId, SupportedComponents, SearchFilter}; +use crate::{SyncStatus, traits::{PartialCalendar, CompleteCalendar}}; +use crate::calendar::{CalendarId, SupportedComponents}; use crate::Item; use crate::item::ItemId; use crate::item::VersionTag; @@ -30,6 +29,33 @@ impl CachedCalendar { items: HashMap::new(), } } + + pub async fn mark_for_deletion(&mut self, item_id: &ItemId) -> Result<(), Box> { + match self.items.get_mut(item_id) { + None => Err("no item for this key".into()), + Some(item) => { + match item.sync_status() { + SyncStatus::Synced(prev_ss) => { + let prev_ss = prev_ss.clone(); + item.set_sync_status( SyncStatus::LocallyDeleted(prev_ss)); + }, + SyncStatus::LocallyModified(prev_ss) => { + let prev_ss = prev_ss.clone(); + item.set_sync_status( SyncStatus::LocallyDeleted(prev_ss)); + }, + SyncStatus::LocallyDeleted(prev_ss) => { + let prev_ss = prev_ss.clone(); + item.set_sync_status( SyncStatus::LocallyDeleted(prev_ss)); + }, + SyncStatus::NotSynced => { + // This was never synced to the server, we can safely delete it as soon as now + self.items.remove(item_id); + }, + }; + Ok(()) + } + } + } } #[async_trait] @@ -82,6 +108,16 @@ impl PartialCalendar for CachedCalendar { Ok(result) } + // This reimplements the trait method to avoid resorting to `get_item_version_tags` + // (this is thus slighlty faster, but also avoids an unnecessary iteration over SyncStatus that might panic for some mocked values if feature `mock_version_tag` is set) + async fn get_item_ids(&self) -> Result, Box> { + Ok(self.items.iter() + .map(|(id, _)| id.clone()) + .collect() + ) + } + + async fn get_item_by_id_mut<'a>(&'a mut self, id: &ItemId) -> Option<&'a mut Item> { self.items.get_mut(id) } diff --git a/tests/sync.rs b/tests/sync.rs index ca7e89a..db88e69 100644 --- a/tests/sync.rs +++ b/tests/sync.rs @@ -136,7 +136,7 @@ async fn populate_test_provider() -> Provider Provider