From ec8861e9830621b7d0897a2095246e7cfc0af430 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Thu, 19 Sep 2019 11:10:25 -0700 Subject: [PATCH] Apply patch sets by value in preparation for AppliedPatchSet --- src/commands/build.rs | 2 +- src/commands/serve.rs | 2 +- src/commands/upload.rs | 2 +- src/snapshot/patch_apply.rs | 44 ++++++++++++++++++++----------------- 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/commands/build.rs b/src/commands/build.rs index 1ae1ee48..86ae0bb3 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -105,7 +105,7 @@ pub fn build(options: &BuildOptions) -> Result<(), BuildError> { let patch_set = compute_patch_set(&snapshot, &tree, root_id); log::trace!("Applying patch set"); - apply_patch_set(&mut tree, &patch_set); + apply_patch_set(&mut tree, patch_set); log::trace!("Opening output file for write"); let mut file = BufWriter::new(File::create(&options.output_file)?); diff --git a/src/commands/serve.rs b/src/commands/serve.rs index a29e4439..26ad77d5 100644 --- a/src/commands/serve.rs +++ b/src/commands/serve.rs @@ -68,7 +68,7 @@ pub fn serve(options: &ServeOptions) -> Result<(), ServeError> { .expect("snapshot did not return an instance"); let patch_set = compute_patch_set(&snapshot, &tree, root_id); - apply_patch_set(&mut tree, &patch_set); + apply_patch_set(&mut tree, patch_set); let session = Arc::new(ServeSession::new(imfs, tree, maybe_project)); let server = LiveServer::new(session); diff --git a/src/commands/upload.rs b/src/commands/upload.rs index 5909e202..df0879b4 100644 --- a/src/commands/upload.rs +++ b/src/commands/upload.rs @@ -72,7 +72,7 @@ pub fn upload(options: UploadOptions) -> Result<(), UploadError> { let patch_set = compute_patch_set(&snapshot, &tree, root_id); log::trace!("Applying patch set"); - apply_patch_set(&mut tree, &patch_set); + apply_patch_set(&mut tree, patch_set); let root_id = tree.get_root_id(); diff --git a/src/snapshot/patch_apply.rs b/src/snapshot/patch_apply.rs index 986f9c17..5521f997 100644 --- a/src/snapshot/patch_apply.rs +++ b/src/snapshot/patch_apply.rs @@ -9,18 +9,18 @@ use super::{ InstancePropertiesWithMeta, InstanceSnapshot, RojoTree, }; -pub fn apply_patch_set(tree: &mut RojoTree, patch_set: &PatchSet) { +pub fn apply_patch_set(tree: &mut RojoTree, patch_set: PatchSet) { let mut context = PatchApplyContext::default(); - for removed_id in &patch_set.removed_instances { - tree.remove_instance(*removed_id); + for removed_id in patch_set.removed_instances { + tree.remove_instance(removed_id); } - for add_patch in &patch_set.added_instances { + for add_patch in patch_set.added_instances { apply_add_child(&mut context, tree, add_patch.parent_id, &add_patch.instance); } - for update_patch in &patch_set.updated_instances { + for update_patch in patch_set.updated_instances { apply_update_child(&context, tree, update_patch); } @@ -95,43 +95,47 @@ fn apply_add_child( } } -fn apply_update_child(context: &PatchApplyContext, tree: &mut RojoTree, patch: &PatchUpdate) { - if let Some(metadata) = &patch.changed_metadata { - tree.update_metadata(patch.id, metadata.clone()); +fn apply_update_child(context: &PatchApplyContext, tree: &mut RojoTree, patch: PatchUpdate) { + if let Some(metadata) = patch.changed_metadata { + tree.update_metadata(patch.id, metadata); } 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.name_mut() = name.clone(); + if let Some(name) = patch.changed_name { + *instance.name_mut() = name; } - if let Some(class_name) = &patch.changed_class_name { - *instance.class_name_mut() = class_name.clone(); + if let Some(class_name) = patch.changed_class_name { + *instance.class_name_mut() = class_name; } - for (key, property_entry) in &patch.changed_properties { + for (key, property_entry) in patch.changed_properties { match property_entry { // Ref values need to be potentially rewritten from snapshot IDs to // instance IDs if they referred to an instance that was created as // part of this patch. 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) + .copied() + .unwrap_or(id); instance.properties_mut().insert( - key.clone(), + key, RbxValue::Ref { - value: Some(*new_id), + value: Some(new_id), }, ); } Some(value) => { - instance.properties_mut().insert(key.clone(), value.clone()); + instance.properties_mut().insert(key, value); } None => { - instance.properties_mut().remove(key); + instance.properties_mut().remove(&key); } } } @@ -182,7 +186,7 @@ mod test { ..Default::default() }; - apply_patch_set(&mut tree, &patch_set); + apply_patch_set(&mut tree, patch_set); let root_instance = tree.get_instance(root_id).unwrap(); let child_id = root_instance.children()[0]; @@ -235,7 +239,7 @@ mod test { ..Default::default() }; - apply_patch_set(&mut tree, &patch_set); + apply_patch_set(&mut tree, patch_set); let expected_properties = hashmap! { "Foo".to_owned() => RbxValue::Int32 { value: 8 },