use std::error::Error;
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex};
use async_trait::async_trait;
use csscolorparser::Color;
use url::Url;
use crate::item::SyncStatus;
use crate::item::Item;
use crate::item::VersionTag;
use crate::calendar::SupportedComponents;
use crate::resource::Resource;
#[async_trait]
pub trait CalDavSource<T: BaseCalendar> {
async fn get_calendars(&self) -> Result<HashMap<Url, Arc<Mutex<T>>>, Box<dyn Error>>;
async fn get_calendar(&self, url: &Url) -> Option<Arc<Mutex<T>>>;
async fn create_calendar(&mut self, url: Url, name: String, supported_components: SupportedComponents, color: Option<Color>)
-> Result<Arc<Mutex<T>>, Box<dyn Error>>;
}
#[async_trait]
pub trait BaseCalendar {
fn name(&self) -> &str;
fn url(&self) -> &Url;
fn supported_components(&self) -> crate::calendar::SupportedComponents;
fn color(&self) -> Option<&Color>;
async fn add_item(&mut self, item: Item) -> Result<SyncStatus, Box<dyn Error>>;
async fn update_item(&mut self, item: Item) -> Result<SyncStatus, Box<dyn Error>>;
fn supports_todo(&self) -> bool {
self.supported_components().contains(crate::calendar::SupportedComponents::TODO)
}
fn supports_events(&self) -> bool {
self.supported_components().contains(crate::calendar::SupportedComponents::EVENT)
}
}
#[async_trait]
pub trait DavCalendar : BaseCalendar {
fn new(name: String, resource: Resource, supported_components: SupportedComponents, color: Option<Color>) -> Self;
async fn get_item_version_tags(&self) -> Result<HashMap<Url, VersionTag>, Box<dyn Error>>;
async fn get_item_by_url(&self, url: &Url) -> Result<Option<Item>, Box<dyn Error>>;
async fn get_items_by_url(&self, urls: &[Url]) -> Result<Vec<Option<Item>>, Box<dyn Error>>;
async fn delete_item(&mut self, item_url: &Url) -> Result<(), Box<dyn Error>>;
async fn get_item_urls(&self) -> Result<HashSet<Url>, Box<dyn Error>> {
let items = self.get_item_version_tags().await?;
Ok(items.iter()
.map(|(url, _tag)| url.clone())
.collect())
}
}
#[async_trait]
pub trait CompleteCalendar : BaseCalendar {
fn new(name: String, url: Url, supported_components: SupportedComponents, color: Option<Color>) -> Self;
async fn get_item_urls(&self) -> Result<HashSet<Url>, Box<dyn Error>>;
async fn get_items(&self) -> Result<HashMap<Url, &Item>, Box<dyn Error>>;
async fn get_items_mut(&mut self) -> Result<HashMap<Url, &mut Item>, Box<dyn Error>>;
async fn get_item_by_url<'a>(&'a self, url: &Url) -> Option<&'a Item>;
async fn get_item_by_url_mut<'a>(&'a mut self, url: &Url) -> Option<&'a mut Item>;
async fn mark_for_deletion(&mut self, item_id: &Url) -> Result<(), Box<dyn Error>>;
async fn immediately_delete_item(&mut self, item_id: &Url) -> Result<(), Box<dyn Error>>;
}