More async functions in traits
This commit is contained in:
parent
d53ec193d8
commit
6ce077ca12
8 changed files with 37 additions and 29 deletions
|
@ -143,8 +143,8 @@ impl Cache {
|
||||||
None => return Err("should not happen, we've just tested keys are the same".into()),
|
None => return Err("should not happen, we've just tested keys are the same".into()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let items_l = cal_l.get_items();
|
let items_l = cal_l.get_items().await;
|
||||||
let items_r = cal_r.get_items();
|
let items_r = cal_r.get_items().await;
|
||||||
|
|
||||||
if keys_are_the_same(&items_l, &items_r) == false {
|
if keys_are_the_same(&items_l, &items_r) == false {
|
||||||
log::debug!("Different keys for items");
|
log::debug!("Different keys for items");
|
||||||
|
|
|
@ -4,6 +4,7 @@ use std::error::Error;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
use crate::traits::{PartialCalendar, CompleteCalendar};
|
use crate::traits::{PartialCalendar, CompleteCalendar};
|
||||||
use crate::calendar::{CalendarId, SupportedComponents, SearchFilter};
|
use crate::calendar::{CalendarId, SupportedComponents, SearchFilter};
|
||||||
|
@ -33,15 +34,16 @@ impl CachedCalendar {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the list of tasks that this calendar contains
|
/// Returns the list of tasks that this calendar contains
|
||||||
pub fn get_tasks(&self) -> HashMap<ItemId, &Item> {
|
pub async fn get_tasks(&self) -> HashMap<ItemId, &Item> {
|
||||||
self.get_tasks_modified_since(None)
|
self.get_tasks_modified_since(None).await
|
||||||
}
|
}
|
||||||
/// Returns the tasks that have been last-modified after `since`
|
/// Returns the tasks that have been last-modified after `since`
|
||||||
pub fn get_tasks_modified_since(&self, since: Option<DateTime<Utc>>) -> HashMap<ItemId, &Item> {
|
pub async fn get_tasks_modified_since(&self, since: Option<DateTime<Utc>>) -> HashMap<ItemId, &Item> {
|
||||||
self.get_items_modified_since(since, Some(SearchFilter::Tasks))
|
self.get_items_modified_since(since, Some(SearchFilter::Tasks)).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
impl PartialCalendar for CachedCalendar {
|
impl PartialCalendar for CachedCalendar {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
&self.name
|
&self.name
|
||||||
|
@ -67,7 +69,7 @@ impl PartialCalendar for CachedCalendar {
|
||||||
Ok(())
|
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 filter = filter.unwrap_or_default();
|
||||||
|
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
|
@ -104,17 +106,18 @@ impl PartialCalendar for CachedCalendar {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
impl CompleteCalendar for CachedCalendar {
|
impl CompleteCalendar for CachedCalendar {
|
||||||
/// Returns the items that have been deleted after `since`
|
/// 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..)
|
self.deleted_items.range(since..)
|
||||||
.map(|(_key, id)| id.clone())
|
.map(|(_key, id)| id.clone())
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the list of items that this calendar contains
|
/// Returns the list of items that this calendar contains
|
||||||
fn get_items(&self) -> HashMap<ItemId, &Item> {
|
async fn get_items(&self) -> HashMap<ItemId, &Item> {
|
||||||
self.get_items_modified_since(None, None)
|
self.get_items_modified_since(None, None).await
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ use std::error::Error;
|
||||||
|
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
use crate::traits::PartialCalendar;
|
use crate::traits::PartialCalendar;
|
||||||
use crate::calendar::SupportedComponents;
|
use crate::calendar::SupportedComponents;
|
||||||
|
@ -26,6 +27,7 @@ impl RemoteCalendar {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
impl PartialCalendar for RemoteCalendar {
|
impl PartialCalendar for RemoteCalendar {
|
||||||
fn name(&self) -> &str { &self.name }
|
fn name(&self) -> &str { &self.name }
|
||||||
fn id(&self) -> &CalendarId { &self.url }
|
fn id(&self) -> &CalendarId { &self.url }
|
||||||
|
@ -34,7 +36,7 @@ impl PartialCalendar for RemoteCalendar {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the items that have been last-modified after `since`
|
/// 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>
|
-> HashMap<ItemId, &Item>
|
||||||
{
|
{
|
||||||
log::error!("Not implemented");
|
log::error!("Not implemented");
|
||||||
|
|
|
@ -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.
|
// 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 {
|
let mut local_dels = match last_sync {
|
||||||
None => HashSet::new(),
|
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() {
|
if last_sync.is_some() {
|
||||||
let server_deletions = cal_server.find_deletions_from(cal_local.get_item_ids());
|
let server_deletions = cal_server.find_deletions_from(cal_local.get_item_ids());
|
||||||
|
@ -97,8 +97,8 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 2 - Compare both changesets...
|
// Step 2 - Compare both changesets...
|
||||||
let server_mods = cal_server.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);
|
let mut local_mods = cal_local.get_items_modified_since(last_sync, None).await;
|
||||||
|
|
||||||
// ...import remote changes,...
|
// ...import remote changes,...
|
||||||
let mut conflicting_tasks = Vec::new();
|
let mut conflicting_tasks = Vec::new();
|
||||||
|
|
|
@ -29,6 +29,7 @@ pub trait SyncSlave {
|
||||||
/// A calendar we have a partial knowledge of.
|
/// A calendar we have a partial knowledge of.
|
||||||
///
|
///
|
||||||
/// Usually, this is a calendar from a remote source, that is synced to a CompleteCalendar
|
/// Usually, this is a calendar from a remote source, that is synced to a CompleteCalendar
|
||||||
|
#[async_trait]
|
||||||
pub trait PartialCalendar {
|
pub trait PartialCalendar {
|
||||||
/// Returns the calendar name
|
/// Returns the calendar name
|
||||||
fn name(&self) -> &str;
|
fn name(&self) -> &str;
|
||||||
|
@ -40,7 +41,7 @@ pub trait PartialCalendar {
|
||||||
fn supported_components(&self) -> crate::calendar::SupportedComponents;
|
fn supported_components(&self) -> crate::calendar::SupportedComponents;
|
||||||
|
|
||||||
/// Returns the items that have been last-modified after `since`
|
/// 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>;
|
-> HashMap<ItemId, &Item>;
|
||||||
|
|
||||||
/// Get the IDs of all current items in this calendar
|
/// Get the IDs of all current items in this calendar
|
||||||
|
@ -76,13 +77,14 @@ pub trait PartialCalendar {
|
||||||
/// A calendar we always know everything about.
|
/// A calendar we always know everything about.
|
||||||
///
|
///
|
||||||
/// Usually, this is a calendar fully stored on a local disk
|
/// Usually, this is a calendar fully stored on a local disk
|
||||||
|
#[async_trait]
|
||||||
pub trait CompleteCalendar : PartialCalendar {
|
pub trait CompleteCalendar : PartialCalendar {
|
||||||
/// Returns the items that have been deleted after `since`
|
/// Returns the items that have been deleted after `since`
|
||||||
///
|
///
|
||||||
/// See also [`PartialCalendar::get_items_deleted_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
|
/// Returns the list of items that this calendar contains
|
||||||
fn get_items(&self) -> HashMap<ItemId, &Item>;
|
async fn get_items(&self) -> HashMap<ItemId, &Item>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -58,13 +58,13 @@ pub fn print_xml(element: &Element) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A debug utility that pretty-prints calendars
|
/// 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
|
where
|
||||||
C: CompleteCalendar,
|
C: CompleteCalendar,
|
||||||
{
|
{
|
||||||
for (id, cal) in cals {
|
for (id, cal) in cals {
|
||||||
println!("CAL {}", id);
|
println!("CAL {}", id);
|
||||||
for (_, item) in cal.lock().unwrap().get_items() {
|
for (_, item) in cal.lock().unwrap().get_items().await {
|
||||||
print_task(item);
|
print_task(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,20 +37,21 @@ static EXAMPLE_TASKS_BODY_LAST_MODIFIED: &str = r#"
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_client() {
|
async fn show_calendars() {
|
||||||
let _ = env_logger::builder().is_test(true).try_init();
|
let _ = env_logger::builder().is_test(true).try_init();
|
||||||
|
|
||||||
let client = Client::new(URL, USERNAME, PASSWORD).unwrap();
|
let client = Client::new(URL, USERNAME, PASSWORD).unwrap();
|
||||||
let calendars = client.get_calendars().await.unwrap();
|
let calendars = client.get_calendars().await.unwrap();
|
||||||
|
|
||||||
let mut last_cal = None;
|
|
||||||
println!("Calendars:");
|
println!("Calendars:");
|
||||||
let _ = calendars.iter()
|
for (id, calendar) in calendars.iter() {
|
||||||
.map(|(id, cal)| {
|
let cal = calendar.lock().unwrap();
|
||||||
println!(" {}\t{}", cal.lock().unwrap().name(), id.as_str());
|
println!(" {}\t{}", cal.name(), id.as_str());
|
||||||
last_cal = Some(id);
|
println!(" Most recent changes:");
|
||||||
})
|
for (_id, task) in cal.get_items_modified_since(None, None).await {
|
||||||
.collect::<()>();
|
my_tasks::utils::print_task(task);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|
|
@ -24,10 +24,10 @@ async fn test_regular_sync() {
|
||||||
|
|
||||||
let cals_server = provider.server().get_calendars().await.unwrap();
|
let cals_server = provider.server().get_calendars().await.unwrap();
|
||||||
println!("----Server-------");
|
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();
|
let cals_local = provider.local().get_calendars().await.unwrap();
|
||||||
println!("\n----Local-------");
|
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());
|
assert!(provider.server().has_same_contents_than(provider.local()).await.unwrap());
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue