The Big Type Overhaul

This commit is contained in:
daladim 2021-03-05 23:32:42 +01:00
parent 02a0eece63
commit 9c17f07660
9 changed files with 122 additions and 117 deletions

View file

@ -1,31 +1,31 @@
use std::error::Error;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use async_trait::async_trait;
use url::Url;
use chrono::{DateTime, Utc};
use crate::item::Item;
use crate::item::ItemId;
use crate::calendar::CalendarId;
#[async_trait]
pub trait CalDavSource<T: PartialCalendar> {
/// Returns the current calendars that this source contains
/// This function may trigger an update (that can be a long process, or that can even fail, e.g. in case of a remote server)
async fn get_calendars(&self) -> Result<&Vec<T>, Box<dyn Error>>;
async fn get_calendars(&self) -> Result<&HashMap<CalendarId, T>, Box<dyn Error>>;
/// Returns the current calendars that this source contains
/// This function may trigger an update (that can be a long process, or that can even fail, e.g. in case of a remote server)
async fn get_calendars_mut(&mut self) -> Result<Vec<&mut T>, Box<dyn Error>>;
async fn get_calendars_mut(&mut self) -> Result<HashMap<CalendarId, &mut T>, Box<dyn Error>>;
//
//
// TODO: find a better search key (do calendars have a unique ID?)
// TODO: search key should be a reference
//
/// Returns the calendar matching the URL
async fn get_calendar(&self, url: Url) -> Option<&T>;
/// Returns the calendar matching the URL
async fn get_calendar_mut(&mut self, url: Url) -> Option<&mut T>;
/// Returns the calendar matching the ID
async fn get_calendar(&self, id: CalendarId) -> Option<&T>;
/// Returns the calendar matching the ID
async fn get_calendar_mut(&mut self, id: CalendarId) -> Option<&mut T>;
}
@ -44,8 +44,8 @@ pub trait PartialCalendar {
/// Returns the calendar name
fn name(&self) -> &str;
/// Returns the calendar URL
fn url(&self) -> &Url;
/// Returns the calendar unique ID
fn id(&self) -> &CalendarId;
/// Returns the supported kinds of components for this calendar
fn supported_components(&self) -> crate::calendar::SupportedComponents;
@ -55,7 +55,7 @@ pub trait PartialCalendar {
-> HashMap<ItemId, &Item>;
/// Get the IDs of all current items in this calendar
fn get_item_ids(&mut self) -> Vec<ItemId>;
fn get_item_ids(&mut self) -> HashSet<ItemId>;
/// Returns a particular item
fn get_item_by_id_mut(&mut self, id: &ItemId) -> Option<&mut Item>;
@ -64,7 +64,7 @@ pub trait PartialCalendar {
fn add_item(&mut self, item: Item);
/// Remove an item from this calendar
fn delete_item(&mut self, item_id: &ItemId);
fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>>;
/// Returns whether this calDAV calendar supports to-do items
@ -78,16 +78,9 @@ pub trait PartialCalendar {
}
/// Finds the IDs of the items that are missing compared to a reference set
fn find_deletions(&mut self, reference_set: Vec<ItemId>) -> Vec<ItemId> {
let mut deletions = Vec::new();
fn find_deletions_from(&mut self, reference_set: HashSet<ItemId>) -> HashSet<ItemId> {
let current_items = self.get_item_ids();
for original_item in reference_set {
if current_items.contains(&original_item) == false {
deletions.push(original_item);
}
}
deletions
reference_set.difference(&current_items).map(|id| id.clone()).collect()
}
}
@ -98,7 +91,7 @@ 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>) -> Vec<ItemId>;
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>;