From 499f25b9b965d434c17e122eba3a563f20981388 Mon Sep 17 00:00:00 2001 From: daladim Date: Sun, 21 Mar 2021 23:54:33 +0100 Subject: [PATCH] ItemId is the Url --- src/item.rs | 31 ++++++++++++++++++++++++++++--- src/lib.rs | 1 + src/task.rs | 4 ++-- tests/caldav_client.rs | 5 +++++ tests/sync.rs | 31 ++++++++++++++++--------------- 5 files changed, 52 insertions(+), 20 deletions(-) diff --git a/src/item.rs b/src/item.rs index b99281a..86bb923 100644 --- a/src/item.rs +++ b/src/item.rs @@ -1,7 +1,11 @@ use std::fmt::{Display, Formatter}; +use std::str::FromStr; use serde::{Deserialize, Serialize}; use chrono::{Utc, DateTime}; +use url::Url; + +use crate::resource::Resource; #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub enum Item { @@ -68,16 +72,37 @@ impl Item { } } + #[derive(Clone, Debug, PartialEq, Hash, Serialize, Deserialize)] pub struct ItemId { - content: String, + content: Url, } impl ItemId{ - pub fn new() -> Self { - let u = uuid::Uuid::new_v4().to_hyphenated().to_string(); + /// Generate a random ItemId. This is only useful in tests + pub fn random() -> Self { + let random = uuid::Uuid::new_v4().to_hyphenated().to_string(); + let s = format!("https://server.com/{}", random); + let u = s.parse().unwrap(); Self { content:u } } } +impl From for ItemId { + fn from(url: Url) -> Self { + Self { content: url } + } +} +impl From<&Resource> for ItemId { + fn from(resource: &Resource) -> Self { + Self { content: resource.url().clone() } + } +} +impl FromStr for ItemId { + type Err = url::ParseError; + fn from_str(s: &str) -> Result { + s.parse() + } +} + impl Eq for ItemId {} impl Display for ItemId { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { diff --git a/src/lib.rs b/src/lib.rs index a328e95..4019ea3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,7 @@ pub mod calendar; pub use calendar::cached_calendar::CachedCalendar; mod item; pub use item::Item; +pub use item::ItemId; mod task; pub use task::Task; mod event; diff --git a/src/task.rs b/src/task.rs index 3e66517..d56a7ff 100644 --- a/src/task.rs +++ b/src/task.rs @@ -20,9 +20,9 @@ pub struct Task { impl Task { /// Create a new Task - pub fn new(name: String, last_modified: DateTime) -> Self { + pub fn new(name: String, id: ItemId, last_modified: DateTime) -> Self { Self { - id: ItemId::new(), + id, name, last_modified, completed: false, diff --git a/tests/caldav_client.rs b/tests/caldav_client.rs index b03b600..ec11660 100644 --- a/tests/caldav_client.rs +++ b/tests/caldav_client.rs @@ -47,6 +47,11 @@ async fn show_calendars() { for (id, calendar) in calendars.iter() { let cal = calendar.lock().unwrap(); println!(" {}\t{}", cal.name(), id.as_str()); + println!(" IDs:"); + for id in cal.get_item_ids().await.unwrap() { + println!(" * {}", id); + } + println!(" Most recent changes:"); for (_id, task) in cal.get_items_modified_since(None, None).await.unwrap() { my_tasks::utils::print_task(task); diff --git a/tests/sync.rs b/tests/sync.rs index 1c44fe7..943d608 100644 --- a/tests/sync.rs +++ b/tests/sync.rs @@ -8,6 +8,7 @@ use my_tasks::traits::{CalDavSource, SyncSlave}; use my_tasks::traits::PartialCalendar; use my_tasks::cache::Cache; use my_tasks::Item; +use my_tasks::ItemId; use my_tasks::Task; use my_tasks::calendar::cached_calendar::CachedCalendar; use my_tasks::Provider; @@ -52,19 +53,19 @@ async fn populate_test_provider() -> Provider Provider Provider