From 84a406ff108bb089b40ce6d3a9a17bb6b82e0dbf Mon Sep 17 00:00:00 2001 From: Nathan Hammond Date: Mon, 17 Jul 2023 20:00:11 +0800 Subject: [PATCH] Path doesn't need cloning, just a borrowed mutable reference. --- crates/turborepo-lib/src/rewrite_json.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/crates/turborepo-lib/src/rewrite_json.rs b/crates/turborepo-lib/src/rewrite_json.rs index 1afebebaa01051..4ce75d6a795a0e 100644 --- a/crates/turborepo-lib/src/rewrite_json.rs +++ b/crates/turborepo-lib/src/rewrite_json.rs @@ -47,7 +47,8 @@ pub fn set_path( let root = get_root(json_document_string)?; // Find the token we'll be modifying and its path from the root. - let (closest_path, closest_node) = get_closest_node(&root, &path, vec![]); + let current_path = &mut vec![]; + let (closest_path, closest_node) = get_closest_node(&root, &path, current_path); // Pull the token metadata off of the token. let (property_count, range): (usize, jsonc_parser::common::Range) = match closest_node { @@ -127,8 +128,8 @@ fn get_root(json_document_string: &str) -> Result( current_node: &'a jsonc_parser::ast::Value<'a>, target_path: &[&'a str], - current_path: Vec<&'a str>, -) -> (Vec<&'a str>, &'a jsonc_parser::ast::Value<'a>) { + current_path: &'a mut Vec<&'a str>, +) -> (&'a mut Vec<&'a str>, &'a jsonc_parser::ast::Value<'a>) { // No target_path? We've arrived. if target_path.is_empty() { return (current_path, current_node); @@ -149,7 +150,7 @@ fn get_closest_node<'a>( Some(property) => { let next_current_node = &property.value; let next_property_name = property.name.as_str(); - let mut next_current_path = current_path.clone(); + let next_current_path = &mut *current_path; next_current_path.push(next_property_name); let next_target_path = &target_path[1..]; @@ -215,7 +216,7 @@ pub fn unset_path( // The key path can appear multiple times. This a vec that contains each time it // occurs. - let path_ranges = find_all_paths(&root, &path, vec![]); + let path_ranges = find_all_paths(&root, &path, &mut vec![]); if path_ranges.is_empty() { return Ok((false, json_document_string.to_owned())); @@ -262,7 +263,7 @@ pub fn unset_path( fn find_all_paths<'a>( current_node: &'a jsonc_parser::ast::Value<'a>, target_path: &[&'a str], - current_path: Vec<&'a str>, + current_path: &mut Vec<&'a str>, ) -> Vec { let mut ranges: Vec = vec![]; @@ -323,7 +324,7 @@ fn find_all_paths<'a>( // We must recurse. let next_current_node = &property.value; let next_property_name = property.name.as_str(); - let mut next_current_path = current_path.clone(); + let next_current_path = &mut *current_path; next_current_path.push(next_property_name); let next_target_path = &target_path[1..];