More async in traits
This commit is contained in:
@@ -57,11 +57,11 @@ impl PartialCalendar for CachedCalendar {
|
||||
self.supported_components
|
||||
}
|
||||
|
||||
fn add_item(&mut self, item: Item) {
|
||||
async fn add_item(&mut self, item: Item) {
|
||||
self.items.insert(item.id().clone(), item);
|
||||
}
|
||||
|
||||
fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>> {
|
||||
async fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>> {
|
||||
if let None = self.items.remove(item_id) {
|
||||
return Err("This key does not exist.".into());
|
||||
}
|
||||
@@ -97,11 +97,11 @@ impl PartialCalendar for CachedCalendar {
|
||||
map
|
||||
}
|
||||
|
||||
fn get_item_ids(&mut self) -> HashSet<ItemId> {
|
||||
async fn get_item_ids(&mut self) -> HashSet<ItemId> {
|
||||
self.items.keys().cloned().collect()
|
||||
}
|
||||
|
||||
fn get_item_by_id_mut(&mut self, id: &ItemId) -> Option<&mut Item> {
|
||||
async fn get_item_by_id_mut<'a>(&'a mut self, id: &ItemId) -> Option<&'a mut Item> {
|
||||
self.items.get_mut(id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,24 +44,24 @@ impl PartialCalendar for RemoteCalendar {
|
||||
}
|
||||
|
||||
/// Get the IDs of all current items in this calendar
|
||||
fn get_item_ids(&mut self) -> HashSet<ItemId> {
|
||||
async fn get_item_ids(&mut self) -> HashSet<ItemId> {
|
||||
log::error!("Not implemented");
|
||||
HashSet::new()
|
||||
}
|
||||
|
||||
/// Returns a particular item
|
||||
fn get_item_by_id_mut(&mut self, _id: &ItemId) -> Option<&mut Item> {
|
||||
async fn get_item_by_id_mut<'a>(&'a mut self, _id: &ItemId) -> Option<&'a mut Item> {
|
||||
log::error!("Not implemented");
|
||||
None
|
||||
}
|
||||
|
||||
/// Add an item into this calendar
|
||||
fn add_item(&mut self, _item: Item) {
|
||||
async fn add_item(&mut self, _item: Item) {
|
||||
log::error!("Not implemented");
|
||||
}
|
||||
|
||||
/// Remove an item from this calendar
|
||||
fn delete_item(&mut self, _item_id: &ItemId) -> Result<(), Box<dyn Error>> {
|
||||
async fn delete_item(&mut self, _item_id: &ItemId) -> Result<(), Box<dyn Error>> {
|
||||
log::error!("Not implemented");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+13
-13
@@ -19,7 +19,7 @@ where
|
||||
L: CalDavSource<T> + SyncSlave,
|
||||
T: CompleteCalendar,
|
||||
S: CalDavSource<U>,
|
||||
U: PartialCalendar,
|
||||
U: PartialCalendar + Sync + Send,
|
||||
{
|
||||
/// The remote server
|
||||
server: S,
|
||||
@@ -35,7 +35,7 @@ where
|
||||
L: CalDavSource<T> + SyncSlave,
|
||||
T: CompleteCalendar,
|
||||
S: CalDavSource<U>,
|
||||
U: PartialCalendar,
|
||||
U: PartialCalendar + Sync + Send,
|
||||
{
|
||||
/// Create a provider.
|
||||
///
|
||||
@@ -83,10 +83,10 @@ where
|
||||
Some(date) => cal_local.get_items_deleted_since(date).await,
|
||||
};
|
||||
if last_sync.is_some() {
|
||||
let server_deletions = cal_server.find_deletions_from(cal_local.get_item_ids());
|
||||
let server_deletions = cal_server.find_deletions_from(cal_local.get_item_ids().await).await;
|
||||
for server_del_id in server_deletions {
|
||||
// Even in case of conflicts, "the server always wins", so it is safe to remove tasks from the local cache as soon as now
|
||||
if let Err(err) = cal_local.delete_item(&server_del_id) {
|
||||
if let Err(err) = cal_local.delete_item(&server_del_id).await {
|
||||
log::error!("Unable to remove local item {}: {}", server_del_id, err);
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ where
|
||||
|
||||
// ...upload local deletions,...
|
||||
for local_del_id in local_dels {
|
||||
if let Err(err) = cal_server.delete_item(&local_del_id) {
|
||||
if let Err(err) = cal_server.delete_item(&local_del_id).await {
|
||||
log::error!("Unable to remove remote item {}: {}", local_del_id, err);
|
||||
}
|
||||
}
|
||||
@@ -126,15 +126,15 @@ where
|
||||
// ...and upload local changes
|
||||
for (local_mod_id, local_mod) in local_mods {
|
||||
// Conflicts are no longer in local_mods
|
||||
if let Err(err) = cal_server.delete_item(&local_mod_id) {
|
||||
if let Err(err) = cal_server.delete_item(&local_mod_id).await {
|
||||
log::error!("Unable to remove remote item (before an update) {}: {}", local_mod_id, err);
|
||||
}
|
||||
// TODO: should I add a .update_item()?
|
||||
cal_server.add_item(local_mod.clone());
|
||||
cal_server.add_item(local_mod.clone()).await;
|
||||
}
|
||||
|
||||
remove_from_calendar(&conflicting_tasks, &mut (*cal_local));
|
||||
move_to_calendar(&mut tasks_to_add, &mut (*cal_local));
|
||||
remove_from_calendar(&conflicting_tasks, &mut (*cal_local)).await;
|
||||
move_to_calendar(&mut tasks_to_add, &mut (*cal_local)).await;
|
||||
}
|
||||
|
||||
self.local.update_last_sync(None);
|
||||
@@ -144,18 +144,18 @@ where
|
||||
}
|
||||
|
||||
|
||||
fn move_to_calendar<C: PartialCalendar>(items: &mut Vec<Item>, calendar: &mut C) {
|
||||
async fn move_to_calendar<C: PartialCalendar>(items: &mut Vec<Item>, calendar: &mut C) {
|
||||
while items.len() > 0 {
|
||||
let item = items.remove(0);
|
||||
log::warn!("Moving {} to calendar", item.name());
|
||||
calendar.add_item(item);
|
||||
calendar.add_item(item).await;
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_from_calendar<C: PartialCalendar>(ids: &Vec<ItemId>, calendar: &mut C) {
|
||||
async fn remove_from_calendar<C: PartialCalendar>(ids: &Vec<ItemId>, calendar: &mut C) {
|
||||
for id in ids {
|
||||
log::info!(" Removing {:?} from calendar", id);
|
||||
if let Err(err) = calendar.delete_item(id) {
|
||||
if let Err(err) = calendar.delete_item(id).await {
|
||||
log::warn!("Unable to delete item {:?} from calendar: {}", id, err);
|
||||
}
|
||||
}
|
||||
|
||||
+6
-6
@@ -45,16 +45,16 @@ pub trait PartialCalendar {
|
||||
-> HashMap<ItemId, &Item>;
|
||||
|
||||
/// Get the IDs of all current items in this calendar
|
||||
fn get_item_ids(&mut self) -> HashSet<ItemId>;
|
||||
async fn get_item_ids(&mut self) -> HashSet<ItemId>;
|
||||
|
||||
/// Returns a particular item
|
||||
fn get_item_by_id_mut(&mut self, id: &ItemId) -> Option<&mut Item>;
|
||||
async fn get_item_by_id_mut<'a>(&'a mut self, id: &ItemId) -> Option<&'a mut Item>;
|
||||
|
||||
/// Add an item into this calendar
|
||||
fn add_item(&mut self, item: Item);
|
||||
async fn add_item(&mut self, item: Item);
|
||||
|
||||
/// Remove an item from this calendar
|
||||
fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>>;
|
||||
async fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>>;
|
||||
|
||||
|
||||
/// Returns whether this calDAV calendar supports to-do items
|
||||
@@ -68,8 +68,8 @@ pub trait PartialCalendar {
|
||||
}
|
||||
|
||||
/// Finds the IDs of the items that are missing compared to a reference set
|
||||
fn find_deletions_from(&mut self, reference_set: HashSet<ItemId>) -> HashSet<ItemId> {
|
||||
let current_items = self.get_item_ids();
|
||||
async fn find_deletions_from(&mut self, reference_set: HashSet<ItemId>) -> HashSet<ItemId> {
|
||||
let current_items = self.get_item_ids().await;
|
||||
reference_set.difference(¤t_items).map(|id| id.clone()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user