More async functions in traits

This commit is contained in:
daladim 2021-03-21 19:05:22 +01:00
parent d53ec193d8
commit 6ce077ca12
8 changed files with 37 additions and 29 deletions

View file

@ -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");

View file

@ -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<ItemId, &Item> {
self.get_tasks_modified_since(None)
pub async fn get_tasks(&self) -> HashMap<ItemId, &Item> {
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<DateTime<Utc>>) -> HashMap<ItemId, &Item> {
self.get_items_modified_since(since, Some(SearchFilter::Tasks))
pub async fn get_tasks_modified_since(&self, since: Option<DateTime<Utc>>) -> HashMap<ItemId, &Item> {
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<DateTime<Utc>>, filter: Option<SearchFilter>) -> HashMap<ItemId, &Item> {
async fn get_items_modified_since(&self, since: Option<DateTime<Utc>>, filter: Option<SearchFilter>) -> HashMap<ItemId, &Item> {
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<Utc>) -> HashSet<ItemId> {
async fn get_items_deleted_since(&self, since: DateTime<Utc>) -> HashSet<ItemId> {
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<ItemId, &Item> {
self.get_items_modified_since(None, None)
async fn get_items(&self) -> HashMap<ItemId, &Item> {
self.get_items_modified_since(None, None).await
}
}

View file

@ -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<DateTime<Utc>>, _filter: Option<crate::calendar::SearchFilter>)
async fn get_items_modified_since(&self, since: Option<DateTime<Utc>>, _filter: Option<crate::calendar::SearchFilter>)
-> HashMap<ItemId, &Item>
{
log::error!("Not implemented");

View file

@ -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();

View file

@ -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<DateTime<Utc>>, filter: Option<crate::calendar::SearchFilter>)
async fn get_items_modified_since(&self, since: Option<DateTime<Utc>>, filter: Option<crate::calendar::SearchFilter>)
-> HashMap<ItemId, &Item>;
/// 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<Utc>) -> HashSet<ItemId>;
async fn get_items_deleted_since(&self, since: DateTime<Utc>) -> HashSet<ItemId>;
/// Returns the list of items that this calendar contains
fn get_items(&self) -> HashMap<ItemId, &Item>;
async fn get_items(&self) -> HashMap<ItemId, &Item>;
}

View file

@ -58,13 +58,13 @@ pub fn print_xml(element: &Element) {
}
/// A debug utility that pretty-prints calendars
pub fn print_calendar_list<C>(cals: &HashMap<CalendarId, Arc<Mutex<C>>>)
pub async fn print_calendar_list<C>(cals: &HashMap<CalendarId, Arc<Mutex<C>>>)
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);
}
}

View file

@ -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]

View file

@ -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());