diff --git a/src/snapshot/patch_apply.rs b/src/snapshot/patch_apply.rs index ed97940c..3dbd1f62 100644 --- a/src/snapshot/patch_apply.rs +++ b/src/snapshot/patch_apply.rs @@ -44,7 +44,7 @@ struct PatchApplyContext { /// then apply properties all at once at the end. fn apply_deferred_properties(context: PatchApplyContext, tree: &mut RojoTree) { for (id, mut properties) in context.properties_to_apply { - let instance = tree + let mut instance = tree .get_instance_mut(id) .expect("Invalid instance ID in deferred property map"); @@ -58,7 +58,7 @@ fn apply_deferred_properties(context: PatchApplyContext, tree: &mut RojoTree) { } } - instance.instance.properties = properties; + *instance.properties_mut() = properties; } } @@ -100,16 +100,16 @@ fn apply_update_child( tree: &mut RojoTree, patch: &PatchUpdateInstance, ) { - let instance = tree + let mut instance = tree .get_instance_mut(patch.id) .expect("Instance referred to by patch does not exist"); if let Some(name) = &patch.changed_name { - instance.instance.name = name.clone(); + *instance.name_mut() = name.clone(); } if let Some(class_name) = &patch.changed_class_name { - instance.instance.class_name = class_name.clone(); + *instance.class_name_mut() = class_name.clone(); } for (key, property_entry) in &patch.changed_properties { @@ -120,7 +120,7 @@ fn apply_update_child( Some(RbxValue::Ref { value: Some(id) }) => { let new_id = context.snapshot_id_to_instance_id.get(id).unwrap_or(id); - instance.instance.properties.insert( + instance.properties_mut().insert( key.clone(), RbxValue::Ref { value: Some(*new_id), @@ -134,7 +134,7 @@ fn apply_update_child( .insert(key.clone(), value.clone()); } None => { - instance.instance.properties.remove(key); + instance.properties_mut().remove(key); } } } @@ -188,16 +188,13 @@ mod test { apply_patch_set(&mut tree, &patch_set); let root_instance = tree.get_instance(root_id).unwrap(); - let child_id = root_instance.instance.get_children_ids()[0]; + let child_id = root_instance.children()[0]; let child_instance = tree.get_instance(child_id).unwrap(); - assert_eq!(child_instance.instance.name.as_str(), &snapshot.name); - assert_eq!( - child_instance.instance.class_name.as_str(), - &snapshot.class_name - ); - assert_eq!(&child_instance.instance.properties, &snapshot.properties); - assert!(child_instance.instance.get_children_ids().is_empty()); + assert_eq!(child_instance.name(), &snapshot.name); + assert_eq!(child_instance.class_name(), &snapshot.class_name); + assert_eq!(child_instance.properties(), &snapshot.properties); + assert!(child_instance.children().is_empty()); } #[test] @@ -250,8 +247,8 @@ mod test { }; let root_instance = tree.get_instance(root_id).unwrap(); - assert_eq!(root_instance.instance.name, "Foo"); - assert_eq!(root_instance.instance.class_name, "NewClassName"); - assert_eq!(root_instance.instance.properties, expected_properties); + assert_eq!(root_instance.name(), "Foo"); + assert_eq!(root_instance.class_name(), "NewClassName"); + assert_eq!(root_instance.properties(), &expected_properties); } } diff --git a/src/snapshot/patch_compute.rs b/src/snapshot/patch_compute.rs index 2fa9bb61..bd87dc7d 100644 --- a/src/snapshot/patch_compute.rs +++ b/src/snapshot/patch_compute.rs @@ -155,7 +155,7 @@ fn compute_children_patches<'a>( .get_instance(id) .expect("Instance did not exist in tree"); - let instance_children = instance.instance.get_children_ids(); + let instance_children = instance.children(); let mut paired_instances = vec![false; instance_children.len()]; @@ -173,8 +173,8 @@ fn compute_children_patches<'a>( .get_instance(**instance_child_id) .expect("Instance did not exist in tree"); - if snapshot_child.name == instance_child.instance.name - && snapshot_child.class_name == instance_child.instance.class_name + if snapshot_child.name == instance_child.name() + && snapshot_child.class_name == instance_child.class_name() { paired_instances[*instance_index] = true; return true; diff --git a/src/snapshot/tree.rs b/src/snapshot/tree.rs index e91afec1..00fb8ba4 100644 --- a/src/snapshot/tree.rs +++ b/src/snapshot/tree.rs @@ -1,6 +1,6 @@ use std::{collections::HashMap, path::PathBuf}; -use rbx_dom_weak::{RbxId, RbxInstance, RbxInstanceProperties, RbxTree}; +use rbx_dom_weak::{RbxId, RbxInstance, RbxInstanceProperties, RbxTree, RbxValue}; use crate::multimap::MultiMap; @@ -147,8 +147,56 @@ pub struct InstanceWithMeta<'a> { pub metadata: &'a InstanceMetadata, } +impl InstanceWithMeta<'_> { + pub fn name(&self) -> &str { + &self.instance.name + } + + pub fn class_name(&self) -> &str { + &self.instance.class_name + } + + pub fn properties(&self) -> &HashMap { + &self.instance.properties + } + + pub fn children(&self) -> &[RbxId] { + self.instance.get_children_ids() + } +} + #[derive(Debug)] pub struct InstanceWithMetaMut<'a> { pub instance: &'a mut RbxInstance, pub metadata: &'a mut InstanceMetadata, } + +impl InstanceWithMetaMut<'_> { + pub fn name(&self) -> &str { + &self.instance.name + } + + pub fn name_mut(&mut self) -> &mut String { + &mut self.instance.name + } + + pub fn class_name(&self) -> &str { + &self.instance.class_name + } + + pub fn class_name_mut(&mut self) -> &mut String { + &mut self.instance.class_name + } + + pub fn properties(&self) -> &HashMap { + &self.instance.properties + } + + pub fn properties_mut(&mut self) -> &mut HashMap { + &mut self.instance.properties + } + + pub fn children(&self) -> &[RbxId] { + self.instance.get_children_ids() + } +}