Non-async version of some functions + renamed a function

This commit is contained in:
daladim 2021-07-05 11:00:36 +02:00
parent ab8f6a9413
commit 58cfb42f64
4 changed files with 52 additions and 19 deletions

View file

@ -25,6 +25,8 @@ const MAIN_FILE: &str = "data.json";
/// A CalDAV source that stores its items in a local folder.
///
/// It automatically updates the content of the folder when dropped (see its `Drop` implementation), but you can also manually call [`Cache::save_to_folder`]
///
/// Most of its methods are part of the `CalDavSource` trait implementation
#[derive(Debug)]
pub struct Cache {
backing_folder: PathBuf,
@ -180,10 +182,8 @@ impl Drop for Cache {
}
}
#[async_trait]
impl CalDavSource<CachedCalendar> for Cache {
async fn get_calendars(&self) -> Result<HashMap<CalendarId, Arc<Mutex<CachedCalendar>>>, Box<dyn Error>> {
impl Cache {
pub fn get_calendars_sync(&self) -> Result<HashMap<CalendarId, Arc<Mutex<CachedCalendar>>>, Box<dyn Error>> {
#[cfg(feature = "local_calendar_mocks_remote_calendars")]
self.mock_behaviour.as_ref().map_or(Ok(()), |b| b.lock().unwrap().can_get_calendars())?;
@ -193,9 +193,20 @@ impl CalDavSource<CachedCalendar> for Cache {
)
}
async fn get_calendar(&self, id: &CalendarId) -> Option<Arc<Mutex<CachedCalendar>>> {
pub fn get_calendar_sync(&self, id: &CalendarId) -> Option<Arc<Mutex<CachedCalendar>>> {
self.data.calendars.get(id).map(|arc| arc.clone())
}
}
#[async_trait]
impl CalDavSource<CachedCalendar> for Cache {
async fn get_calendars(&self) -> Result<HashMap<CalendarId, Arc<Mutex<CachedCalendar>>>, Box<dyn Error>> {
self.get_calendars_sync()
}
async fn get_calendar(&self, id: &CalendarId) -> Option<Arc<Mutex<CachedCalendar>>> {
self.get_calendar_sync(id)
}
async fn create_calendar(&mut self, id: CalendarId, name: String, supported_components: SupportedComponents) -> Result<Arc<Mutex<CachedCalendar>>, Box<dyn Error>> {
log::debug!("Inserting local calendar {}", id);

View file

@ -17,6 +17,8 @@ use crate::mock_behaviour::MockBehaviour;
/// A calendar used by the [`cache`](crate::cache) module
///
/// Most of its methods are part of traits implementations
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CachedCalendar {
name: String,
@ -111,6 +113,32 @@ impl CachedCalendar {
Ok(true)
}
/// The non-async version of [`Self::get_item_ids`]
pub fn get_item_ids_sync(&self) -> Result<HashSet<ItemId>, Box<dyn Error>> {
Ok(self.items.iter()
.map(|(id, _)| id.clone())
.collect()
)
}
/// The non-async version of [`Self::get_items`]
pub fn get_items_sync(&self) -> Result<HashMap<ItemId, &Item>, Box<dyn Error>> {
Ok(self.items.iter()
.map(|(id, item)| (id.clone(), item))
.collect()
)
}
/// The non-async version of [`Self::get_item_by_id`]
pub fn get_item_by_id_sync<'a>(&'a self, id: &ItemId) -> Option<&'a Item> {
self.items.get(id)
}
/// The non-async version of [`Self::get_item_by_id_mut`]
pub fn get_item_by_id_mut_sync<'a>(&'a mut self, id: &ItemId) -> Option<&'a mut Item> {
self.items.get_mut(id)
}
}
@ -163,25 +191,19 @@ impl CompleteCalendar for CachedCalendar {
}
async fn get_item_ids(&self) -> Result<HashSet<ItemId>, Box<dyn Error>> {
Ok(self.items.iter()
.map(|(id, _)| id.clone())
.collect()
)
self.get_item_ids_sync()
}
async fn get_items(&self) -> Result<HashMap<ItemId, &Item>, Box<dyn Error>> {
Ok(self.items.iter()
.map(|(id, item)| (id.clone(), item))
.collect()
)
self.get_items_sync()
}
async fn get_item_by_id_ref<'a>(&'a self, id: &ItemId) -> Option<&'a Item> {
self.items.get(id)
async fn get_item_by_id<'a>(&'a self, id: &ItemId) -> Option<&'a Item> {
self.get_item_by_id_sync(id)
}
async fn get_item_by_id_mut<'a>(&'a mut self, id: &ItemId) -> Option<&'a mut Item> {
self.items.get_mut(id)
self.get_item_by_id_mut_sync(id)
}
async fn mark_for_deletion(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>> {

View file

@ -177,7 +177,7 @@ where
let mut local_items_to_handle = cal_local.get_item_ids().await?;
for (id, remote_tag) in remote_items {
result.trace(&format!("***** Considering remote item {}...", id));
match cal_local.get_item_by_id_ref(&id).await {
match cal_local.get_item_by_id(&id).await {
None => {
// This was created on the remote
result.debug(&format!("* {} is a remote addition", id));
@ -230,7 +230,7 @@ where
// Also iterate on the local tasks that are not on the remote
for id in local_items_to_handle {
result.trace(&format!("##### Considering local item {}...", id));
let local_item = match cal_local.get_item_by_id_ref(&id).await {
let local_item = match cal_local.get_item_by_id(&id).await {
None => {
result.error(&format!("Inconsistent state: missing task {} from the local tasks", id));
continue;

View file

@ -103,7 +103,7 @@ pub trait CompleteCalendar : BaseCalendar {
async fn get_items(&self) -> Result<HashMap<ItemId, &Item>, Box<dyn Error>>;
/// Returns a particular item
async fn get_item_by_id_ref<'a>(&'a self, id: &ItemId) -> Option<&'a Item>;
async fn get_item_by_id<'a>(&'a self, id: &ItemId) -> Option<&'a Item>;
/// Returns a particular item
async fn get_item_by_id_mut<'a>(&'a mut self, id: &ItemId) -> Option<&'a mut Item>;