diff --git a/src/cache.rs b/src/cache.rs index 135bc83..443de21 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -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 for Cache { - async fn get_calendars(&self) -> Result>>, Box> { +impl Cache { + pub fn get_calendars_sync(&self) -> Result>>, Box> { #[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 for Cache { ) } - async fn get_calendar(&self, id: &CalendarId) -> Option>> { + pub fn get_calendar_sync(&self, id: &CalendarId) -> Option>> { self.data.calendars.get(id).map(|arc| arc.clone()) } +} + +#[async_trait] +impl CalDavSource for Cache { + async fn get_calendars(&self) -> Result>>, Box> { + self.get_calendars_sync() + } + + async fn get_calendar(&self, id: &CalendarId) -> Option>> { + self.get_calendar_sync(id) + } async fn create_calendar(&mut self, id: CalendarId, name: String, supported_components: SupportedComponents) -> Result>, Box> { log::debug!("Inserting local calendar {}", id); diff --git a/src/calendar/cached_calendar.rs b/src/calendar/cached_calendar.rs index d8a76fa..49958b2 100644 --- a/src/calendar/cached_calendar.rs +++ b/src/calendar/cached_calendar.rs @@ -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, Box> { + Ok(self.items.iter() + .map(|(id, _)| id.clone()) + .collect() + ) + } + + /// The non-async version of [`Self::get_items`] + pub fn get_items_sync(&self) -> Result, Box> { + 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, Box> { - Ok(self.items.iter() - .map(|(id, _)| id.clone()) - .collect() - ) + self.get_item_ids_sync() } async fn get_items(&self) -> Result, Box> { - 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> { diff --git a/src/provider.rs b/src/provider.rs index 08ffb60..e641df2 100644 --- a/src/provider.rs +++ b/src/provider.rs @@ -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; diff --git a/src/traits.rs b/src/traits.rs index 693b37f..b7ff999 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -103,7 +103,7 @@ pub trait CompleteCalendar : BaseCalendar { async fn get_items(&self) -> Result, Box>; /// 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>;