kitchen-freezer/examples/provider-sync.rs

132 lines
4.9 KiB
Rust
Raw Normal View History

2021-03-01 23:39:16 +01:00
use std::path::Path;
2021-04-17 19:31:09 +02:00
use chrono::{Utc};
2021-03-01 23:39:16 +01:00
use my_tasks::{client::Client, traits::CalDavSource};
2021-04-13 23:30:43 +02:00
use my_tasks::calendar::{CalendarId, cached_calendar::CachedCalendar, remote_calendar::RemoteCalendar};
use my_tasks::Item;
use my_tasks::Task;
2021-04-17 19:31:09 +02:00
use my_tasks::task::CompletionStatus;
use my_tasks::ItemId;
2021-03-01 23:39:16 +01:00
use my_tasks::cache::Cache;
use my_tasks::Provider;
2021-04-13 23:30:43 +02:00
use my_tasks::traits::BaseCalendar;
2021-04-17 19:31:09 +02:00
use my_tasks::traits::CompleteCalendar;
2021-02-24 23:49:20 +01:00
use my_tasks::settings::URL;
use my_tasks::settings::USERNAME;
use my_tasks::settings::PASSWORD;
2021-04-13 23:30:43 +02:00
use my_tasks::settings::EXAMPLE_CALENDAR_URL;
use my_tasks::utils::pause;
2021-02-24 23:49:20 +01:00
2021-04-19 08:30:34 +02:00
const CACHE_FOLDER: &str = "test_cache/provider_sync";
2021-03-01 23:39:16 +01:00
2021-02-24 23:49:20 +01:00
#[tokio::main]
async fn main() {
env_logger::init();
2021-04-13 23:30:43 +02:00
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();
2021-04-11 19:15:04 +02:00
let cache_path = Path::new(CACHE_FOLDER);
2021-02-24 23:49:20 +01:00
let client = Client::new(URL, USERNAME, PASSWORD).unwrap();
let cache = match Cache::from_folder(&cache_path) {
2021-03-01 23:39:16 +01:00
Ok(cache) => cache,
Err(err) => {
log::warn!("Invalid cache file: {}. Using a default cache", err);
Cache::new(&cache_path)
}
};
let mut provider = Provider::new(client, cache);
2021-03-01 23:39:16 +01:00
let cals = provider.local().get_calendars().await.unwrap();
2021-04-17 19:31:09 +02:00
println!("---- Local items, before sync -----");
my_tasks::utils::print_calendar_list(&cals).await;
2021-03-01 23:39:16 +01:00
2021-04-13 23:30:43 +02:00
println!("Starting a sync...");
2021-04-11 19:15:04 +02:00
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.");
}
2021-04-13 23:30:43 +02:00
provider.local().save_to_folder().unwrap();
2021-04-11 19:15:04 +02:00
2021-04-17 19:31:09 +02:00
println!("---- Local items, after sync -----");
let cals = provider.local().get_calendars().await.unwrap();
my_tasks::utils::print_calendar_list(&cals).await;
2021-04-13 23:30:43 +02:00
2021-04-17 19:31:09 +02:00
let changed_calendar_id: CalendarId = EXAMPLE_CALENDAR_URL.parse().unwrap();
add_items_and_sync_again(&mut provider, &changed_calendar_id).await;
2021-04-13 23:30:43 +02:00
}
2021-04-17 19:31:09 +02:00
async fn add_items_and_sync_again(
provider: &mut Provider<Cache, CachedCalendar, Client, RemoteCalendar>,
changed_calendar_id: &CalendarId)
{
println!("\nNow, we'll add a task and run the sync again.");
2021-04-13 23:30:43 +02:00
pause();
2021-04-17 19:31:09 +02:00
let new_name = "This is a new task we're adding as an example, with ÜTF-8 characters";
2021-04-13 23:30:43 +02:00
let new_task = Task::new(String::from(new_name), false, &changed_calendar_id);
2021-04-17 19:31:09 +02:00
let new_id = new_task.id().clone();
provider.local().get_calendar(changed_calendar_id).await.unwrap()
.lock().unwrap().add_item(Item::Task(new_task)).await.unwrap();
2021-04-13 23:30:43 +02:00
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();
2021-04-17 19:31:09 +02:00
complete_item_and_sync_again(provider, changed_calendar_id, &new_id).await;
}
async fn complete_item_and_sync_again(
provider: &mut Provider<Cache, CachedCalendar, Client, RemoteCalendar>,
changed_calendar_id: &CalendarId,
id_to_complete: &ItemId)
{
println!("\nNow, we'll mark this last task as completed, and run the sync again.");
pause();
let completion_status = CompletionStatus::Completed(Some(Utc::now()));
provider.local().get_calendar(changed_calendar_id).await.unwrap()
.lock().unwrap().get_item_by_id_mut(id_to_complete).await.unwrap()
.unwrap_task_mut()
.set_completion_status(completion_status);
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 completed task");
}
provider.local().save_to_folder().unwrap();
remove_item_and_sync_again(provider, changed_calendar_id, id_to_complete).await;
}
async fn remove_item_and_sync_again(
provider: &mut Provider<Cache, CachedCalendar, Client, RemoteCalendar>,
changed_calendar_id: &CalendarId,
id_to_remove: &ItemId)
{
println!("\nNow, we'll delete this last task, and run the sync again.");
pause();
provider.local().get_calendar(changed_calendar_id).await.unwrap()
.lock().unwrap()
.mark_for_deletion(id_to_remove).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 deleted task");
}
provider.local().save_to_folder().unwrap();
2021-04-13 23:30:43 +02:00
println!("Done. You can start this example again to see the cache being restored from its current saved state")
2021-02-24 23:49:20 +01:00
}