Provider-sync example

This commit is contained in:
daladim 2021-04-13 23:30:43 +02:00
parent 36f08756bd
commit e39cdc7cd5
3 changed files with 186 additions and 142 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/target /target
/test_cache /test_cache
/example_cache

View file

@ -1,11 +1,17 @@
use std::path::Path; use std::path::Path;
use my_tasks::{client::Client, traits::CalDavSource}; use my_tasks::{client::Client, traits::CalDavSource};
use my_tasks::calendar::{CalendarId, cached_calendar::CachedCalendar, remote_calendar::RemoteCalendar};
use my_tasks::Item;
use my_tasks::Task;
use my_tasks::cache::Cache; use my_tasks::cache::Cache;
use my_tasks::Provider; use my_tasks::Provider;
use my_tasks::traits::BaseCalendar;
use my_tasks::settings::URL; use my_tasks::settings::URL;
use my_tasks::settings::USERNAME; use my_tasks::settings::USERNAME;
use my_tasks::settings::PASSWORD; use my_tasks::settings::PASSWORD;
use my_tasks::settings::EXAMPLE_CALENDAR_URL;
use my_tasks::utils::pause;
const CACHE_FOLDER: &str = "example_cache"; const CACHE_FOLDER: &str = "example_cache";
@ -14,7 +20,10 @@ const CACHE_FOLDER: &str = "example_cache";
async fn main() { async fn main() {
env_logger::init(); env_logger::init();
println!("This examples show how to sync a remote server with a local cache, using a Provider"); println!("This examples show how to sync a remote server with a local cache, using a Provider.");
println!("Make sure you have edited your settings.rs to include correct URLs and credentials.");
println!("You can also set the RUST_LOG environment variable to display more info about the sync.");
pause();
let cache_path = Path::new(CACHE_FOLDER); let cache_path = Path::new(CACHE_FOLDER);
@ -32,11 +41,36 @@ async fn main() {
println!("---- before sync -----"); println!("---- before sync -----");
my_tasks::utils::print_calendar_list(&cals).await; my_tasks::utils::print_calendar_list(&cals).await;
println!("Starting a sync...");
if provider.sync().await == false { if provider.sync().await == false {
log::warn!("Sync did not complete, see the previous log lines for more info. You can safely start a new sync."); log::warn!("Sync did not complete, see the previous log lines for more info. You can safely start a new sync.");
} }
provider.local().save_to_folder().unwrap();
println!("---- after sync -----"); println!("---- after sync -----");
let cals = provider.local().get_calendars().await.unwrap(); let cals = provider.local().get_calendars().await.unwrap();
my_tasks::utils::print_calendar_list(&cals).await; my_tasks::utils::print_calendar_list(&cals).await;
add_items_and_sync_again(&mut provider).await;
}
async fn add_items_and_sync_again(provider: &mut Provider<Cache, CachedCalendar, Client, RemoteCalendar>) {
println!("Now, we'll add a task and run the sync again.");
pause();
let changed_calendar_id: CalendarId = EXAMPLE_CALENDAR_URL.parse().unwrap();
let changed_calendar = provider.local().get_calendar(&changed_calendar_id).await.unwrap();
let new_name = "New example task";
let new_task = Task::new(String::from(new_name), false, &changed_calendar_id);
changed_calendar.lock().unwrap().add_item(Item::Task(new_task)).await.unwrap();
if provider.sync().await == false {
log::warn!("Sync did not complete, see the previous log lines for more info. You can safely start a new sync. The new task may not have been synced.");
} else {
println!("Done syncing the new task '{}'", new_name);
}
provider.local().save_to_folder().unwrap();
println!("Done. You can start this example again to see the cache being restored from its current saved state")
} }

View file

@ -1,141 +1,150 @@
///! Some utility functions ///! Some utility functions
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::hash::Hash; use std::hash::Hash;
use std::io::{stdin, stdout, Read, Write};
use minidom::Element;
use minidom::Element;
use crate::traits::CompleteCalendar;
use crate::traits::DavCalendar; use crate::traits::CompleteCalendar;
use crate::calendar::CalendarId; use crate::traits::DavCalendar;
use crate::Item; use crate::calendar::CalendarId;
use crate::item::SyncStatus; use crate::Item;
use crate::item::SyncStatus;
/// Walks an XML tree and returns every element that has the given name
pub fn find_elems<S: AsRef<str>>(root: &Element, searched_name: S) -> Vec<&Element> { /// Walks an XML tree and returns every element that has the given name
let searched_name = searched_name.as_ref(); pub fn find_elems<S: AsRef<str>>(root: &Element, searched_name: S) -> Vec<&Element> {
let mut elems: Vec<&Element> = Vec::new(); let searched_name = searched_name.as_ref();
let mut elems: Vec<&Element> = Vec::new();
for el in root.children() {
if el.name() == searched_name { for el in root.children() {
elems.push(el); if el.name() == searched_name {
} else { elems.push(el);
let ret = find_elems(el, searched_name); } else {
elems.extend(ret); let ret = find_elems(el, searched_name);
} elems.extend(ret);
} }
elems }
} elems
}
/// Walks an XML tree until it finds an elements with the given name
pub fn find_elem<S: AsRef<str>>(root: &Element, searched_name: S) -> Option<&Element> { /// Walks an XML tree until it finds an elements with the given name
let searched_name = searched_name.as_ref(); pub fn find_elem<S: AsRef<str>>(root: &Element, searched_name: S) -> Option<&Element> {
if root.name() == searched_name { let searched_name = searched_name.as_ref();
return Some(root); if root.name() == searched_name {
} return Some(root);
}
for el in root.children() {
if el.name() == searched_name { for el in root.children() {
return Some(el); if el.name() == searched_name {
} else { return Some(el);
let ret = find_elem(el, searched_name); } else {
if ret.is_some() { let ret = find_elem(el, searched_name);
return ret; if ret.is_some() {
} return ret;
} }
} }
None }
} None
}
pub fn print_xml(element: &Element) {
use std::io::Write; pub fn print_xml(element: &Element) {
let mut writer = std::io::stdout(); let mut writer = std::io::stdout();
let mut xml_writer = minidom::quick_xml::Writer::new_with_indent( let mut xml_writer = minidom::quick_xml::Writer::new_with_indent(
std::io::stdout(), std::io::stdout(),
0x20, 4 0x20, 4
); );
let _ = element.to_writer(&mut xml_writer); let _ = element.to_writer(&mut xml_writer);
let _ = writer.write(&[0x0a]); let _ = writer.write(&[0x0a]);
} }
/// A debug utility that pretty-prints calendars /// A debug utility that pretty-prints calendars
pub async fn print_calendar_list<C>(cals: &HashMap<CalendarId, Arc<Mutex<C>>>) pub async fn print_calendar_list<C>(cals: &HashMap<CalendarId, Arc<Mutex<C>>>)
where where
C: CompleteCalendar, C: CompleteCalendar,
{ {
for (id, cal) in cals { for (id, cal) in cals {
println!("CAL {} ({})", cal.lock().unwrap().name(), id); println!("CAL {} ({})", cal.lock().unwrap().name(), id);
match cal.lock().unwrap().get_items().await { match cal.lock().unwrap().get_items().await {
Err(_err) => continue, Err(_err) => continue,
Ok(map) => { Ok(map) => {
for (_, item) in map { for (_, item) in map {
print_task(item); print_task(item);
} }
}, },
} }
} }
} }
/// A debug utility that pretty-prints calendars /// A debug utility that pretty-prints calendars
pub async fn print_dav_calendar_list<C>(cals: &HashMap<CalendarId, Arc<Mutex<C>>>) pub async fn print_dav_calendar_list<C>(cals: &HashMap<CalendarId, Arc<Mutex<C>>>)
where where
C: DavCalendar, C: DavCalendar,
{ {
for (id, cal) in cals { for (id, cal) in cals {
println!("CAL {} ({})", cal.lock().unwrap().name(), id); println!("CAL {} ({})", cal.lock().unwrap().name(), id);
match cal.lock().unwrap().get_item_version_tags().await { match cal.lock().unwrap().get_item_version_tags().await {
Err(_err) => continue, Err(_err) => continue,
Ok(map) => { Ok(map) => {
for (id, version_tag) in map { for (id, version_tag) in map {
println!(" * {} (version {:?})", id, version_tag); println!(" * {} (version {:?})", id, version_tag);
} }
}, },
} }
} }
} }
pub fn print_task(item: &Item) { pub fn print_task(item: &Item) {
match item { match item {
Item::Task(task) => { Item::Task(task) => {
let completion = if task.completed() { "" } else { " " }; let completion = if task.completed() { "" } else { " " };
let sync = match task.sync_status() { let sync = match task.sync_status() {
SyncStatus::NotSynced => ".", SyncStatus::NotSynced => ".",
SyncStatus::Synced(_) => "=", SyncStatus::Synced(_) => "=",
SyncStatus::LocallyModified(_) => "~", SyncStatus::LocallyModified(_) => "~",
SyncStatus::LocallyDeleted(_) => "x", SyncStatus::LocallyDeleted(_) => "x",
}; };
println!(" {}{} {}\t{}", completion, sync, task.name(), task.id()); println!(" {}{} {}\t{}", completion, sync, task.name(), task.id());
}, },
_ => return, _ => return,
} }
} }
/// Compare keys of two hashmaps for equality /// Compare keys of two hashmaps for equality
pub fn keys_are_the_same<T, U, V>(left: &HashMap<T, U>, right: &HashMap<T, V>) -> bool pub fn keys_are_the_same<T, U, V>(left: &HashMap<T, U>, right: &HashMap<T, V>) -> bool
where where
T: Hash + Eq + Clone + std::fmt::Display, T: Hash + Eq + Clone + std::fmt::Display,
{ {
if left.len() != right.len() { if left.len() != right.len() {
log::debug!("Count of keys mismatch: {} and {}", left.len(), right.len()); log::debug!("Count of keys mismatch: {} and {}", left.len(), right.len());
return false; return false;
} }
let keys_l: HashSet<T> = left.keys().cloned().collect(); let keys_l: HashSet<T> = left.keys().cloned().collect();
let keys_r: HashSet<T> = right.keys().cloned().collect(); let keys_r: HashSet<T> = right.keys().cloned().collect();
let result = keys_l == keys_r; let result = keys_l == keys_r;
if result == false { if result == false {
log::debug!("Keys of a map mismatch"); log::debug!("Keys of a map mismatch");
for key in keys_l { for key in keys_l {
log::debug!(" left: {}", key); log::debug!(" left: {}", key);
} }
log::debug!("RIGHT:"); log::debug!("RIGHT:");
for key in keys_r { for key in keys_r {
log::debug!(" right: {}", key); log::debug!(" right: {}", key);
} }
} }
result result
} }
/// Wait for the user to press enter
pub fn pause() {
let mut stdout = stdout();
stdout.write_all(b"Press Enter to continue...").unwrap();
stdout.flush().unwrap();
stdin().read_exact(&mut [0]).unwrap();
}