shared/
shared.rs

1use std::path::Path;
2
3use kitchen_fridge::client::Client;
4use kitchen_fridge::traits::CalDavSource;
5use kitchen_fridge::CalDavProvider;
6use kitchen_fridge::cache::Cache;
7
8
9// TODO: change these values with yours
10pub const URL: &str = "https://my.server.com/remote.php/dav/files/john";
11pub const USERNAME: &str = "username";
12pub const PASSWORD: &str = "secret_password";
13
14pub const EXAMPLE_EXISTING_CALENDAR_URL: &str = "https://my.server.com/remote.php/dav/calendars/john/a_calendar_name/";
15pub const EXAMPLE_CREATED_CALENDAR_URL: &str =  "https://my.server.com/remote.php/dav/calendars/john/a_calendar_that_we_have_created/";
16
17fn main() {
18    panic!("This file is not supposed to be executed");
19}
20
21
22/// Initializes a Provider, and run an initial sync from the server
23pub async fn initial_sync(cache_folder: &str) -> CalDavProvider {
24    let cache_path = Path::new(cache_folder);
25
26    let client = Client::new(URL, USERNAME, PASSWORD).unwrap();
27    let cache = match Cache::from_folder(&cache_path) {
28        Ok(cache) => cache,
29        Err(err) => {
30            log::warn!("Invalid cache file: {}. Using a default cache", err);
31            Cache::new(&cache_path)
32        }
33    };
34    let mut provider = CalDavProvider::new(client, cache);
35
36
37    let cals = provider.local().get_calendars().await.unwrap();
38    println!("---- Local items, before sync -----");
39    kitchen_fridge::utils::print_calendar_list(&cals).await;
40
41    println!("Starting a sync...");
42    println!("Depending on your RUST_LOG value, you may see more or less details about the progress.");
43    // Note that we could use sync_with_feedback() to have better and formatted feedback
44    if provider.sync().await == false {
45        log::warn!("Sync did not complete, see the previous log lines for more info. You can safely start a new sync.");
46    }
47    provider.local().save_to_folder().unwrap();
48
49    println!("---- Local items, after sync -----");
50    let cals = provider.local().get_calendars().await.unwrap();
51    kitchen_fridge::utils::print_calendar_list(&cals).await;
52
53    provider
54}