provider_sync/
provider-sync.rs1use chrono::{Utc};
4use url::Url;
5
6use kitchen_fridge::traits::CalDavSource;
7use kitchen_fridge::calendar::SupportedComponents;
8use kitchen_fridge::Item;
9use kitchen_fridge::Task;
10use kitchen_fridge::task::CompletionStatus;
11use kitchen_fridge::CalDavProvider;
12use kitchen_fridge::traits::BaseCalendar;
13use kitchen_fridge::traits::CompleteCalendar;
14use kitchen_fridge::utils::pause;
15
16mod shared;
17use shared::initial_sync;
18use shared::{URL, USERNAME, EXAMPLE_EXISTING_CALENDAR_URL, EXAMPLE_CREATED_CALENDAR_URL};
19
20const CACHE_FOLDER: &str = "test_cache/provider_sync";
21
22
23#[tokio::main]
24async fn main() {
25 env_logger::init();
26
27 println!("This example show how to sync a remote server with a local cache, using a Provider.");
28 println!("Make sure you have edited the constants in the 'shared.rs' file to include correct URLs and credentials.");
29 println!("You can also set the RUST_LOG environment variable to display more info about the sync.");
30 println!("");
31 println!("This will use the following settings:");
32 println!(" * URL = {}", URL);
33 println!(" * USERNAME = {}", USERNAME);
34 println!(" * EXAMPLE_EXISTING_CALENDAR_URL = {}", EXAMPLE_EXISTING_CALENDAR_URL);
35 println!(" * EXAMPLE_CREATED_CALENDAR_URL = {}", EXAMPLE_CREATED_CALENDAR_URL);
36 pause();
37
38 let mut provider = initial_sync(CACHE_FOLDER).await;
39
40 add_items_and_sync_again(&mut provider).await;
41}
42
43async fn add_items_and_sync_again(provider: &mut CalDavProvider) {
44 println!("\nNow, we'll add a calendar and a few tasks and run the sync again.");
45 pause();
46
47 let new_calendar_url: Url = EXAMPLE_CREATED_CALENDAR_URL.parse().unwrap();
49 let new_calendar_name = "A brave new calendar".to_string();
50 if let Err(_err) = provider.local_mut()
51 .create_calendar(new_calendar_url.clone(), new_calendar_name.clone(), SupportedComponents::TODO, Some("#ff8000".parse().unwrap()))
52 .await {
53 println!("Unable to add calendar, maybe it exists already. We're not adding it after all.");
54 }
55
56 let new_name = "This is a new task in a new calendar";
58 let new_task = Task::new(String::from(new_name), true, &new_calendar_url);
59 provider.local().get_calendar(&new_calendar_url).await.unwrap()
60 .lock().unwrap().add_item(Item::Task(new_task)).await.unwrap();
61
62
63 let changed_calendar_url: Url = EXAMPLE_EXISTING_CALENDAR_URL.parse().unwrap();
65 let new_task_name = "This is a new task we're adding as an example, with ÜTF-8 characters";
66 let new_task = Task::new(String::from(new_task_name), false, &changed_calendar_url);
67 let new_url = new_task.url().clone();
68 provider.local().get_calendar(&changed_calendar_url).await.unwrap()
69 .lock().unwrap().add_item(Item::Task(new_task)).await.unwrap();
70
71
72 if provider.sync().await == false {
73 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.");
74 } else {
75 println!("Done syncing the new task '{}' and the new calendar '{}'", new_task_name, new_calendar_name);
76 }
77 provider.local().save_to_folder().unwrap();
78
79 complete_item_and_sync_again(provider, &changed_calendar_url, &new_url).await;
80}
81
82async fn complete_item_and_sync_again(
83 provider: &mut CalDavProvider,
84 changed_calendar_url: &Url,
85 url_to_complete: &Url)
86{
87 println!("\nNow, we'll mark this last task as completed, and run the sync again.");
88 pause();
89
90 let completion_status = CompletionStatus::Completed(Some(Utc::now()));
91 provider.local().get_calendar(changed_calendar_url).await.unwrap()
92 .lock().unwrap().get_item_by_url_mut(url_to_complete).await.unwrap()
93 .unwrap_task_mut()
94 .set_completion_status(completion_status);
95
96 if provider.sync().await == false {
97 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.");
98 } else {
99 println!("Done syncing the completed task");
100 }
101 provider.local().save_to_folder().unwrap();
102
103 remove_items_and_sync_again(provider, changed_calendar_url, url_to_complete).await;
104}
105
106async fn remove_items_and_sync_again(
107 provider: &mut CalDavProvider,
108 changed_calendar_url: &Url,
109 id_to_remove: &Url)
110{
111 println!("\nNow, we'll delete this last task, and run the sync again.");
112 pause();
113
114 provider.local().get_calendar(changed_calendar_url).await.unwrap()
116 .lock().unwrap()
117 .mark_for_deletion(id_to_remove).await.unwrap();
118
119 if provider.sync().await == false {
120 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.");
121 } else {
122 println!("Done syncing the deleted task");
123 }
124 provider.local().save_to_folder().unwrap();
125
126 println!("Done. You can start this example again to see the cache being restored from its current saved state")
127}