mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-04 05:00:05 +08:00
refactor(profile): name index operations after what they change
This commit is contained in:
@@ -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<Opt
|
||||
|
||||
match CoreManager::global().change_core(&clash_core).await {
|
||||
Ok(_) => {
|
||||
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<Opt
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn restart_core() -> 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
|
||||
|
||||
@@ -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<PrfOption>)
|
||||
}
|
||||
};
|
||||
|
||||
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<PrfOption>)
|
||||
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<std::string::String>) -> 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<ValidationO
|
||||
// Runtime refresh and tray rebuilding happen after saved node selections are restored.
|
||||
profiles::activate_selected_nodes();
|
||||
|
||||
if let Err(e) = profiles_save_file_safe().await {
|
||||
if let Err(e) = save_committed_profiles().await {
|
||||
logging!(warn, Type::Cmd, "异步保存配置文件失败: {e:#}");
|
||||
}
|
||||
|
||||
@@ -433,7 +432,7 @@ pub async fn patch_profile(index: std::string::String, profile: ProfilePatch) ->
|
||||
// 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")?;
|
||||
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ pub(super) async fn build_profile(item: &PrfItem, file_data: Option<String>) ->
|
||||
}
|
||||
|
||||
async fn append_companion_item(item: &mut PrfItem) -> Result<String> {
|
||||
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"))
|
||||
|
||||
@@ -233,9 +233,9 @@ pub(crate) async fn current_mapping(profiles: &IProfiles) -> Result<Mapping> {
|
||||
// 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<String>) -> Result<()> {
|
||||
pub(crate) async fn create_profile(item: &PrfItem, file_data: Option<String>) -> 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<IProfiles>, item: &mut PrfItem) -> Result<()> {
|
||||
pub(super) async fn append_to_profile_index(profiles: &Draft<IProfiles>, 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<IProfiles>, 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
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user