[provider] Minor refactoring

This commit is contained in:
daladim 2021-04-03 23:25:34 +02:00
parent 427212be89
commit 842e34fb85

View file

@ -3,6 +3,7 @@
use std::error::Error;
use std::collections::HashSet;
use std::marker::PhantomData;
use std::sync::{Arc, Mutex};
use crate::traits::{CalDavSource, DavCalendar};
use crate::traits::CompleteCalendar;
@ -57,24 +58,33 @@ where
log::info!("Starting a sync.");
let cals_remote = self.remote.get_calendars().await?;
for (id, cal_remote) in cals_remote {
let mut cal_remote = cal_remote.lock().unwrap();
for (cal_id, cal_remote) in cals_remote {
let cal_local = loop {
if let Some(cal) = self.local.get_calendar(&id).await {
if let Some(cal) = self.local.get_calendar(&cal_id).await {
break cal;
}
// This calendar does not exist locally yet, let's add it
log::error!("TODO: what name/SP should we choose?");
let new_calendar = T::new(String::from("new calendar"), id.clone(), SupportedComponents::TODO);
let new_calendar = T::new(String::from("new calendar"), cal_id.clone(), SupportedComponents::TODO);
if let Err(err) = self.local.insert_calendar(new_calendar).await {
log::warn!("Unable to create local calendar {}: {}. Skipping it.", id, err);
log::warn!("Unable to create local calendar {}: {}. Skipping it.", cal_id, err);
continue;
}
};
if let Err(err) = Self::sync_calendar_pair(cal_local, cal_remote).await {
log::warn!("Unable to sync calendar {}: {}, skipping this time.", cal_id, err);
}
}
Ok(())
}
async fn sync_calendar_pair(cal_local: Arc<Mutex<T>>, cal_remote: Arc<Mutex<U>>) -> Result<(), Box<dyn Error>> {
let mut cal_remote = cal_remote.lock().unwrap();
let mut cal_local = cal_local.lock().unwrap();
// Step 1 - find the differences
@ -203,7 +213,7 @@ where
log::debug!("> Applying remote addition {} locally", id_add);
match cal_remote.get_item_by_id(&id_add).await {
Err(err) => {
log::warn!("Unable to get remote item {}: {}. Skipping it.", id, err);
log::warn!("Unable to get remote item {}: {}. Skipping it.", id_add, err);
continue;
},
Ok(item) => match item {
@ -224,7 +234,7 @@ where
log::debug!("> Applying remote change {} locally", id_change);
match cal_remote.get_item_by_id(&id_change).await {
Err(err) => {
log::warn!("Unable to get remote item {}: {}. Skippin it", id, err);
log::warn!("Unable to get remote item {}: {}. Skipping it", id_change, err);
continue;
},
Ok(item) => match item {
@ -233,6 +243,12 @@ where
continue;
},
Some(item) => {
//
//
//
//
// TODO: implement update_item (maybe only create_item also updates it?)
//
if let Err(err) = cal_local.immediately_delete_item(&id_change).await {
log::error!("Unable to delete item {} from local calendar: {}", id_change, err);
}
@ -272,6 +288,12 @@ where
continue;
},
Some(item) => {
//
//
//
//
// TODO: implement update_item (maybe only create_item also updates it?)
//
if let Err(err) = cal_remote.delete_item(&id_change).await {
log::error!("Unable to delete item {} from remote calendar: {}", id_change, err);
}
@ -281,13 +303,12 @@ where
// Update local sync status
item.set_sync_status(new_ss);
},
}
}
};
}
};
}
Ok(())
}
}