From f7601764357e1217c6c869020b5d446e9929acbb Mon Sep 17 00:00:00 2001 From: daladim Date: Sat, 3 Apr 2021 17:42:55 +0200 Subject: [PATCH] Cleaner comparisons --- src/cache.rs | 29 ++++++++++++++++++++++------- src/calendar/cached_calendar.rs | 2 +- src/event.rs | 5 ++++- src/item.rs | 10 +++++++++- src/task.rs | 11 ++++++++++- 5 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index dea0d55..94bf697 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -118,7 +118,7 @@ impl Cache { /// Compares two Caches to check they have the same current content /// - /// This is not a complete equality test: some attributes (last sync date, deleted items...) may differ + /// This is not a complete equality test: some attributes (sync status...) may differ pub async fn has_same_contents_than(&self, other: &Self) -> Result> { let calendars_l = self.get_calendars().await?; let calendars_r = other.get_calendars().await?; @@ -128,9 +128,10 @@ impl Cache { return Ok(false); } - for (id, cal_l) in calendars_l { + for (calendar_id, cal_l) in calendars_l { + log::debug!("Comparing calendars {}", calendar_id); let cal_l = cal_l.lock().unwrap(); - let cal_r = match calendars_r.get(&id) { + let cal_r = match calendars_r.get(&calendar_id) { Some(c) => c.lock().unwrap(), None => return Err("should not happen, we've just tested keys are the same".into()), }; @@ -147,8 +148,10 @@ impl Cache { Some(c) => c, None => return Err("should not happen, we've just tested keys are the same".into()), }; - if &item_l != item_r { - log::debug!("Different items"); + if item_l.has_same_observable_content(&item_r) == false { + log::debug!("Different items for id {}:", id_l); + log::debug!("{:#?}", item_l); + log::debug!("{:#?}", item_r); return Ok(false); } } @@ -159,15 +162,27 @@ impl Cache { fn keys_are_the_same(left: &HashMap, right: &HashMap) -> bool where - T: Hash + Eq + Clone, + T: Hash + Eq + Clone + std::fmt::Display, { if left.len() != right.len() { + log::debug!("Count of keys mismatch: {} and {}", left.len(), right.len()); return false; } let keys_l: HashSet = left.keys().cloned().collect(); let keys_r: HashSet = right.keys().cloned().collect(); - keys_l == keys_r + let result = keys_l == keys_r; + if result == false { + log::debug!("Keys of a map mismatch"); + for key in keys_l { + log::debug!(" left: {}", key); + } + log::debug!("RIGHT:"); + for key in keys_r { + log::debug!(" right: {}", key); + } + } + result } #[async_trait] diff --git a/src/calendar/cached_calendar.rs b/src/calendar/cached_calendar.rs index 9e09fac..8eb6af9 100644 --- a/src/calendar/cached_calendar.rs +++ b/src/calendar/cached_calendar.rs @@ -12,7 +12,7 @@ use crate::item::ItemId; /// A calendar used by the [`cache`](crate::cache) module -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct CachedCalendar { name: String, id: CalendarId, diff --git a/src/event.rs b/src/event.rs index c3367f9..8dbe77d 100644 --- a/src/event.rs +++ b/src/event.rs @@ -4,7 +4,6 @@ use serde::{Deserialize, Serialize}; use crate::item::ItemId; use crate::item::SyncStatus; -use crate::item::VersionTag; /// TODO: implement Event one day. /// This crate currently only supports tasks, not calendar events. @@ -34,4 +33,8 @@ impl Event { pub fn set_sync_status(&mut self, new_status: SyncStatus) { self.sync_status = new_status; } + + pub fn has_same_observable_content(&self, _other: &Event) -> bool { + unimplemented!(); + } } diff --git a/src/item.rs b/src/item.rs index 011697a..6e44fef 100644 --- a/src/item.rs +++ b/src/item.rs @@ -10,7 +10,7 @@ use crate::resource::Resource; -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub enum Item { Event(crate::event::Event), Task(crate::task::Task), @@ -79,6 +79,14 @@ impl Item { _ => panic!("Not a task"), } } + + pub fn has_same_observable_content(&self, other: &Item) -> bool { + match (self, other) { + (Item::Event(s), Item::Event(o)) => s.has_same_observable_content(o), + (Item::Task(s), Item::Task(o)) => s.has_same_observable_content(o), + _ => false, + } + } } diff --git a/src/task.rs b/src/task.rs index 21a620e..a20649f 100644 --- a/src/task.rs +++ b/src/task.rs @@ -4,7 +4,7 @@ use crate::item::ItemId; use crate::item::SyncStatus; /// A to-do task -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct Task { /// The task unique ID, that will never change id: ItemId, @@ -33,6 +33,15 @@ impl Task { pub fn name(&self) -> &str { &self.name } pub fn completed(&self) -> bool { self.completed } pub fn sync_status(&self) -> &SyncStatus { &self.sync_status } + + pub fn has_same_observable_content(&self, other: &Task) -> bool { + self.id == other.id + && self.name == other.name + && self.completed == other.completed + // sync status must be the same variant, but we ignore its embedded version tag + && std::mem::discriminant(&self.sync_status) == std::mem::discriminant(&other.sync_status) + } + pub fn set_sync_status(&mut self, new_status: SyncStatus) { self.sync_status = new_status; }