Cleaner comparisons
This commit is contained in:
parent
751a98d281
commit
f760176435
5 changed files with 46 additions and 11 deletions
29
src/cache.rs
29
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<bool, Box<dyn Error>> {
|
||||
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<T, U, V>(left: &HashMap<T, U>, right: &HashMap<T, V>) -> 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<T> = left.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]
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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!();
|
||||
}
|
||||
}
|
||||
|
|
10
src/item.rs
10
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
11
src/task.rs
11
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue