From 651f297785bb662261f589ba71c007b0e3884fac Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Thu, 3 Dec 2026 19:38:42 +0800 Subject: [PATCH] refactor(profile): name index operations after what they change --- src-tauri/src/cmd/clash.rs | 6 +++--- src-tauri/src/cmd/profile.rs | 21 ++++++++++----------- src-tauri/src/config/config.rs | 6 +++--- src-tauri/src/config/prfitem.rs | 2 +- src-tauri/src/config/profiles.rs | 16 ++++++++-------- src-tauri/src/utils/resolve/scheme.rs | 4 ++-- 6 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src-tauri/src/cmd/clash.rs b/src-tauri/src/cmd/clash.rs index 8c6468955..0a9720719 100644 --- a/src-tauri/src/cmd/clash.rs +++ b/src-tauri/src/cmd/clash.rs @@ -4,7 +4,7 @@ use crate::feat; use crate::utils::{dirs, yaml_emitter}; use crate::{ cmd::StringifyErr as _, - config::{ClashInfo, Config, profiles::profiles_save_file_safe}, + config::{ClashInfo, Config, profiles::save_committed_profiles}, constants, core::{ CoreManager, handle, @@ -59,7 +59,7 @@ pub async fn change_clash_core(clash_core: std::string::String) -> CmdResult { - logging_error!(Type::Core, profiles_save_file_safe().await); + logging_error!(Type::Core, save_committed_profiles().await); match CoreManager::global().restart_core().await { Ok(_) => { @@ -88,7 +88,7 @@ pub async fn change_clash_core(clash_core: std::string::String) -> CmdResult CmdResult { - logging_error!(Type::Core, profiles_save_file_safe().await); + logging_error!(Type::Core, save_committed_profiles().await); let result = CoreManager::global() .restart_core() .await diff --git a/src-tauri/src/cmd/profile.rs b/src-tauri/src/cmd/profile.rs index 18163b48d..23f03c47e 100644 --- a/src-tauri/src/cmd/profile.rs +++ b/src-tauri/src/cmd/profile.rs @@ -7,9 +7,8 @@ use crate::{ config::{ Config, IProfiles, PrfItem, PrfOption, build_remote_profile, profiles::{ - PROFILE_WRITE_LOCK, cleanup_profile_files, profiles_append_item_safe, - profiles_append_item_with_filedata_safe, profiles_patch_item_safe, profiles_reorder_safe, - profiles_save_file_safe, save_profiles, + PROFILE_WRITE_LOCK, append_profile, cleanup_profile_files, patch_profile_item, reorder_profiles, + save_committed_profiles, save_profiles, }, read_profile_item_file, }, @@ -80,12 +79,12 @@ pub async fn import_profile(url: std::string::String, option: Option) } }; - if let Err(e) = profiles_append_item_safe(item).await { + if let Err(e) = append_profile(item).await { logging!(error, Type::Cmd, "[导入订阅] 保存配置失败: {e:#}"); return Err(coded_error("PROFILE_IMPORT_FAILED", e)); } - if let Err(e) = profiles_save_file_safe().await { + if let Err(e) = save_committed_profiles().await { logging!(error, Type::Cmd, "[导入订阅] 保存配置文件失败: {e:#}"); return Err(coded_error("PROFILE_IMPORT_FAILED", e)); } @@ -104,7 +103,7 @@ pub async fn import_profile(url: std::string::String, option: Option) pub async fn reorder_profile(active_id: std::string::String, over_id: std::string::String) -> CmdResult { let active_id: String = active_id.into(); let over_id: String = over_id.into(); - match profiles_reorder_safe(&active_id, &over_id).await { + match reorder_profiles(&active_id, &over_id).await { Ok(_) => { logging!(info, Type::Cmd, "重新排序配置文件: {} -> {}", active_id, over_id); Ok(()) @@ -127,9 +126,9 @@ pub async fn reorder_profile(active_id: std::string::String, over_id: std::strin pub async fn create_profile(item: ProfileCreate, file_data: Option) -> CmdResult { let item: PrfItem = item.into(); let file_data = file_data.map(String::from); - match profiles_append_item_with_filedata_safe(&item, file_data).await { + match profiles::create_profile(&item, file_data).await { Ok(_) => { - profiles_save_file_safe() + save_committed_profiles() .await .with_error_code("PROFILE_CREATE_FAILED")?; logging_error!(Type::Timer, Timer::global().refresh().await); @@ -219,7 +218,7 @@ async fn restore_previous_profile(prev_profile: &String) -> CmdResult<()> { .edit_draft(|d| d.patch_current(Some(prev_profile))); Config::profiles().await.apply(); crate::process::AsyncHandler::spawn(|| async move { - if let Err(e) = profiles_save_file_safe().await { + if let Err(e) = save_committed_profiles().await { logging!(warn, Type::Cmd, "异步保存恢复配置文件失败: {e:#}"); } }); @@ -247,7 +246,7 @@ async fn handle_success(current_value: Option<&String>) -> CmdResult // Prevent an in-flight restore from overwriting a newer UI or chain selection. let records_a_selection = profile.selected.is_some(); - profiles_patch_item_safe(&index, &profile) + patch_profile_item(&index, &profile) .await .with_error_code("PROFILE_UPDATE_FAILED")?; diff --git a/src-tauri/src/config/config.rs b/src-tauri/src/config/config.rs index ec7ccc028..240d66636 100644 --- a/src-tauri/src/config/config.rs +++ b/src-tauri/src/config/config.rs @@ -3,7 +3,7 @@ use crate::core::notify::NoticeStatus; use crate::{ config::{ prfitem::{build_merge_profile, build_script_profile}, - profiles::{load_profiles, profiles_append_item_to_safe, save_profiles}, + profiles::{append_to_profile_index, load_profiles, save_profiles}, runtime::IRuntime, }, constants::{files, timing}, @@ -142,11 +142,11 @@ impl Config { if profiles.latest_arc().get_item("Merge").is_err() { let merge_item = &mut build_merge_profile(Some("Merge".into())); - profiles_append_item_to_safe(&profiles, merge_item).await?; + append_to_profile_index(&profiles, merge_item).await?; } if profiles.latest_arc().get_item("Script").is_err() { let script_item = &mut build_script_profile(Some("Script".into())); - profiles_append_item_to_safe(&profiles, script_item).await?; + append_to_profile_index(&profiles, script_item).await?; } Ok(()) } diff --git a/src-tauri/src/config/prfitem.rs b/src-tauri/src/config/prfitem.rs index d1e795400..2eb1fb608 100644 --- a/src-tauri/src/config/prfitem.rs +++ b/src-tauri/src/config/prfitem.rs @@ -45,7 +45,7 @@ pub(super) async fn build_profile(item: &PrfItem, file_data: Option) -> } async fn append_companion_item(item: &mut PrfItem) -> Result { - profiles::profiles_append_item_safe(item).await?; + profiles::append_profile(item).await?; item.uid .clone() .ok_or_else(|| anyhow::anyhow!("appended companion item has no uid")) diff --git a/src-tauri/src/config/profiles.rs b/src-tauri/src/config/profiles.rs index 3b25a8fcc..121cbe78f 100644 --- a/src-tauri/src/config/profiles.rs +++ b/src-tauri/src/config/profiles.rs @@ -233,9 +233,9 @@ pub(crate) async fn current_mapping(profiles: &IProfiles) -> Result { // These helpers serialize asynchronous operations against committed profile data. use crate::config::Config; -pub(crate) async fn profiles_append_item_with_filedata_safe(item: &PrfItem, file_data: Option) -> Result<()> { +pub(crate) async fn create_profile(item: &PrfItem, file_data: Option) -> Result<()> { let item = &mut build_profile(item, file_data).await?; - profiles_append_item_safe(item).await + append_profile(item).await } /// Applies `edit` to the committed profiles; commits only when the edit succeeds. @@ -253,12 +253,12 @@ where .await } -pub(crate) async fn profiles_append_item_safe(item: &mut PrfItem) -> Result<()> { +pub(crate) async fn append_profile(item: &mut PrfItem) -> Result<()> { let profiles = Config::profiles().await; - profiles_append_item_to_safe(&profiles, item).await + append_to_profile_index(&profiles, item).await } -pub(super) async fn profiles_append_item_to_safe(profiles: &Draft, item: &mut PrfItem) -> Result<()> { +pub(super) async fn append_to_profile_index(profiles: &Draft, item: &mut PrfItem) -> Result<()> { profiles .with_data_modify(|mut profiles| async move { append_item(&mut profiles, item).await?; @@ -267,7 +267,7 @@ pub(super) async fn profiles_append_item_to_safe(profiles: &Draft, it .await } -pub(crate) async fn profiles_patch_item_safe(index: &str, item: &PrfItem) -> Result<()> { +pub(crate) async fn patch_profile_item(index: &str, item: &PrfItem) -> Result<()> { with_profiles_edit(|mut profiles| async move { patch_item(&mut profiles, index, item).await?; Ok(profiles) @@ -275,7 +275,7 @@ pub(crate) async fn profiles_patch_item_safe(index: &str, item: &PrfItem) -> Res .await } -pub(crate) async fn profiles_reorder_safe(active_id: &str, over_id: &str) -> Result<()> { +pub(crate) async fn reorder_profiles(active_id: &str, over_id: &str) -> Result<()> { with_profiles_edit(|mut profiles| async move { if profiles.reorder(active_id, over_id) { save_profiles(&profiles).await?; @@ -285,7 +285,7 @@ pub(crate) async fn profiles_reorder_safe(active_id: &str, over_id: &str) -> Res .await } -pub(crate) async fn profiles_save_file_safe() -> Result<()> { +pub(crate) async fn save_committed_profiles() -> Result<()> { with_profiles_edit(|profiles| async move { save_profiles(&profiles).await.map(|_| profiles) }).await } diff --git a/src-tauri/src/utils/resolve/scheme.rs b/src-tauri/src/utils/resolve/scheme.rs index b16ffa448..5ff14ac56 100644 --- a/src-tauri/src/utils/resolve/scheme.rs +++ b/src-tauri/src/utils/resolve/scheme.rs @@ -51,14 +51,14 @@ async fn import_subscription(url: &str, name: Option<&String>) { }; let uid = item.uid.clone().unwrap_or_default(); - if let Err(e) = profiles::profiles_append_item_safe(&mut item).await { + if let Err(e) = profiles::append_profile(&mut item).await { logging!(error, Type::Config, "failed to import subscription url: {:?}", e); Config::profiles().await.discard(); handle::Handle::notice(NoticeStatus::ImportSubUrlError, e.to_string()); return; } - if let Err(e) = profiles::profiles_save_file_safe().await { + if let Err(e) = profiles::save_committed_profiles().await { logging!(error, Type::Config, "failed to save imported subscription: {e:#}"); handle::Handle::notice(NoticeStatus::ImportSubUrlError, e.to_string()); return;