Added a second example
This commit is contained in:
parent
94b3bb27ba
commit
039965966f
3 changed files with 127 additions and 44 deletions
|
@ -1,40 +1,29 @@
|
|||
//! This is an example of how kitchen-fridge can be used
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use chrono::{Utc};
|
||||
use url::Url;
|
||||
|
||||
use kitchen_fridge::{client::Client, traits::CalDavSource};
|
||||
use kitchen_fridge::traits::CalDavSource;
|
||||
use kitchen_fridge::calendar::SupportedComponents;
|
||||
use kitchen_fridge::Item;
|
||||
use kitchen_fridge::Task;
|
||||
use kitchen_fridge::task::CompletionStatus;
|
||||
use kitchen_fridge::cache::Cache;
|
||||
use kitchen_fridge::CalDavProvider;
|
||||
use kitchen_fridge::traits::BaseCalendar;
|
||||
use kitchen_fridge::traits::CompleteCalendar;
|
||||
use kitchen_fridge::utils::pause;
|
||||
|
||||
const CACHE_FOLDER: &str = "test_cache/provider_sync";
|
||||
|
||||
|
||||
// TODO: change these values with yours
|
||||
pub const URL: &str = "https://my.server.com/remote.php/dav/files/john";
|
||||
pub const USERNAME: &str = "username";
|
||||
pub const PASSWORD: &str = "secret_password";
|
||||
|
||||
pub const EXAMPLE_EXISTING_CALENDAR_URL: &str = "https://my.server.com/remote.php/dav/calendars/john/a_calendar_name/";
|
||||
pub const EXAMPLE_CREATED_CALENDAR_URL: &str = "https://my.server.com/remote.php/dav/calendars/john/a_calendar_that_we_have_created/";
|
||||
|
||||
mod shared;
|
||||
use shared::initial_sync;
|
||||
use shared::{URL, USERNAME, EXAMPLE_EXISTING_CALENDAR_URL, EXAMPLE_CREATED_CALENDAR_URL};
|
||||
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
env_logger::init();
|
||||
|
||||
println!("This examples show how to sync a remote server with a local cache, using a Provider.");
|
||||
println!("Make sure you have edited the constants in this file to include correct URLs and credentials.");
|
||||
println!("This example show how to sync a remote server with a local cache, using a Provider.");
|
||||
println!("Make sure you have edited the constants in the 'shared.rs' file to include correct URLs and credentials.");
|
||||
println!("You can also set the RUST_LOG environment variable to display more info about the sync.");
|
||||
println!("");
|
||||
println!("This will use the following settings:");
|
||||
|
@ -44,33 +33,7 @@ async fn main() {
|
|||
println!(" * EXAMPLE_CREATED_CALENDAR_URL = {}", EXAMPLE_CREATED_CALENDAR_URL);
|
||||
pause();
|
||||
|
||||
let cache_path = Path::new(CACHE_FOLDER);
|
||||
|
||||
let client = Client::new(URL, USERNAME, PASSWORD).unwrap();
|
||||
let cache = match Cache::from_folder(&cache_path) {
|
||||
Ok(cache) => cache,
|
||||
Err(err) => {
|
||||
log::warn!("Invalid cache file: {}. Using a default cache", err);
|
||||
Cache::new(&cache_path)
|
||||
}
|
||||
};
|
||||
let mut provider = CalDavProvider::new(client, cache);
|
||||
|
||||
let cals = provider.local().get_calendars().await.unwrap();
|
||||
println!("---- Local items, before sync -----");
|
||||
kitchen_fridge::utils::print_calendar_list(&cals).await;
|
||||
|
||||
println!("Starting a sync...");
|
||||
println!("Depending on your RUST_LOG value, you may see more or less details about the progress.");
|
||||
// Note that we could use sync_with_feedback() to have better and formatted feedback
|
||||
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.");
|
||||
}
|
||||
provider.local().save_to_folder().unwrap();
|
||||
|
||||
println!("---- Local items, after sync -----");
|
||||
let cals = provider.local().get_calendars().await.unwrap();
|
||||
kitchen_fridge::utils::print_calendar_list(&cals).await;
|
||||
let mut provider = initial_sync().await;
|
||||
|
||||
add_items_and_sync_again(&mut provider).await;
|
||||
}
|
||||
|
|
56
examples/shared.rs
Normal file
56
examples/shared.rs
Normal file
|
@ -0,0 +1,56 @@
|
|||
use std::path::Path;
|
||||
|
||||
use kitchen_fridge::client::Client;
|
||||
use kitchen_fridge::traits::CalDavSource;
|
||||
use kitchen_fridge::CalDavProvider;
|
||||
use kitchen_fridge::cache::Cache;
|
||||
|
||||
|
||||
// TODO: change these values with yours
|
||||
pub const URL: &str = "https://my.server.com/remote.php/dav/files/john";
|
||||
pub const USERNAME: &str = "username";
|
||||
pub const PASSWORD: &str = "secret_password";
|
||||
|
||||
pub const EXAMPLE_EXISTING_CALENDAR_URL: &str = "https://my.server.com/remote.php/dav/calendars/john/a_calendar_name/";
|
||||
pub const EXAMPLE_CREATED_CALENDAR_URL: &str = "https://my.server.com/remote.php/dav/calendars/john/a_calendar_that_we_have_created/";
|
||||
|
||||
const CACHE_FOLDER: &str = "test_cache/provider_sync";
|
||||
|
||||
fn main() {
|
||||
panic!("This file is not supposed to be executed");
|
||||
}
|
||||
|
||||
|
||||
/// Initializes a Provider, and run an initial sync from the server
|
||||
pub async fn initial_sync() -> CalDavProvider {
|
||||
let cache_path = Path::new(CACHE_FOLDER);
|
||||
|
||||
let client = Client::new(URL, USERNAME, PASSWORD).unwrap();
|
||||
let cache = match Cache::from_folder(&cache_path) {
|
||||
Ok(cache) => cache,
|
||||
Err(err) => {
|
||||
log::warn!("Invalid cache file: {}. Using a default cache", err);
|
||||
Cache::new(&cache_path)
|
||||
}
|
||||
};
|
||||
let mut provider = CalDavProvider::new(client, cache);
|
||||
|
||||
|
||||
let cals = provider.local().get_calendars().await.unwrap();
|
||||
println!("---- Local items, before sync -----");
|
||||
kitchen_fridge::utils::print_calendar_list(&cals).await;
|
||||
|
||||
println!("Starting a sync...");
|
||||
println!("Depending on your RUST_LOG value, you may see more or less details about the progress.");
|
||||
// Note that we could use sync_with_feedback() to have better and formatted feedback
|
||||
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.");
|
||||
}
|
||||
provider.local().save_to_folder().unwrap();
|
||||
|
||||
println!("---- Local items, after sync -----");
|
||||
let cals = provider.local().get_calendars().await.unwrap();
|
||||
kitchen_fridge::utils::print_calendar_list(&cals).await;
|
||||
|
||||
provider
|
||||
}
|
64
examples/toggle-completions.rs
Normal file
64
examples/toggle-completions.rs
Normal file
|
@ -0,0 +1,64 @@
|
|||
//! This is an example of how kitchen-fridge can be used.
|
||||
//! This binary simply toggles all completion statuses of the tasks it finds.
|
||||
|
||||
use std::error::Error;
|
||||
|
||||
use chrono::Utc;
|
||||
|
||||
use kitchen_fridge::item::Item;
|
||||
use kitchen_fridge::task::CompletionStatus;
|
||||
use kitchen_fridge::CalDavProvider;
|
||||
use kitchen_fridge::utils::pause;
|
||||
|
||||
mod shared;
|
||||
use shared::initial_sync;
|
||||
use shared::{URL, USERNAME};
|
||||
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
env_logger::init();
|
||||
|
||||
println!("This example show how to sync a remote server with a local cache, using a Provider.");
|
||||
println!("Make sure you have edited the constants in the 'shared.rs' file to include correct URLs and credentials.");
|
||||
println!("You can also set the RUST_LOG environment variable to display more info about the sync.");
|
||||
println!("");
|
||||
println!("This will use the following settings:");
|
||||
println!(" * URL = {}", URL);
|
||||
println!(" * USERNAME = {}", USERNAME);
|
||||
pause();
|
||||
|
||||
let mut provider = initial_sync().await;
|
||||
|
||||
toggle_all_tasks_and_sync_again(&mut provider).await.unwrap();
|
||||
}
|
||||
|
||||
async fn toggle_all_tasks_and_sync_again(provider: &mut CalDavProvider) -> Result<(), Box<dyn Error>> {
|
||||
let mut n_toggled = 0;
|
||||
|
||||
for (_url, cal) in provider.local().get_calendars_sync()?.iter() {
|
||||
for (_url, item) in cal.lock().unwrap().get_items_mut_sync()?.iter_mut() {
|
||||
match item {
|
||||
Item::Task(task) => {
|
||||
match task.completed() {
|
||||
false => task.set_completion_status(CompletionStatus::Completed(Some(Utc::now()))),
|
||||
true => task.set_completion_status(CompletionStatus::Uncompleted),
|
||||
};
|
||||
n_toggled += 1;
|
||||
}
|
||||
Item::Event(_) => {
|
||||
// Not doing anything with calendar events
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("{} items toggled.", n_toggled);
|
||||
println!("Syncing...");
|
||||
|
||||
provider.sync().await;
|
||||
|
||||
println!("Syncing complete.");
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Add table
Reference in a new issue