Refactor error handling and improve code readability in ServeSession and applyPatch

- Enhanced error logging in ServeSession by formatting warning messages for better clarity.
- Improved readability in applyPatch by restructuring conditional statements for clarity.
- Simplified assertions in tests for script pruning to maintain consistency and readability.

These changes aim to improve code maintainability and enhance the clarity of error messages.
This commit is contained in:
Joe 2026-02-28 06:44:37 -06:00
parent 9820ec6534
commit dcd48642be
5 changed files with 25 additions and 27 deletions

9
.cursor/hooks.json Normal file
View file

@ -0,0 +1,9 @@
{
"version": 1,
"hooks": {
"stop": [
{ "command": "cargo fmt" },
{ "command": "stylua plugin/src" }
]
}
}

View file

@ -76,10 +76,11 @@ local function applyPatch(instanceMap, patch)
remapProgress = false remapProgress = false
for id, virtualInstance in pairs(patch.added) do for id, virtualInstance in pairs(patch.added) do
local parentInstance = instanceMap.fromIds[virtualInstance.Parent] local parentInstance = instanceMap.fromIds[virtualInstance.Parent]
if not parentInstance then continue end if not parentInstance then
continue
end
for _, child in parentInstance:GetChildren() do for _, child in parentInstance:GetChildren() do
if child.Name == virtualInstance.Name if child.Name == virtualInstance.Name and child.ClassName == virtualInstance.ClassName then
and child.ClassName == virtualInstance.ClassName then
local existingId = instanceMap.fromInstances[child] local existingId = instanceMap.fromInstances[child]
if existingId and existingId ~= id then if existingId and existingId ~= id then
Log.trace("Echo remap: {} -> {} for {}", existingId, id, child:GetFullName()) Log.trace("Echo remap: {} -> {} for {}", existingId, id, child:GetFullName())

View file

@ -963,7 +963,10 @@ function ServeSession:__connectScriptsOnlyWatchers()
local scriptTempId = string.gsub(HttpService:GenerateGUID(false), "-", ""):lower() local scriptTempId = string.gsub(HttpService:GenerateGUID(false), "-", ""):lower()
local encoded = encodeInstance(descendant, prevParentId) local encoded = encodeInstance(descendant, prevParentId)
if not encoded then if not encoded then
Log.warn("Scripts-only: failed to encode new script {}, rolling back temp IDs", descendant:GetFullName()) Log.warn(
"Scripts-only: failed to encode new script {}, rolling back temp IDs",
descendant:GetFullName()
)
for tempId in patch.added do for tempId in patch.added do
self.__instanceMap:removeId(tempId) self.__instanceMap:removeId(tempId)
end end

View file

@ -473,9 +473,7 @@ impl ApiService {
let before = request.updated.len(); let before = request.updated.len();
request.updated.retain(|update| { request.updated.retain(|update| {
tree.get_instance(update.id) tree.get_instance(update.id)
.map(|inst| { .map(|inst| crate::snapshot::is_script_class(inst.class_name().as_str()))
crate::snapshot::is_script_class(inst.class_name().as_str())
})
.unwrap_or(false) .unwrap_or(false)
}); });
let filtered = before - request.updated.len(); let filtered = before - request.updated.len();
@ -561,8 +559,7 @@ impl ApiService {
continue; continue;
} }
let parent = added.parent.unwrap_or(Ref::none()); let parent = added.parent.unwrap_or(Ref::none());
let real_parent = let real_parent = temp_to_real.get(&parent).copied().unwrap_or(parent);
temp_to_real.get(&parent).copied().unwrap_or(parent);
if tree.get_instance(real_parent).is_none() { if tree.get_instance(real_parent).is_none() {
continue; continue;
} }
@ -3642,10 +3639,10 @@ fn filter_subscribe_message_for_scripts<'a>(
// in the tree but weren't part of this patch. Without injection the // in the tree but weren't part of this patch. Without injection the
// plugin receives an orphaned script with an unknown parent. // plugin receives an orphaned script with an unknown parent.
for id in included_ids.iter().copied().collect::<Vec<_>>() { for id in included_ids.iter().copied().collect::<Vec<_>>() {
if !msg.added.contains_key(&id) { if let std::collections::hash_map::Entry::Vacant(e) = msg.added.entry(id) {
if let Some(tree_inst) = tree.get_instance(id) { if let Some(tree_inst) = tree.get_instance(id) {
let inst = instance_for_scripts_only(tree_inst, &included_ids); let inst = instance_for_scripts_only(tree_inst, &included_ids);
msg.added.insert(id, inst); e.insert(inst);
} }
} }
} }

View file

@ -18,10 +18,7 @@ fn scripts_only_read_prunes_non_script_subtrees() {
.map(|inst| inst.name.as_ref()) .map(|inst| inst.name.as_ref())
.collect(); .collect();
assert!( assert!(names.contains(&"TopScript"), "TopScript should be included");
names.contains(&"TopScript"),
"TopScript should be included"
);
assert!( assert!(
names.contains(&"DeepScript"), names.contains(&"DeepScript"),
"DeepScript should be included" "DeepScript should be included"
@ -31,18 +28,9 @@ fn scripts_only_read_prunes_non_script_subtrees() {
"SomeScript should be included" "SomeScript should be included"
); );
assert!( assert!(!names.contains(&"PartA"), "PartA should be pruned");
!names.contains(&"PartA"), assert!(!names.contains(&"PartB"), "PartB should be pruned");
"PartA should be pruned" assert!(!names.contains(&"SomeModel"), "SomeModel should be pruned");
);
assert!(
!names.contains(&"PartB"),
"PartB should be pruned"
);
assert!(
!names.contains(&"SomeModel"),
"SomeModel should be pruned"
);
assert!( assert!(
!names.contains(&"PureParts"), !names.contains(&"PureParts"),
"PureParts folder should be pruned (no script descendants)" "PureParts folder should be pruned (no script descendants)"