From a2f227e73ba8bc64a08c94f5fac93c75da6ef740 Mon Sep 17 00:00:00 2001 From: daladim Date: Tue, 13 Apr 2021 23:19:41 +0200 Subject: [PATCH] Correctly serializing cache --- src/cache.rs | 24 ++++++++++++++++++++++-- src/item.rs | 25 +++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index 4462aa4..15e9737 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -210,6 +210,8 @@ mod tests { use url::Url; use crate::calendar::SupportedComponents; + use crate::item::Item; + use crate::task::Task; #[tokio::test] async fn serde_cache() { @@ -219,12 +221,30 @@ mod tests { let mut cache = Cache::new(&cache_path); - let _ = cache.create_calendar( + let shopping_list = cache.create_calendar( Url::parse("https://caldav.com/shopping").unwrap(), - "shopping list".to_string(), + "My shopping list".to_string(), SupportedComponents::TODO, ).await.unwrap(); + let bucket_list = cache.create_calendar( + Url::parse("https://caldav.com/bucket-list").unwrap(), + "My bucket list".to_string(), + SupportedComponents::TODO, + ).await.unwrap(); + + { + let mut bucket_list = bucket_list.lock().unwrap(); + let cal_id = bucket_list.id().clone(); + bucket_list.add_item(Item::Task(Task::new( + String::from("Attend a concert of JS Bach"), false, &cal_id + ))).await.unwrap(); + + bucket_list.add_item(Item::Task(Task::new( + String::from("Climb the Lighthouse of Alexandria"), true, &cal_id + ))).await.unwrap(); + } + cache.save_to_folder().unwrap(); diff --git a/src/item.rs b/src/item.rs index 1f0c9f2..76d59c8 100644 --- a/src/item.rs +++ b/src/item.rs @@ -3,7 +3,7 @@ use std::fmt::{Display, Formatter}; use std::str::FromStr; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; use url::Url; use crate::resource::Resource; @@ -98,7 +98,7 @@ impl Item { } -#[derive(Clone, Debug, PartialEq, Hash, Serialize, Deserialize)] +#[derive(Clone, Debug, PartialEq, Hash)] pub struct ItemId { content: Url, } @@ -139,6 +139,27 @@ impl Display for ItemId { } } +/// Used to support serde +impl Serialize for ItemId { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(self.content.as_str()) + } +} +/// Used to support serde +impl<'de> Deserialize<'de> for ItemId { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let u = Url::deserialize(deserializer)?; + Ok(ItemId{ content: u }) + } +} + + /// A VersionTag is basically a CalDAV `ctag` or `etag`. Whenever it changes, this means the data has changed. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]