diff --git a/src-tauri/src/cmds.rs b/src-tauri/src/cmds.rs index d2b0bab..03dac1f 100644 --- a/src-tauri/src/cmds.rs +++ b/src-tauri/src/cmds.rs @@ -1,5 +1,5 @@ use crate::{ - core::{ClashInfo, ProfileItem, ProfilesConfig, VergeConfig}, + core::{ClashInfo, ProfileItem, Profiles, VergeConfig}, states::{ClashState, ProfilesState, VergeState}, utils::{dirs::app_home_dir, fetch::fetch_profile, sysopt::SysProxyConfig}, }; @@ -10,8 +10,8 @@ use tauri::State; /// get all profiles from `profiles.yaml` /// do not acquire the lock of ProfileLock #[tauri::command] -pub fn get_profiles(profiles: State<'_, ProfilesState>) -> Result { - match profiles.0.lock() { +pub fn get_profiles(profiles_state: State<'_, ProfilesState>) -> Result { + match profiles_state.0.lock() { Ok(profiles) => Ok(profiles.clone()), Err(_) => Err("failed to get profiles lock".into()), } @@ -19,8 +19,8 @@ pub fn get_profiles(profiles: State<'_, ProfilesState>) -> Result) -> Result<(), String> { - match profiles.0.lock() { +pub fn sync_profiles(profiles_state: State<'_, ProfilesState>) -> Result<(), String> { + match profiles_state.0.lock() { Ok(mut profiles) => profiles.sync_file(), Err(_) => Err("failed to get profiles lock".into()), } @@ -32,11 +32,11 @@ pub fn sync_profiles(profiles: State<'_, ProfilesState>) -> Result<(), String> { pub async fn import_profile( url: String, with_proxy: bool, - profiles: State<'_, ProfilesState>, + profiles_state: State<'_, ProfilesState>, ) -> Result<(), String> { match fetch_profile(&url, with_proxy).await { Some(result) => { - let mut profiles = profiles.0.lock().unwrap(); + let mut profiles = profiles_state.0.lock().unwrap(); profiles.import_from_url(url, result) } None => Err(format!("failed to fetch profile from `{}`", url)), @@ -48,11 +48,11 @@ pub async fn import_profile( pub async fn update_profile( index: usize, with_proxy: bool, - clash: State<'_, ClashState>, - profiles: State<'_, ProfilesState>, + clash_state: State<'_, ClashState>, + profiles_state: State<'_, ProfilesState>, ) -> Result<(), String> { // maybe we can get the url from the web app directly - let url = match profiles.0.lock() { + let url = match profiles_state.0.lock() { Ok(mut profile) => { let items = profile.items.take().unwrap_or(vec![]); if index >= items.len() { @@ -69,14 +69,14 @@ pub async fn update_profile( }; match fetch_profile(&url, with_proxy).await { - Some(result) => match profiles.0.lock() { + Some(result) => match profiles_state.0.lock() { Ok(mut profiles) => { profiles.update_item(index, result)?; // reactivate the profile let current = profiles.current.clone().unwrap_or(0); if current == index { - let clash = clash.0.lock().unwrap(); + let clash = clash_state.0.lock().unwrap(); profiles.activate(&clash) } else { Ok(()) @@ -92,14 +92,14 @@ pub async fn update_profile( #[tauri::command] pub fn select_profile( index: usize, - clash: State<'_, ClashState>, - profiles: State<'_, ProfilesState>, + clash_state: State<'_, ClashState>, + profiles_state: State<'_, ProfilesState>, ) -> Result<(), String> { - let mut profiles = profiles.0.lock().unwrap(); + let mut profiles = profiles_state.0.lock().unwrap(); match profiles.put_current(index) { Ok(()) => { - let clash = clash.0.lock().unwrap(); + let clash = clash_state.0.lock().unwrap(); profiles.activate(&clash) } Err(err) => Err(err), @@ -131,9 +131,9 @@ pub fn delete_profile( pub fn patch_profile( index: usize, profile: ProfileItem, - profiles: State<'_, ProfilesState>, + profiles_state: State<'_, ProfilesState>, ) -> Result<(), String> { - match profiles.0.lock() { + match profiles_state.0.lock() { Ok(mut profiles) => profiles.patch_item(index, profile), Err(_) => Err("can not get profiles lock".into()), } diff --git a/src-tauri/src/core/clash.rs b/src-tauri/src/core/clash.rs index 8b74cc6..c7a23a9 100644 --- a/src-tauri/src/core/clash.rs +++ b/src-tauri/src/core/clash.rs @@ -1,4 +1,4 @@ -use super::{ProfilesConfig, Verge}; +use super::{Profiles, Verge}; use crate::utils::{config, dirs}; use serde::{Deserialize, Serialize}; use serde_yaml::{Mapping, Value}; @@ -139,7 +139,7 @@ impl Clash { /// restart clash sidecar /// should reactivate profile after restart - pub fn restart_sidecar(&mut self, profiles: &mut ProfilesConfig) -> Result<(), String> { + pub fn restart_sidecar(&mut self, profiles: &mut Profiles) -> Result<(), String> { self.update_config(); self.drop_sidecar()?; self.run_sidecar()?; @@ -171,7 +171,7 @@ impl Clash { &mut self, patch: Mapping, verge: &mut Verge, - profiles: &mut ProfilesConfig, + profiles: &mut Profiles, ) -> Result<(), String> { for (key, value) in patch.iter() { let value = value.clone(); diff --git a/src-tauri/src/core/profiles.rs b/src-tauri/src/core/profiles.rs index 386d6b7..b93e664 100644 --- a/src-tauri/src/core/profiles.rs +++ b/src-tauri/src/core/profiles.rs @@ -11,7 +11,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; /// Define the `profiles.yaml` schema #[derive(Default, Debug, Clone, Deserialize, Serialize)] -pub struct ProfilesConfig { +pub struct Profiles { /// current profile's name pub current: Option, @@ -63,10 +63,10 @@ pub struct ProfileResponse { static PROFILE_YAML: &str = "profiles.yaml"; static PROFILE_TEMP: &str = "clash-verge-runtime.yaml"; -impl ProfilesConfig { +impl Profiles { /// read the config from the file pub fn read_file() -> Self { - config::read_yaml::(dirs::app_home_dir().join(PROFILE_YAML)) + config::read_yaml::(dirs::app_home_dir().join(PROFILE_YAML)) } /// save the config to the file @@ -99,7 +99,7 @@ impl ProfilesConfig { File::create(path).unwrap().write(file_data).unwrap(); // update `profiles.yaml` - let data = ProfilesConfig::read_file(); + let data = Profiles::read_file(); let mut items = data.items.unwrap_or(vec![]); let now = SystemTime::now() diff --git a/src-tauri/src/states.rs b/src-tauri/src/states.rs index 8d67fc5..1e10192 100644 --- a/src-tauri/src/states.rs +++ b/src-tauri/src/states.rs @@ -1,8 +1,8 @@ -use crate::core::{Clash, ProfilesConfig, Verge}; +use crate::core::{Clash, Profiles, Verge}; use std::sync::{Arc, Mutex}; #[derive(Default)] -pub struct ProfilesState(pub Arc>); +pub struct ProfilesState(pub Arc>); #[derive(Default)] pub struct ClashState(pub Arc>); diff --git a/src-tauri/src/utils/resolve.rs b/src-tauri/src/utils/resolve.rs index 21b18c1..343b880 100644 --- a/src-tauri/src/utils/resolve.rs +++ b/src-tauri/src/utils/resolve.rs @@ -1,5 +1,5 @@ use super::{init, server}; -use crate::{core::ProfilesConfig, states}; +use crate::{core::Profiles, states}; use tauri::{App, AppHandle, Manager}; use tauri_plugin_shadows::Shadows; @@ -38,7 +38,7 @@ pub fn resolve_setup(app: &App) { log::error!("{}", err); } - *profiles = ProfilesConfig::read_file(); + *profiles = Profiles::read_file(); if let Err(err) = profiles.activate(&clash) { log::error!("{}", err); }