Correctly serializing cache

This commit is contained in:
daladim 2021-04-13 23:19:41 +02:00
parent 65da7b34cf
commit a2f227e73b
2 changed files with 45 additions and 4 deletions

View file

@ -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();

View file

@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.content.as_str())
}
}
/// Used to support serde
impl<'de> Deserialize<'de> for ItemId {
fn deserialize<D>(deserializer: D) -> Result<ItemId, D::Error>
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)]