kitchen-freezer/src/traits.rs

95 lines
3.7 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-02-18 12:02:04 +01:00
2021-02-25 00:47:39 +01:00
use async_trait::async_trait;
2021-02-28 18:00:37 +01:00
use chrono::{DateTime, Utc};
2021-02-18 12:02:04 +01:00
2021-03-01 23:39:16 +01:00
use crate::item::Item;
use crate::item::ItemId;
2021-03-05 23:32:42 +01:00
use crate::calendar::CalendarId;
2021-02-18 12:02:04 +01:00
2021-02-25 00:47:39 +01:00
#[async_trait]
2021-03-01 23:39:16 +01:00
pub trait CalDavSource<T: PartialCalendar> {
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-05 23:32:42 +01:00
async fn get_calendars(&self) -> Result<&HashMap<CalendarId, T>, Box<dyn Error>>;
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-05 23:32:42 +01:00
async fn get_calendars_mut(&mut self) -> Result<HashMap<CalendarId, &mut T>, Box<dyn Error>>;
2021-02-26 17:55:23 +01:00
2021-03-05 23:32:42 +01:00
/// 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>;
2021-02-26 17:55:23 +01:00
2021-02-18 12:02:04 +01:00
}
2021-02-28 18:00:37 +01:00
pub trait SyncSlave {
/// Returns the last time this source successfully synced from a master source (e.g. from a server)
/// (or None in case it has never been synchronized)
fn get_last_sync(&self) -> Option<DateTime<Utc>>;
/// Update the last sync timestamp to now, or to a custom time in case `timepoint` is `Some`
fn update_last_sync(&mut self, timepoint: Option<DateTime<Utc>>);
}
2021-03-01 23:39:16 +01:00
/// A calendar we have a partial knowledge of.
///
/// Usually, this is a calendar from a remote source, that is synced to a CompleteCalendar
pub trait PartialCalendar {
/// 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;
/// 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>)
-> HashMap<ItemId, &Item>;
2021-03-02 00:21:58 +01:00
/// Get the IDs of all current items in this calendar
2021-03-05 23:32:42 +01:00
fn get_item_ids(&mut self) -> HashSet<ItemId>;
2021-03-02 00:21:58 +01:00
2021-03-01 23:39:16 +01:00
/// Returns a particular item
fn get_item_by_id_mut(&mut self, id: &ItemId) -> Option<&mut Item>;
/// Add an item into this calendar
fn add_item(&mut self, item: Item);
/// Remove an item from this calendar
2021-03-05 23:32:42 +01:00
fn delete_item(&mut self, item_id: &ItemId) -> Result<(), Box<dyn Error>>;
2021-03-01 23:39:16 +01:00
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)
}
/// Finds the IDs of the items that are missing compared to a reference set
2021-03-05 23:32:42 +01:00
fn find_deletions_from(&mut self, reference_set: HashSet<ItemId>) -> HashSet<ItemId> {
2021-03-02 00:21:58 +01:00
let current_items = self.get_item_ids();
2021-03-05 23:32:42 +01:00
reference_set.difference(&current_items).map(|id| id.clone()).collect()
2021-03-02 00:21:58 +01:00
}
2021-03-01 23:39:16 +01:00
}
/// A calendar we always know everything about.
///
/// Usually, this is a calendar fully stored on a local disk
pub trait CompleteCalendar : PartialCalendar {
/// Returns the items that have been deleted after `since`
///
/// See also [`PartialCalendar::get_items_deleted_since`]
2021-03-05 23:32:42 +01:00
fn get_items_deleted_since(&self, since: DateTime<Utc>) -> HashSet<ItemId>;
2021-03-01 23:39:16 +01:00
/// Returns the list of items that this calendar contains
fn get_items(&self) -> HashMap<ItemId, &Item>;
}