diff --git a/src/cache.rs b/src/cache.rs index 8c4a208..02db660 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -143,8 +143,8 @@ impl Cache { None => return Err("should not happen, we've just tested keys are the same".into()), }; - let items_l = cal_l.get_items(); - let items_r = cal_r.get_items(); + let items_l = cal_l.get_items().await; + let items_r = cal_r.get_items().await; if keys_are_the_same(&items_l, &items_r) == false { log::debug!("Different keys for items"); diff --git a/src/calendar/cached_calendar.rs b/src/calendar/cached_calendar.rs index 33769b2..e93d219 100644 --- a/src/calendar/cached_calendar.rs +++ b/src/calendar/cached_calendar.rs @@ -4,6 +4,7 @@ use std::error::Error; use serde::{Deserialize, Serialize}; use chrono::{DateTime, Utc}; +use async_trait::async_trait; use crate::traits::{PartialCalendar, CompleteCalendar}; use crate::calendar::{CalendarId, SupportedComponents, SearchFilter}; @@ -33,15 +34,16 @@ impl CachedCalendar { } /// Returns the list of tasks that this calendar contains - pub fn get_tasks(&self) -> HashMap { - self.get_tasks_modified_since(None) + pub async fn get_tasks(&self) -> HashMap { + self.get_tasks_modified_since(None).await } /// Returns the tasks that have been last-modified after `since` - pub fn get_tasks_modified_since(&self, since: Option>) -> HashMap { - self.get_items_modified_since(since, Some(SearchFilter::Tasks)) + pub async fn get_tasks_modified_since(&self, since: Option>) -> HashMap { + self.get_items_modified_since(since, Some(SearchFilter::Tasks)).await } } +#[async_trait] impl PartialCalendar for CachedCalendar { fn name(&self) -> &str { &self.name @@ -67,7 +69,7 @@ impl PartialCalendar for CachedCalendar { Ok(()) } - fn get_items_modified_since(&self, since: Option>, filter: Option) -> HashMap { + async fn get_items_modified_since(&self, since: Option>, filter: Option) -> HashMap { let filter = filter.unwrap_or_default(); let mut map = HashMap::new(); @@ -104,17 +106,18 @@ impl PartialCalendar for CachedCalendar { } } +#[async_trait] impl CompleteCalendar for CachedCalendar { /// Returns the items that have been deleted after `since` - fn get_items_deleted_since(&self, since: DateTime) -> HashSet { + async fn get_items_deleted_since(&self, since: DateTime) -> HashSet { self.deleted_items.range(since..) .map(|(_key, id)| id.clone()) .collect() } /// Returns the list of items that this calendar contains - fn get_items(&self) -> HashMap { - self.get_items_modified_since(None, None) + async fn get_items(&self) -> HashMap { + self.get_items_modified_since(None, None).await } } diff --git a/src/calendar/remote_calendar.rs b/src/calendar/remote_calendar.rs index a009de0..4574603 100644 --- a/src/calendar/remote_calendar.rs +++ b/src/calendar/remote_calendar.rs @@ -3,6 +3,7 @@ use std::error::Error; use url::Url; use chrono::{DateTime, Utc}; +use async_trait::async_trait; use crate::traits::PartialCalendar; use crate::calendar::SupportedComponents; @@ -26,6 +27,7 @@ impl RemoteCalendar { } } +#[async_trait] impl PartialCalendar for RemoteCalendar { fn name(&self) -> &str { &self.name } fn id(&self) -> &CalendarId { &self.url } @@ -34,7 +36,7 @@ impl PartialCalendar for RemoteCalendar { } /// Returns the items that have been last-modified after `since` - fn get_items_modified_since(&self, _since: Option>, _filter: Option) + async fn get_items_modified_since(&self, since: Option>, _filter: Option) -> HashMap { log::error!("Not implemented"); diff --git a/src/provider.rs b/src/provider.rs index b229717..58f2db8 100644 --- a/src/provider.rs +++ b/src/provider.rs @@ -80,7 +80,7 @@ where // Step 1 - "Server always wins", so a delteion from the server must be applied locally, even if it was locally modified. let mut local_dels = match last_sync { None => HashSet::new(), - Some(date) => cal_local.get_items_deleted_since(date), + 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()); @@ -97,8 +97,8 @@ where } // Step 2 - Compare both changesets... - let server_mods = cal_server.get_items_modified_since(last_sync, None); - let mut local_mods = cal_local.get_items_modified_since(last_sync, None); + let server_mods = cal_server.get_items_modified_since(last_sync, None).await; + let mut local_mods = cal_local.get_items_modified_since(last_sync, None).await; // ...import remote changes,... let mut conflicting_tasks = Vec::new(); diff --git a/src/traits.rs b/src/traits.rs index a31e1c6..c0071ed 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -29,6 +29,7 @@ pub trait SyncSlave { /// A calendar we have a partial knowledge of. /// /// Usually, this is a calendar from a remote source, that is synced to a CompleteCalendar +#[async_trait] pub trait PartialCalendar { /// Returns the calendar name fn name(&self) -> &str; @@ -40,7 +41,7 @@ pub trait PartialCalendar { fn supported_components(&self) -> crate::calendar::SupportedComponents; /// Returns the items that have been last-modified after `since` - fn get_items_modified_since(&self, since: Option>, filter: Option) + async fn get_items_modified_since(&self, since: Option>, filter: Option) -> HashMap; /// Get the IDs of all current items in this calendar @@ -76,13 +77,14 @@ pub trait PartialCalendar { /// A calendar we always know everything about. /// /// Usually, this is a calendar fully stored on a local disk +#[async_trait] pub trait CompleteCalendar : PartialCalendar { /// Returns the items that have been deleted after `since` /// /// See also [`PartialCalendar::get_items_deleted_since`] - fn get_items_deleted_since(&self, since: DateTime) -> HashSet; + async fn get_items_deleted_since(&self, since: DateTime) -> HashSet; /// Returns the list of items that this calendar contains - fn get_items(&self) -> HashMap; + async fn get_items(&self) -> HashMap; } diff --git a/src/utils.rs b/src/utils.rs index 595e908..d0b398d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -58,13 +58,13 @@ pub fn print_xml(element: &Element) { } /// A debug utility that pretty-prints calendars -pub fn print_calendar_list(cals: &HashMap>>) +pub async fn print_calendar_list(cals: &HashMap>>) where C: CompleteCalendar, { for (id, cal) in cals { println!("CAL {}", id); - for (_, item) in cal.lock().unwrap().get_items() { + for (_, item) in cal.lock().unwrap().get_items().await { print_task(item); } } diff --git a/tests/caldav_client.rs b/tests/caldav_client.rs index 676380d..df6cdf6 100644 --- a/tests/caldav_client.rs +++ b/tests/caldav_client.rs @@ -37,20 +37,21 @@ static EXAMPLE_TASKS_BODY_LAST_MODIFIED: &str = r#" "#; #[tokio::test] -async fn test_client() { +async fn show_calendars() { let _ = env_logger::builder().is_test(true).try_init(); let client = Client::new(URL, USERNAME, PASSWORD).unwrap(); let calendars = client.get_calendars().await.unwrap(); - let mut last_cal = None; println!("Calendars:"); - let _ = calendars.iter() - .map(|(id, cal)| { - println!(" {}\t{}", cal.lock().unwrap().name(), id.as_str()); - last_cal = Some(id); - }) - .collect::<()>(); + for (id, calendar) in calendars.iter() { + let cal = calendar.lock().unwrap(); + println!(" {}\t{}", cal.name(), id.as_str()); + println!(" Most recent changes:"); + for (_id, task) in cal.get_items_modified_since(None, None).await { + my_tasks::utils::print_task(task); + } + } } #[tokio::test] diff --git a/tests/sync.rs b/tests/sync.rs index a41ef76..a1254a5 100644 --- a/tests/sync.rs +++ b/tests/sync.rs @@ -24,10 +24,10 @@ async fn test_regular_sync() { let cals_server = provider.server().get_calendars().await.unwrap(); println!("----Server-------"); - my_tasks::utils::print_calendar_list(&cals_server); + my_tasks::utils::print_calendar_list(&cals_server).await; let cals_local = provider.local().get_calendars().await.unwrap(); println!("\n----Local-------"); - my_tasks::utils::print_calendar_list(&cals_local); + my_tasks::utils::print_calendar_list(&cals_local).await; assert!(provider.server().has_same_contents_than(provider.local()).await.unwrap());