Cleaner comparisons

This commit is contained in:
daladim 2021-04-03 17:42:55 +02:00
parent 751a98d281
commit f760176435
5 changed files with 46 additions and 11 deletions

View file

@ -118,7 +118,7 @@ impl Cache {
/// Compares two Caches to check they have the same current content /// 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<bool, Box<dyn Error>> { pub async fn has_same_contents_than(&self, other: &Self) -> Result<bool, Box<dyn Error>> {
let calendars_l = self.get_calendars().await?; let calendars_l = self.get_calendars().await?;
let calendars_r = other.get_calendars().await?; let calendars_r = other.get_calendars().await?;
@ -128,9 +128,10 @@ impl Cache {
return Ok(false); 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_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(), Some(c) => c.lock().unwrap(),
None => return Err("should not happen, we've just tested keys are the same".into()), None => return Err("should not happen, we've just tested keys are the same".into()),
}; };
@ -147,8 +148,10 @@ impl Cache {
Some(c) => c, Some(c) => c,
None => return Err("should not happen, we've just tested keys are the same".into()), None => return Err("should not happen, we've just tested keys are the same".into()),
}; };
if &item_l != item_r { if item_l.has_same_observable_content(&item_r) == false {
log::debug!("Different items"); log::debug!("Different items for id {}:", id_l);
log::debug!("{:#?}", item_l);
log::debug!("{:#?}", item_r);
return Ok(false); return Ok(false);
} }
} }
@ -159,15 +162,27 @@ impl Cache {
fn keys_are_the_same<T, U, V>(left: &HashMap<T, U>, right: &HashMap<T, V>) -> bool fn keys_are_the_same<T, U, V>(left: &HashMap<T, U>, right: &HashMap<T, V>) -> bool
where where
T: Hash + Eq + Clone, T: Hash + Eq + Clone + std::fmt::Display,
{ {
if left.len() != right.len() { if left.len() != right.len() {
log::debug!("Count of keys mismatch: {} and {}", left.len(), right.len());
return false; return false;
} }
let keys_l: HashSet<T> = left.keys().cloned().collect(); let keys_l: HashSet<T> = left.keys().cloned().collect();
let keys_r: HashSet<T> = right.keys().cloned().collect(); let keys_r: HashSet<T> = 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] #[async_trait]

View file

@ -12,7 +12,7 @@ use crate::item::ItemId;
/// A calendar used by the [`cache`](crate::cache) module /// A calendar used by the [`cache`](crate::cache) module
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CachedCalendar { pub struct CachedCalendar {
name: String, name: String,
id: CalendarId, id: CalendarId,

View file

@ -4,7 +4,6 @@ use serde::{Deserialize, Serialize};
use crate::item::ItemId; use crate::item::ItemId;
use crate::item::SyncStatus; use crate::item::SyncStatus;
use crate::item::VersionTag;
/// TODO: implement Event one day. /// TODO: implement Event one day.
/// This crate currently only supports tasks, not calendar events. /// 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) { pub fn set_sync_status(&mut self, new_status: SyncStatus) {
self.sync_status = new_status; self.sync_status = new_status;
} }
pub fn has_same_observable_content(&self, _other: &Event) -> bool {
unimplemented!();
}
} }

View file

@ -10,7 +10,7 @@ use crate::resource::Resource;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Item { pub enum Item {
Event(crate::event::Event), Event(crate::event::Event),
Task(crate::task::Task), Task(crate::task::Task),
@ -79,6 +79,14 @@ impl Item {
_ => panic!("Not a task"), _ => 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,
}
}
} }

View file

@ -4,7 +4,7 @@ use crate::item::ItemId;
use crate::item::SyncStatus; use crate::item::SyncStatus;
/// A to-do task /// A to-do task
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Task { pub struct Task {
/// The task unique ID, that will never change /// The task unique ID, that will never change
id: ItemId, id: ItemId,
@ -33,6 +33,15 @@ impl Task {
pub fn name(&self) -> &str { &self.name } pub fn name(&self) -> &str { &self.name }
pub fn completed(&self) -> bool { self.completed } pub fn completed(&self) -> bool { self.completed }
pub fn sync_status(&self) -> &SyncStatus { &self.sync_status } 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) { pub fn set_sync_status(&mut self, new_status: SyncStatus) {
self.sync_status = new_status; self.sync_status = new_status;
} }