More Results in the trait

This commit is contained in:
daladim 2021-03-21 19:58:37 +01:00
parent 9a2a61a17b
commit a8ccdef0bb
6 changed files with 24 additions and 19 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().await;
let items_r = cal_r.get_items().await;
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

@ -34,11 +34,11 @@ impl CachedCalendar {
}
/// Returns the list of tasks that this calendar contains
pub async fn get_tasks(&self) -> HashMap<ItemId, &Item> {
pub async fn get_tasks(&self) -> Result<HashMap<ItemId, &Item>, Box<dyn Error>> {
self.get_tasks_modified_since(None).await
}
/// Returns the tasks that have been last-modified after `since`
pub async 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>>) -> Result<HashMap<ItemId, &Item>, Box<dyn Error>> {
self.get_items_modified_since(since, Some(SearchFilter::Tasks)).await
}
}
@ -69,7 +69,7 @@ impl PartialCalendar for CachedCalendar {
Ok(())
}
async 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>) -> Result<HashMap<ItemId, &Item>, Box<dyn Error>> {
let filter = filter.unwrap_or_default();
let mut map = HashMap::new();
@ -94,7 +94,7 @@ impl PartialCalendar for CachedCalendar {
map.insert(item.id().clone(), item);
}
map
Ok(map)
}
async fn get_item_ids(&mut self) -> HashSet<ItemId> {
@ -109,14 +109,14 @@ impl PartialCalendar for CachedCalendar {
#[async_trait]
impl CompleteCalendar for CachedCalendar {
/// Returns the items that have been deleted after `since`
async fn get_items_deleted_since(&self, since: DateTime<Utc>) -> HashSet<ItemId> {
self.deleted_items.range(since..)
async fn get_items_deleted_since(&self, since: DateTime<Utc>) -> Result<HashSet<ItemId>, Box<dyn Error>> {
Ok(self.deleted_items.range(since..)
.map(|(_key, id)| id.clone())
.collect()
.collect())
}
/// Returns the list of items that this calendar contains
async fn get_items(&self) -> HashMap<ItemId, &Item> {
async fn get_items(&self) -> Result<HashMap<ItemId, &Item>, Box<dyn Error>> {
self.get_items_modified_since(None, None).await
}

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).await,
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().await).await;
@ -97,8 +97,8 @@ where
}
// Step 2 - Compare both changesets...
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;
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

@ -42,7 +42,7 @@ pub trait PartialCalendar {
/// Returns the items that have been last-modified after `since`
async fn get_items_modified_since(&self, since: Option<DateTime<Utc>>, filter: Option<crate::calendar::SearchFilter>)
-> HashMap<ItemId, &Item>;
-> Result<HashMap<ItemId, &Item>, Box<dyn Error>>;
/// Get the IDs of all current items in this calendar
async fn get_item_ids(&mut self) -> HashSet<ItemId>;
@ -82,9 +82,9 @@ pub trait CompleteCalendar : PartialCalendar {
/// Returns the items that have been deleted after `since`
///
/// See also [`PartialCalendar::get_items_deleted_since`]
async fn get_items_deleted_since(&self, since: DateTime<Utc>) -> HashSet<ItemId>;
async fn get_items_deleted_since(&self, since: DateTime<Utc>) -> Result<HashSet<ItemId>, Box<dyn Error>>;
/// Returns the list of items that this calendar contains
async fn get_items(&self) -> HashMap<ItemId, &Item>;
async fn get_items(&self) -> Result<HashMap<ItemId, &Item>, Box<dyn Error>>;
}

View file

@ -64,9 +64,14 @@ where
{
for (id, cal) in cals {
println!("CAL {}", id);
for (_, item) in cal.lock().unwrap().get_items().await {
match cal.lock().unwrap().get_items().await {
Err(_err) => continue,
Ok(map) => {
for (_, item) in map {
print_task(item);
}
},
}
}
}

View file

@ -48,7 +48,7 @@ async fn show_calendars() {
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 {
for (_id, task) in cal.get_items_modified_since(None, None).await.unwrap() {
my_tasks::utils::print_task(task);
}
}