toggle_completions/
toggle-completions.rs

1//! This is an example of how kitchen-fridge can be used.
2//! This binary simply toggles all completion statuses of the tasks it finds.
3
4use std::error::Error;
5
6use chrono::Utc;
7
8use kitchen_fridge::item::Item;
9use kitchen_fridge::task::CompletionStatus;
10use kitchen_fridge::CalDavProvider;
11use kitchen_fridge::utils::pause;
12
13mod shared;
14use shared::initial_sync;
15use shared::{URL, USERNAME};
16
17const CACHE_FOLDER: &str = "test_cache/toggle_completion";
18
19#[tokio::main]
20async fn main() {
21    env_logger::init();
22
23    println!("This example show how to sync a remote server with a local cache, using a Provider.");
24    println!("Make sure you have edited the constants in the 'shared.rs' file to include correct URLs and credentials.");
25    println!("You can also set the RUST_LOG environment variable to display more info about the sync.");
26    println!("");
27    println!("This will use the following settings:");
28    println!("  * URL = {}", URL);
29    println!("  * USERNAME = {}", USERNAME);
30    pause();
31
32    let mut provider = initial_sync(CACHE_FOLDER).await;
33
34    toggle_all_tasks_and_sync_again(&mut provider).await.unwrap();
35}
36
37async fn toggle_all_tasks_and_sync_again(provider: &mut CalDavProvider) -> Result<(), Box<dyn Error>> {
38    let mut n_toggled = 0;
39
40    for (_url, cal) in provider.local().get_calendars_sync()?.iter() {
41        for (_url, item) in cal.lock().unwrap().get_items_mut_sync()?.iter_mut() {
42            match item {
43                Item::Task(task) => {
44                    match task.completed() {
45                        false => task.set_completion_status(CompletionStatus::Completed(Some(Utc::now()))),
46                        true => task.set_completion_status(CompletionStatus::Uncompleted),
47                    };
48                    n_toggled += 1;
49                }
50                Item::Event(_) => {
51                    // Not doing anything with calendar events
52                },
53            }
54        }
55    }
56
57    println!("{} items toggled.", n_toggled);
58    println!("Syncing...");
59
60    provider.sync().await;
61
62    println!("Syncing complete.");
63
64    Ok(())
65}