Add mark_for_deletion

This commit is contained in:
daladim 2021-03-24 09:04:23 +01:00
parent bcc8fc705b
commit fd0568dbcc
2 changed files with 42 additions and 6 deletions

View file

@ -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<dyn Error>> {
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<HashSet<ItemId>, Box<dyn Error>> {
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)
}

View file

@ -136,7 +136,7 @@ async fn populate_test_provider() -> Provider<Cache, CachedCalendar, Cache, Cach
let cal_local = local.get_calendar(&cal_id).await.unwrap();
let mut cal_local = cal_local.lock().unwrap();
cal_local.delete_item(&task_c_id).await.unwrap();
cal_local.mark_for_deletion(&task_c_id).await.unwrap();
cal_local.get_item_by_id_mut(&task_d_id).await.unwrap().unwrap_task_mut()
.set_name("D has been locally renamed".into());
@ -153,8 +153,8 @@ async fn populate_test_provider() -> Provider<Cache, CachedCalendar, Cache, Cach
cal_local.get_item_by_id_mut(&task_j_id).await.unwrap().unwrap_task_mut()
.set_completed(true);
cal_local.delete_item(&task_k_id).await.unwrap();
cal_local.delete_item(&task_l_id).await.unwrap();
cal_local.mark_for_deletion(&task_k_id).await.unwrap();
cal_local.mark_for_deletion(&task_l_id).await.unwrap();
let task_o = Item::Task(Task::new("task O (new from local)".into(), ItemId::random(), SyncStatus::NotSynced));
cal_local.add_item(task_o).await;