diff --git a/src/store/mod.rs b/src/store/mod.rs index a903c5c..1f19eba 100644 --- a/src/store/mod.rs +++ b/src/store/mod.rs @@ -147,25 +147,21 @@ impl ThingsStore { let task = self.parse_task(uuid, props, entity, obj.degraded); self.tasks_by_uuid.insert(uuid.clone(), task); } - Some(EntityType::Area3) => { + Some(entity) if entity.is_area_family() => { let StateProperties::Area(props) = &obj.properties else { continue; }; let area = self.parse_area(uuid, props); self.areas_by_uuid.insert(uuid.clone(), area); } - Some(EntityType::Tag3 | EntityType::Tag4) => { + Some(entity) if entity.is_tag_family() => { let StateProperties::Tag(props) = &obj.properties else { continue; }; let tag = self.parse_tag(uuid, props); self.tags_by_uuid.insert(uuid.clone(), tag); } - Some( - EntityType::ChecklistItem - | EntityType::ChecklistItem2 - | EntityType::ChecklistItem3, - ) => { + Some(entity) if entity.is_checklist_family() => { if let StateProperties::ChecklistItem(props) = &obj.properties { checklist_items.push(self.parse_checklist_item(uuid, props)); } diff --git a/src/store/state.rs b/src/store/state.rs index 6db16b2..2c33a8a 100644 --- a/src/store/state.rs +++ b/src/store/state.rs @@ -213,13 +213,17 @@ fn wire_object_properties(obj: &WireObject) -> StateProperties { } } -/// an object of a stored kind whose payload did not parse keeps the fields that do and is marked +/// an object of a stored kind, or another version of one, whose payload did not parse keeps the fields that do and is marked /// -/// an object an update reaches before any create, a task whose note cannot be read, a task of a kind this CLI does not read and a tombstone whose payload does not parse are marked too +/// an object an update reaches before any create, a task whose note cannot be read, an object of a version this CLI does not read and a tombstone it cannot read are marked too fn insert_state_object(state: &mut RawState, uuid: &ThingsId, obj: WireObject) { let stored = obj.entity_type.as_ref().is_some_and(EntityType::is_stored); + let other_version = obj + .entity_type + .as_ref() + .is_some_and(EntityType::is_other_stored_version); let unparsed = stored && matches!(obj.payload, Properties::Unknown(_)); - let properties = if unparsed { + let properties = if (stored || other_version) && matches!(obj.payload, Properties::Unknown(_)) { obj.readable_properties() .map_or(StateProperties::Other, Into::into) } else { @@ -231,25 +235,35 @@ fn insert_state_object(state: &mut RawState, uuid: &ThingsId, obj: WireObject) { Properties::TaskCreate(props) if props.notes.as_ref().is_some_and(|notes| notes.apply_to(None).is_err()) ); - let future = obj - .entity_type - .as_ref() - .is_some_and(|entity| entity.is_task_family() && !stored); // a tombstone whose payload does not parse names nothing to purge // its own mark reports it as not replayed let unread_tombstone = matches!(obj.entity_type, Some(EntityType::Tombstone2)) && matches!(obj.payload, Properties::Unknown(_)); - let degraded = unparsed || create_less || unreadable_note || future || unread_tombstone; + // a tombstone of another version is not read + // what it names stays + // its own mark reports it + let other_tombstone = obj + .entity_type + .as_ref() + .is_some_and(EntityType::is_other_tombstone_version); + let degraded = unparsed + || create_less + || unreadable_note + || other_version + || unread_tombstone + || other_tombstone; if unparsed { warn!(target: "things::replay", uuid = %uuid, "the object's payload did not parse, the fields that do are kept"); } else if unreadable_note { warn!(target: "things::replay", uuid = %uuid, "the task's note cannot be read"); } else if create_less { warn!(target: "things::replay", uuid = %uuid, "an update reached the object before any create, it is kept partial"); - } else if future { - warn!(target: "things::replay", uuid = %uuid, "a task of a kind this CLI does not read, it is kept opaque"); + } else if other_version { + warn!(target: "things::replay", uuid = %uuid, "an object of a version this CLI does not read, it is kept as far as it parses"); } else if unread_tombstone { warn!(target: "things::replay", uuid = %uuid, "the tombstone's payload did not parse, nothing is purged"); + } else if other_tombstone { + warn!(target: "things::replay", uuid = %uuid, "a tombstone of a version this CLI does not read, nothing is purged"); } state.insert( uuid.clone(), @@ -303,6 +317,12 @@ fn apply_update_payload( Some("the payload is of another kind than the object".to_string()) } }; + // an update of another version may carry what the known schema drops + let failure = failure.or_else(|| { + entity_type + .is_some_and(EntityType::is_other_stored_version) + .then(|| "an update of a version this CLI does not read".to_string()) + }); if let Some(failure) = failure { warn!(target: "things::replay", uuid = %uuid, "{failure}"); existing.degraded = true; @@ -587,6 +607,23 @@ mod tests { ); } + #[test] + fn a_tombstone_of_another_version_is_marked_and_purges_nothing() { + for kind in ["Tombstone", "Tombstone3"] { + let items = [ + r#"{"Ta11111111111111111111":{"t":0,"e":"Task7","p":{"tt":"Order tiles","st":1,"tr":true}}}"#.to_string(), + format!(r#"{{"Tm11111111111111111111":{{"t":0,"e":"{kind}","p":{{"dloid":"Ta11111111111111111111"}}}}}}"#), + ]; + let state = fold_items(items.map(|item| wire_item(&item))); + let tombstone = "Tm11111111111111111111".parse::().expect("id"); + assert!( + state.contains_key(&"Ta11111111111111111111".parse::().expect("id")), + "{kind} leaves the to-do" + ); + assert_eq!(degraded_ids(&state), vec![tombstone], "{kind}"); + } + } + #[test] fn a_tombstone_purges_what_it_names_whatever_its_deletion_time_holds() { let items = [ @@ -619,6 +656,57 @@ mod tests { assert_eq!(task.title, "Plant bulbs"); } + #[test] + fn another_version_of_a_stored_kind_is_read_as_far_as_it_parses_and_marked() { + let items = [ + r#"{"Ta11111111111111111111":{"t":0,"e":"Task7","p":{"tt":"Travel","st":1}}}"#, + r#"{"Cm11111111111111111111":{"t":0,"e":"ChecklistItem4","p":{"tt":"Take pills","ts":["Ta11111111111111111111"],"ss":0,"ix":1}}}"#, + r#"{"Tg11111111111111111111":{"t":0,"e":"Tag5","p":{"tt":"Errand"}}}"#, + r#"{"Ar11111111111111111111":{"t":0,"e":"Area3","p":{"tt":"Home"}}}"#, + r#"{"Ar11111111111111111111":{"t":1,"e":"Area4","p":{"tt":"House"}}}"#, + // fields that fail the known schema leave the rest readable + r#"{"Tb11111111111111111111":{"t":0,"e":"Task7","p":{"tt":"Pack","st":1}}}"#, + r#"{"Cn11111111111111111111":{"t":0,"e":"ChecklistItem4","p":{"tt":"Charger","ts":["Tb11111111111111111111"],"ss":"open","ix":2}}}"#, + r#"{"Th11111111111111111111":{"t":0,"e":"Tag5","p":{"tt":"Garden","ix":"first"}}}"#, + // an older version counts as another one too + r#"{"Tc11111111111111111111":{"t":0,"e":"Task3","p":{"tt":"Post letters","st":1}}}"#, + r#"{"As11111111111111111111":{"t":0,"e":"Area2","p":{"tt":"Work"}}}"#, + ]; + let state = fold_items(items.map(wire_item)); + let store = ThingsStore::from_raw_state(&state); + // the owner of a checklist item of another version takes no writes + for owner in ["Ta11111111111111111111", "Tb11111111111111111111"] { + assert!(store.get_task(owner).expect("to-do").degraded, "{owner}"); + } + let mut titles = store + .tags() + .into_iter() + .map(|tag| tag.title) + .collect::>(); + titles.sort(); + assert_eq!(titles, ["Errand", "Garden"]); + assert_eq!( + store + .get_area("Ar11111111111111111111") + .expect("area") + .title, + "House" + ); + let ids = [ + "Ar11111111111111111111", + "As11111111111111111111", + "Cm11111111111111111111", + "Cn11111111111111111111", + "Ta11111111111111111111", + "Tb11111111111111111111", + "Tc11111111111111111111", + "Tg11111111111111111111", + "Th11111111111111111111", + ] + .map(|id| id.parse::().expect("id")); + assert_eq!(degraded_ids(&state), ids); + } + #[test] fn the_trash_lists_a_to_do_whose_trashed_container_is_no_project() { // an update before any create leaves the container a partial to-do diff --git a/src/wire/wire_object.rs b/src/wire/wire_object.rs index 5f8a1f9..94ce06e 100644 --- a/src/wire/wire_object.rs +++ b/src/wire/wire_object.rs @@ -176,20 +176,20 @@ impl WireObject { let payload = match operation_type { OperationType::Delete => Delete, OperationType::Create => match entity_type { - Some(Task6 | Task7) => TaskCreate(Box::new(parse(p)?)), - Some(ChecklistItem | ChecklistItem2 | ChecklistItem3) => ChecklistCreate(parse(p)?), - Some(Tag3 | Tag4) => TagCreate(parse(p)?), - Some(Area3) => AreaCreate(parse(p)?), + Some(entity) if entity.is_task_family() => TaskCreate(Box::new(parse(p)?)), + Some(entity) if entity.is_checklist_family() => ChecklistCreate(parse(p)?), + Some(entity) if entity.is_tag_family() => TagCreate(parse(p)?), + Some(entity) if entity.is_area_family() => AreaCreate(parse(p)?), Some(Tombstone2) => TombstoneCreate(parse(p)?), Some(Command) => CommandCreate(parse(p)?), Some(Settings3 | Settings4 | Settings5) => Ignored(p), _ => Properties::Unknown(p), }, OperationType::Update => match entity_type { - Some(Task6 | Task7) => TaskUpdate(Box::new(parse(p)?)), - Some(ChecklistItem | ChecklistItem2 | ChecklistItem3) => ChecklistUpdate(parse(p)?), - Some(Tag3 | Tag4) => TagUpdate(parse(p)?), - Some(Area3) => AreaUpdate(parse(p)?), + Some(entity) if entity.is_task_family() => TaskUpdate(Box::new(parse(p)?)), + Some(entity) if entity.is_checklist_family() => ChecklistUpdate(parse(p)?), + Some(entity) if entity.is_tag_family() => TagUpdate(parse(p)?), + Some(entity) if entity.is_area_family() => AreaUpdate(parse(p)?), Some(Settings3 | Settings4 | Settings5) => Ignored(p), _ => Properties::Unknown(p), }, @@ -316,7 +316,8 @@ pub enum OperationType { /// /// versioned by Things, `Task6` or `Area3` for instance /// -/// the kinds of histories from before base58 ids, `Task3`, `Task4`, `Area2` and the first `Tombstone`, are not read and parse as unknown +/// a version this CLI does not know parses as unknown +/// the `Task3`, `Task4`, `Area2` and first `Tombstone` kinds of histories from before base58 ids are among them #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Display, EnumString)] #[serde(from = "String", into = "String")] pub enum EntityType { @@ -381,16 +382,50 @@ impl EntityType { matches!(self, Self::Task6 | Self::Task7) } + /// a task of any version, another one read with the latest known schema pub fn is_task_family(&self) -> bool { - if self.is_task() { - return true; - } + self.is_task() || self.is_version_of("Task") + } + + /// a checklist item of any version, another one read with the latest known schema + pub fn is_checklist_family(&self) -> bool { + matches!( + self, + Self::ChecklistItem | Self::ChecklistItem2 | Self::ChecklistItem3 + ) || self.is_version_of("ChecklistItem") + } + + /// a tag of any version, another one read with the latest known schema + pub fn is_tag_family(&self) -> bool { + matches!(self, Self::Tag3 | Self::Tag4) || self.is_version_of("Tag") + } + /// an area of any version, another one read with the latest known schema + pub fn is_area_family(&self) -> bool { + matches!(self, Self::Area3) || self.is_version_of("Area") + } + + /// a version this CLI does not know of a kind it stores + pub fn is_other_stored_version(&self) -> bool { + !self.is_stored() + && (self.is_task_family() + || self.is_checklist_family() + || self.is_tag_family() + || self.is_area_family()) + } + + /// a tombstone of a version this CLI does not read, the first `Tombstone` or one after `Tombstone2` + pub fn is_other_tombstone_version(&self) -> bool { + matches!(self, Self::Unknown(name) if name == "Tombstone") + || self.is_version_of("Tombstone") + } + + /// an unknown kind named `prefix` with a version number + fn is_version_of(&self, prefix: &str) -> bool { let Self::Unknown(name) = self else { return false; }; - - name.strip_prefix("Task").is_some_and(|version| { + name.strip_prefix(prefix).is_some_and(|version| { !version.is_empty() && version.chars().all(|c| c.is_ascii_digit()) }) } diff --git a/tests/cli/delete/newer_task_kind.in/journal.json b/tests/cli/delete/newer_task_kind.in/journal.json new file mode 100644 index 0000000..0ed2298 --- /dev/null +++ b/tests/cli/delete/newer_task_kind.in/journal.json @@ -0,0 +1,5 @@ +[ + {"Pa11111111111111111111": {"t":0,"e":"Task7","p":{"tt":"Garden","tp":1,"ss":0,"st":1,"ix":1,"cd":1,"md":1}}}, + {"Tb11111111111111111111": {"t":0,"e":"Task7","p":{"tt":"Rake leaves","tp":0,"ss":0,"st":1,"ix":1,"cd":1,"md":1,"pr":["Pa11111111111111111111"]}}}, + {"Ta11111111111111111111": {"t":0,"e":"Task8","p":{"tt":"Plant bulbs","tp":0,"ss":0,"st":1,"ix":2,"cd":1,"md":1,"pr":["Pa11111111111111111111"]}}} +] diff --git a/tests/cli/delete/newer_task_kind.trycmd b/tests/cli/delete/newer_task_kind.trycmd new file mode 100644 index 0000000..a6660a8 --- /dev/null +++ b/tests/cli/delete/newer_task_kind.trycmd @@ -0,0 +1,17 @@ +```trycmd +$ run.sh things project Pa +◯ Garden (0/2) + + Tb ▢ Rake leaves + Ta ▢ Plant bulbs +1 object did not replay completely and will not be written, THINGS_LOG=warn lists the ids + +``` + +```trycmd +$ run.sh things --now-ts 1774400000.0 delete Pa +? 1 +1 object did not replay completely and will not be written, THINGS_LOG=warn lists the ids +Failed to delete items: Not writing Ta11111111111111111111: its history did not replay completely. + +```