Clean up some of the instance wrapper APIs

This commit is contained in:
Lucien Greathouse
2019-09-09 13:59:36 -07:00
parent 824b984a64
commit 47ee8d54a8
3 changed files with 67 additions and 22 deletions

View File

@@ -44,7 +44,7 @@ struct PatchApplyContext {
/// then apply properties all at once at the end. /// then apply properties all at once at the end.
fn apply_deferred_properties(context: PatchApplyContext, tree: &mut RojoTree) { fn apply_deferred_properties(context: PatchApplyContext, tree: &mut RojoTree) {
for (id, mut properties) in context.properties_to_apply { for (id, mut properties) in context.properties_to_apply {
let instance = tree let mut instance = tree
.get_instance_mut(id) .get_instance_mut(id)
.expect("Invalid instance ID in deferred property map"); .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, tree: &mut RojoTree,
patch: &PatchUpdateInstance, patch: &PatchUpdateInstance,
) { ) {
let instance = tree let mut instance = tree
.get_instance_mut(patch.id) .get_instance_mut(patch.id)
.expect("Instance referred to by patch does not exist"); .expect("Instance referred to by patch does not exist");
if let Some(name) = &patch.changed_name { 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 { 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 { for (key, property_entry) in &patch.changed_properties {
@@ -120,7 +120,7 @@ fn apply_update_child(
Some(RbxValue::Ref { value: Some(id) }) => { Some(RbxValue::Ref { value: Some(id) }) => {
let new_id = context.snapshot_id_to_instance_id.get(id).unwrap_or(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(), key.clone(),
RbxValue::Ref { RbxValue::Ref {
value: Some(*new_id), value: Some(*new_id),
@@ -134,7 +134,7 @@ fn apply_update_child(
.insert(key.clone(), value.clone()); .insert(key.clone(), value.clone());
} }
None => { None => {
instance.instance.properties.remove(key); instance.properties_mut().remove(key);
} }
} }
} }
@@ -188,16 +188,13 @@ mod test {
apply_patch_set(&mut tree, &patch_set); apply_patch_set(&mut tree, &patch_set);
let root_instance = tree.get_instance(root_id).unwrap(); 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(); let child_instance = tree.get_instance(child_id).unwrap();
assert_eq!(child_instance.instance.name.as_str(), &snapshot.name); assert_eq!(child_instance.name(), &snapshot.name);
assert_eq!( assert_eq!(child_instance.class_name(), &snapshot.class_name);
child_instance.instance.class_name.as_str(), assert_eq!(child_instance.properties(), &snapshot.properties);
&snapshot.class_name assert!(child_instance.children().is_empty());
);
assert_eq!(&child_instance.instance.properties, &snapshot.properties);
assert!(child_instance.instance.get_children_ids().is_empty());
} }
#[test] #[test]
@@ -250,8 +247,8 @@ mod test {
}; };
let root_instance = tree.get_instance(root_id).unwrap(); let root_instance = tree.get_instance(root_id).unwrap();
assert_eq!(root_instance.instance.name, "Foo"); assert_eq!(root_instance.name(), "Foo");
assert_eq!(root_instance.instance.class_name, "NewClassName"); assert_eq!(root_instance.class_name(), "NewClassName");
assert_eq!(root_instance.instance.properties, expected_properties); assert_eq!(root_instance.properties(), &expected_properties);
} }
} }

View File

@@ -155,7 +155,7 @@ fn compute_children_patches<'a>(
.get_instance(id) .get_instance(id)
.expect("Instance did not exist in tree"); .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()]; let mut paired_instances = vec![false; instance_children.len()];
@@ -173,8 +173,8 @@ fn compute_children_patches<'a>(
.get_instance(**instance_child_id) .get_instance(**instance_child_id)
.expect("Instance did not exist in tree"); .expect("Instance did not exist in tree");
if snapshot_child.name == instance_child.instance.name if snapshot_child.name == instance_child.name()
&& snapshot_child.class_name == instance_child.instance.class_name && snapshot_child.class_name == instance_child.class_name()
{ {
paired_instances[*instance_index] = true; paired_instances[*instance_index] = true;
return true; return true;

View File

@@ -1,6 +1,6 @@
use std::{collections::HashMap, path::PathBuf}; 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; use crate::multimap::MultiMap;
@@ -147,8 +147,56 @@ pub struct InstanceWithMeta<'a> {
pub metadata: &'a InstanceMetadata, 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<String, RbxValue> {
&self.instance.properties
}
pub fn children(&self) -> &[RbxId] {
self.instance.get_children_ids()
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct InstanceWithMetaMut<'a> { pub struct InstanceWithMetaMut<'a> {
pub instance: &'a mut RbxInstance, pub instance: &'a mut RbxInstance,
pub metadata: &'a mut InstanceMetadata, 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<String, RbxValue> {
&self.instance.properties
}
pub fn properties_mut(&mut self) -> &mut HashMap<String, RbxValue> {
&mut self.instance.properties
}
pub fn children(&self) -> &[RbxId] {
self.instance.get_children_ids()
}
}