Traits are closer to what actual calendars provide

This commit is contained in:
daladim 2021-03-28 01:22:24 +01:00
parent 9355629136
commit 479011755e
11 changed files with 177 additions and 161 deletions

View file

@ -10,7 +10,7 @@ use crate::item::VersionTag;
use crate::calendar::CalendarId;
#[async_trait]
pub trait CalDavSource<T: PartialCalendar> {
pub trait CalDavSource<T: BaseCalendar> {
/// 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<HashMap<CalendarId, Arc<Mutex<T>>>, Box<dyn Error>>;
@ -18,11 +18,9 @@ pub trait CalDavSource<T: PartialCalendar> {
async fn get_calendar(&self, id: &CalendarId) -> Option<Arc<Mutex<T>>>;
}
/// A calendar we have a partial knowledge of.
///
/// Usually, this is a calendar from a remote source, that is synced to a CompleteCalendar
/// This trait contains functions that are common to all calendars
#[async_trait]
pub trait PartialCalendar {
pub trait BaseCalendar {
/// Returns the calendar name
fn name(&self) -> &str;
@ -32,20 +30,11 @@ pub trait PartialCalendar {
/// Returns the supported kinds of components for this calendar
fn supported_components(&self) -> crate::calendar::SupportedComponents;
/// Get the IDs and the version tags of every item in this calendar
async fn get_item_version_tags(&self) -> Result<HashMap<ItemId, VersionTag>, Box<dyn Error>>;
/// Returns a particular item
async fn get_item_by_id_mut<'a>(&'a mut self, id: &ItemId) -> Option<&'a mut Item>;
/// Returns a particular item
async fn get_item_by_id<'a>(&'a self, id: &ItemId) -> Option<&'a Item>;
/// Add an item into this calendar
async fn add_item(&mut self, item: Item) -> Result<(), Box<dyn Error>>;
/// Remove an item from this calendar
async fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>>;
/// Returns a particular item
async fn get_item_by_id<'a>(&'a self, id: &ItemId) -> Option<&'a Item>;
/// Returns whether this calDAV calendar supports to-do items
@ -57,6 +46,17 @@ pub trait PartialCalendar {
fn supports_events(&self) -> bool {
self.supported_components().contains(crate::calendar::SupportedComponents::EVENT)
}
}
/// Functions availabe for calendars that are backed by a CalDAV server
#[async_trait]
pub trait DavCalendar : BaseCalendar {
/// Get the IDs and the version tags of every item in this calendar
async fn get_item_version_tags(&self) -> Result<HashMap<ItemId, VersionTag>, Box<dyn Error>>;
/// Delete an item
async fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>>;
/// Get the IDs of all current items in this calendar
async fn get_item_ids(&self) -> Result<HashSet<ItemId>, Box<dyn Error>> {
@ -65,20 +65,28 @@ pub trait PartialCalendar {
.map(|(id, _tag)| id.clone())
.collect())
}
/// Finds the IDs of the items that are missing compared to a reference set
async fn find_deletions_from(&self, reference_set: HashSet<ItemId>) -> Result<HashSet<ItemId>, Box<dyn Error>> {
let current_items = self.get_item_ids().await?;
Ok(reference_set.difference(&current_items).map(|id| id.clone()).collect())
}
}
/// A calendar we always know everything about.
/// Functions availabe for calendars we have full knowledge of
///
/// Usually, this is a calendar fully stored on a local disk
/// Usually, these are local calendars fully backed by a local folder
#[async_trait]
pub trait CompleteCalendar : PartialCalendar {
/// Returns the list of items that this calendar contains
async fn get_items(&self) -> Result<HashMap<ItemId, &Item>, Box<dyn Error>>;
}
pub trait CompleteCalendar : BaseCalendar {
/// Get the IDs of all current items in this calendar
async fn get_item_ids(&self) -> Result<HashSet<ItemId>, Box<dyn Error>>;
/// Returns all items that this calendar contains
async fn get_items(&self) -> Result<HashMap<ItemId, &Item>, Box<dyn Error>>;
/// Returns a particular item
async fn get_item_by_id_mut<'a>(&'a mut self, id: &ItemId) -> Option<&'a mut Item>;
/// Mark an item for deletion.
/// This is required so that the upcoming sync will know it should also also delete this task from the server
/// (and then call [`immediately_delete_item`] once it has been successfully deleted on the server)
async fn mark_for_deletion(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>>;
/// Immediately remove an item. See [`mark_for_deletion`]
async fn immediately_delete_item(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>>;
}