diff --git a/crates/clash-verge-profile/src/lib.rs b/crates/clash-verge-profile/src/lib.rs index 261e0079c..cdf82788b 100644 --- a/crates/clash-verge-profile/src/lib.rs +++ b/crates/clash-verge-profile/src/lib.rs @@ -199,41 +199,30 @@ impl IProfiles { pub fn plan_delete_item(&mut self, uid: &str) -> Result { let deleting_current = self.current.as_deref().is_none_or(|current| current == uid); - let delete_uids = self.get_item(uid)?.option.as_ref().map_or_else(Vec::new, |op| { - [ - op.merge.clone(), - op.script.clone(), - op.rules.clone(), - op.proxies.clone(), - op.groups.clone(), - ] - .into_iter() - .collect::>() - }); - let mut items = self.items.take().unwrap_or_default(); - let mut files = Vec::new(); - - if let Some(file) = take_item_file_by_uid(&mut items, Some(uid)) { - files.push(file); - } - - for delete_uid in delete_uids { - if let Some(file) = take_item_file_by_uid(&mut items, delete_uid.as_deref()) { - files.push(file); - } - } - - if deleting_current { - self.current = None; - for item in items.iter() { - if matches!(item.itype.as_deref(), Some("remote" | "local")) { - self.current = item.uid.clone(); - break; + let missing_profile = || ProfileNotFound { uid: uid.to_owned() }; + let items = self.items.as_mut().ok_or_else(missing_profile)?; + let profile = remove_item(items, Some(uid)).ok_or_else(missing_profile)?; + let mut files = Vec::from_iter(profile.file); + if let Some(options) = profile.option { + for companion_uid in [ + options.merge, + options.script, + options.rules, + options.proxies, + options.groups, + ] { + if let Some(file) = remove_item(items, companion_uid.as_deref()).and_then(|item| item.file) { + files.push(file); } } } - self.items = Some(items); + if deleting_current { + self.current = items + .iter() + .find(|item| matches!(item.itype.as_deref(), Some("remote" | "local"))) + .and_then(|item| item.uid.clone()); + } Ok(ProfileDeletePlan { deleting_current, files, @@ -258,9 +247,9 @@ impl IProfiles { } } -fn take_item_file_by_uid(items: &mut Vec, target_uid: Option<&str>) -> Option { +fn remove_item(items: &mut Vec, target_uid: Option<&str>) -> Option { let index = items.iter().position(|item| item.uid.as_deref() == target_uid)?; - items.remove(index).file + Some(items.remove(index)) } #[cfg(test)]