kitchen-freezer/src/provider.rs

104 lines
3.4 KiB
Rust
Raw Normal View History

2021-02-28 00:19:00 +01:00
//! This modules abstracts data sources and merges them in a single virtual one
2021-03-05 23:32:42 +01:00
use std::error::Error;
2021-03-19 00:08:16 +01:00
use std::collections::HashSet;
2021-03-05 23:32:42 +01:00
use std::marker::PhantomData;
2021-02-28 00:19:00 +01:00
use chrono::{DateTime, Utc};
2021-03-01 23:39:16 +01:00
use crate::traits::{CalDavSource, CompleteCalendar};
use crate::traits::PartialCalendar;
use crate::Item;
use crate::item::ItemId;
2021-02-28 00:19:00 +01:00
2021-02-28 18:00:37 +01:00
/// A data source that combines two `CalDavSources` (usually a server and a local cache), which is able to sync both sources.
2021-03-22 22:39:50 +01:00
pub struct Provider<L, T, R, U>
2021-02-28 00:19:00 +01:00
where
L: CalDavSource<T>,
2021-03-01 23:39:16 +01:00
T: CompleteCalendar,
2021-03-22 22:39:50 +01:00
R: CalDavSource<U>,
2021-03-21 19:19:49 +01:00
U: PartialCalendar + Sync + Send,
2021-02-28 00:19:00 +01:00
{
2021-03-22 22:39:50 +01:00
/// The remote source (usually a server)
remote: R,
2021-02-28 00:19:00 +01:00
/// The local cache
local: L,
2021-03-01 23:39:16 +01:00
phantom_t: PhantomData<T>,
phantom_u: PhantomData<U>,
2021-02-28 00:19:00 +01:00
}
2021-03-22 22:39:50 +01:00
impl<L, T, R, U> Provider<L, T, R, U>
2021-02-28 00:19:00 +01:00
where
L: CalDavSource<T>,
2021-03-01 23:39:16 +01:00
T: CompleteCalendar,
2021-03-22 22:39:50 +01:00
R: CalDavSource<U>,
2021-03-21 19:19:49 +01:00
U: PartialCalendar + Sync + Send,
2021-02-28 00:19:00 +01:00
{
2021-02-28 18:02:01 +01:00
/// Create a provider.
///
2021-03-22 22:39:50 +01:00
/// `remote` is usually a [`Client`](crate::client::Client), `local` is usually a [`Cache`](crate::cache::Cache).
/// However, both can be interchangeable. The only difference is that `remote` always wins in case of a sync conflict
pub fn new(remote: R, local: L) -> Self {
Self { remote, local,
2021-03-01 23:39:16 +01:00
phantom_t: PhantomData, phantom_u: PhantomData,
}
2021-02-28 00:19:00 +01:00
}
2021-02-28 18:02:01 +01:00
/// Returns the data source described as the `server`
2021-03-22 22:39:50 +01:00
pub fn remote(&self) -> &R { &self.remote }
2021-02-28 18:02:01 +01:00
/// Returns the data source described as the `local`
2021-02-28 00:19:00 +01:00
pub fn local(&self) -> &L { &self.local }
2021-02-28 18:02:01 +01:00
/// Performs a synchronisation between `local` and `server`.
///
2021-03-20 20:12:48 +01:00
/// This bidirectional sync applies additions/deletions made on a source to the other source.
2021-02-28 18:02:01 +01:00
/// In case of conflicts (the same item has been modified on both ends since the last sync, `server` always wins)
2021-02-28 00:19:00 +01:00
pub async fn sync(&mut self) -> Result<(), Box<dyn Error>> {
log::info!("Starting a sync.");
2021-03-22 22:37:29 +01:00
2021-03-22 22:39:50 +01:00
let cals_remote = self.remote.get_calendars().await?;
for (id, cal_remote) in cals_remote {
let mut cal_remote = cal_remote.lock().unwrap();
2021-03-18 23:59:06 +01:00
2021-03-21 19:27:55 +01:00
let cal_local = match self.local.get_calendar(&id).await {
2021-02-28 00:19:00 +01:00
None => {
log::error!("TODO: implement here");
continue;
},
Some(cal) => cal,
};
2021-03-18 23:59:06 +01:00
let mut cal_local = cal_local.lock().unwrap();
2021-02-28 00:19:00 +01:00
2021-03-22 22:37:29 +01:00
// Step 1 - find the differences
// let mut local_del = HashSet::new();
// let mut remote_del = HashSet::new();
// let mut local_changes = HashSet::new();
// let mut remote_change = HashSet::new();
// let mut local_additions = HashSet::new();
// let mut remote_additions = HashSet::new();
2021-02-28 00:19:00 +01:00
}
Ok(())
}
}
2021-03-21 19:19:49 +01:00
async fn move_to_calendar<C: PartialCalendar>(items: &mut Vec<Item>, calendar: &mut C) {
while items.len() > 0 {
let item = items.remove(0);
2021-03-20 20:12:48 +01:00
log::warn!("Moving {} to calendar", item.name());
2021-03-21 19:19:49 +01:00
calendar.add_item(item).await;
2021-02-28 00:19:00 +01:00
}
}
2021-03-21 19:19:49 +01:00
async fn remove_from_calendar<C: PartialCalendar>(ids: &Vec<ItemId>, calendar: &mut C) {
2021-02-28 00:19:00 +01:00
for id in ids {
2021-03-20 20:12:48 +01:00
log::info!(" Removing {:?} from calendar", id);
2021-03-21 19:19:49 +01:00
if let Err(err) = calendar.delete_item(id).await {
2021-03-19 00:08:16 +01:00
log::warn!("Unable to delete item {:?} from calendar: {}", id, err);
}
2021-02-28 00:19:00 +01:00
}
}