kitchen-freezer/src/traits.rs

119 lines
4.8 KiB
Rust
Raw Normal View History

2021-02-20 00:10:05 +01:00
use std::error::Error;
2021-03-05 23:32:42 +01:00
use std::collections::{HashMap, HashSet};
2021-03-18 23:59:06 +01:00
use std::sync::{Arc, Mutex};
2021-02-18 12:02:04 +01:00
2021-02-25 00:47:39 +01:00
use async_trait::async_trait;
2021-02-18 12:02:04 +01:00
2021-04-28 23:43:03 +02:00
use crate::item::SyncStatus;
2021-03-01 23:39:16 +01:00
use crate::item::Item;
use crate::item::ItemId;
use crate::item::VersionTag;
2021-03-05 23:32:42 +01:00
use crate::calendar::CalendarId;
use crate::calendar::SupportedComponents;
2021-04-04 00:35:59 +02:00
use crate::resource::Resource;
2021-02-18 12:02:04 +01:00
2021-04-03 16:27:04 +02:00
/// This trait must be implemented by data sources (either local caches or remote CalDAV clients)
2021-02-25 00:47:39 +01:00
#[async_trait]
pub trait CalDavSource<T: BaseCalendar> {
2021-02-25 00:47:39 +01:00
/// 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)
2021-03-18 23:59:06 +01:00
async fn get_calendars(&self) -> Result<HashMap<CalendarId, Arc<Mutex<T>>>, Box<dyn Error>>;
2021-03-05 23:32:42 +01:00
/// Returns the calendar matching the ID
2021-03-21 19:27:55 +01:00
async fn get_calendar(&self, id: &CalendarId) -> Option<Arc<Mutex<T>>>;
2021-04-04 00:35:59 +02:00
/// Create a calendar if it did not exist, and return it
async fn create_calendar(&mut self, id: CalendarId, name: String, supported_components: SupportedComponents)
-> Result<Arc<Mutex<T>>, Box<dyn Error>>;
2021-04-19 23:04:12 +02:00
// Removing a calendar is not supported yet
2021-02-18 12:02:04 +01:00
}
2021-02-28 18:00:37 +01:00
/// This trait contains functions that are common to all calendars
2021-03-21 19:05:22 +01:00
#[async_trait]
pub trait BaseCalendar {
2021-03-01 23:39:16 +01:00
/// Returns the calendar name
fn name(&self) -> &str;
2021-03-05 23:32:42 +01:00
/// Returns the calendar unique ID
fn id(&self) -> &CalendarId;
2021-03-01 23:39:16 +01:00
/// Returns the supported kinds of components for this calendar
fn supported_components(&self) -> crate::calendar::SupportedComponents;
2021-04-03 18:32:59 +02:00
/// Add an item into this calendar, and return its new sync status.
/// For local calendars, the sync status is not modified.
/// For remote calendars, the sync status is updated by the server
async fn add_item(&mut self, item: Item) -> Result<SyncStatus, Box<dyn Error>>;
2021-03-01 23:39:16 +01:00
2021-04-18 00:13:39 +02:00
/// Update an item that already exists in this calendar and returns its new `SyncStatus`
/// This replaces a given item at a given URL
async fn update_item(&mut self, item: Item) -> Result<SyncStatus, Box<dyn Error>>;
2021-03-02 00:21:58 +01:00
/// Returns whether this calDAV calendar supports to-do items
fn supports_todo(&self) -> bool {
self.supported_components().contains(crate::calendar::SupportedComponents::TODO)
}
/// Returns whether this calDAV calendar supports calendar items
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 {
2021-04-04 00:35:59 +02:00
/// Create a new calendar
fn new(name: String, resource: Resource, supported_components: SupportedComponents) -> Self;
/// 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(&self, id: &ItemId) -> Result<Option<Item>, Box<dyn Error>>;
/// Delete an item
async fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>>;
2021-03-02 00:21:58 +01:00
/// Get the IDs of all current items in this calendar
async fn get_item_ids(&self) -> Result<HashSet<ItemId>, Box<dyn Error>> {
let items = self.get_item_version_tags().await?;
Ok(items.iter()
.map(|(id, _tag)| id.clone())
.collect())
}
2021-04-17 20:05:44 +02:00
// Note: the CalDAV protocol could also enable to do this:
// fn get_current_version(&self) -> CTag
2021-03-01 23:39:16 +01:00
}
/// Functions availabe for calendars we have full knowledge of
2021-03-01 23:39:16 +01:00
///
/// Usually, these are local calendars fully backed by a local folder
2021-03-21 19:05:22 +01:00
#[async_trait]
pub trait CompleteCalendar : BaseCalendar {
2021-04-03 09:23:20 +02:00
/// Create a new calendar
fn new(name: String, id: CalendarId, supported_components: SupportedComponents) -> Self;
/// 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
2021-03-21 19:58:37 +01:00
async fn get_items(&self) -> Result<HashMap<ItemId, &Item>, Box<dyn Error>>;
2021-03-01 23:39:16 +01:00
2021-03-29 23:46:21 +02:00
/// Returns a particular item
async fn get_item_by_id_ref<'a>(&'a self, id: &ItemId) -> Option<&'a Item>;
/// 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
2021-04-03 16:27:04 +02:00
/// (and then call [`CompleteCalendar::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>>;
2021-04-03 16:27:04 +02:00
/// Immediately remove an item. See [`CompleteCalendar::mark_for_deletion`]
async fn immediately_delete_item(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>>;
}