mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-04 05:00:05 +08:00
refactor(profile): remove companions from the deleted profile
This commit is contained in:
@@ -199,41 +199,30 @@ impl IProfiles {
|
||||
|
||||
pub fn plan_delete_item(&mut self, uid: &str) -> Result<ProfileDeletePlan, ProfileNotFound> {
|
||||
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::<Vec<_>>()
|
||||
});
|
||||
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<PrfItem>, target_uid: Option<&str>) -> Option<String> {
|
||||
fn remove_item(items: &mut Vec<PrfItem>, target_uid: Option<&str>) -> Option<PrfItem> {
|
||||
let index = items.iter().position(|item| item.uid.as_deref() == target_uid)?;
|
||||
items.remove(index).file
|
||||
Some(items.remove(index))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user