From 44aa5f243074007a7139a67c9271bff89745a4ac Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Fri, 4 Dec 2026 01:48:32 +0800 Subject: [PATCH] refactor(profile): simplify saved body validation and rollback --- src-tauri/src/cmd/save_profile.rs | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src-tauri/src/cmd/save_profile.rs b/src-tauri/src/cmd/save_profile.rs index a8c848a2e..be4954ab3 100644 --- a/src-tauri/src/cmd/save_profile.rs +++ b/src-tauri/src/cmd/save_profile.rs @@ -51,7 +51,6 @@ pub async fn save_profile_file( let profiles_dir = dirs::app_profiles_dir().stringify_err()?; let file_path = profiles_dir.join(rel_path.as_str()); - let file_path_str = file_path.to_string_lossy().to_string(); // 读取原始内容(在释放profiles_guard后进行) let original_existed = fs::try_exists(&file_path).await.map_err(|err| { @@ -61,9 +60,9 @@ pub async fn save_profile_file( )) })?; let original_content = if original_existed { - read_profile_item_file(Some(rel_path.as_str())).await.stringify_err()? + Some(read_profile_item_file(Some(rel_path.as_str())).await.stringify_err()?) } else { - String::new() + None }; // 保存新的配置文件 @@ -71,20 +70,12 @@ pub async fn save_profile_file( drop(profile_guard); let original = OriginalProfileFile { - content: &original_content, - existed: original_existed, + content: original_content.as_deref(), written: &file_data, }; - let changes_applied = handle_saved_profile_file( - &file_path_str, - &file_path, - &original, - is_merge_file, - is_script_file, - affects_runtime, - ) - .await?; + let changes_applied = + validate_and_apply_profile(&file_path, &original, is_merge_file, is_script_file, affects_runtime).await?; if changes_applied.is_valid() && let Some(trigger) = backup_trigger @@ -96,8 +87,7 @@ pub async fn save_profile_file( } struct OriginalProfileFile<'a> { - content: &'a str, - existed: bool, + content: Option<&'a str>, written: &'a str, } @@ -106,8 +96,8 @@ async fn restore_original(file_path: &std::path::Path, original: &OriginalProfil if fs::read(file_path).await.stringify_err()? != original.written.as_bytes() { return Err("profile file changed during validation".into()); } - if original.existed { - fs::write(file_path, original.content).await.stringify_err() + if let Some(content) = original.content { + fs::write(file_path, content).await.stringify_err() } else { fs::remove_file(file_path).await.stringify_err() } @@ -134,8 +124,7 @@ fn profile_affects_runtime(profiles: &IProfiles, index: &str) -> bool { .contains(&index) } -async fn handle_saved_profile_file( - file_path_str: &str, +async fn validate_and_apply_profile( file_path: &std::path::Path, original: &OriginalProfileFile<'_>, is_merge_file: bool, @@ -150,7 +139,7 @@ async fn handle_saved_profile_file( (ValidationNoticeTarget::Runtime, "YAML配置文件") }; - match CoreConfigValidator::validate_config_file_outcome(file_path_str, Some(is_merge_file)).await { + match CoreConfigValidator::validate_config_file_outcome(&file_path.to_string_lossy(), Some(is_merge_file)).await { Ok(outcome) if outcome.is_valid() => {} Ok(outcome) => { logging!(warn, Type::Config, "[cmd配置save] 文件验证失败: {}", outcome);