mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
refactor: simplify function return types and remove unnecessary wraps
This commit is contained in:
@@ -254,3 +254,4 @@ cloned_instead_of_copied = "deny"
|
|||||||
unnecessary_self_imports = "deny"
|
unnecessary_self_imports = "deny"
|
||||||
unused_trait_names = "deny"
|
unused_trait_names = "deny"
|
||||||
wildcard_imports = "deny"
|
wildcard_imports = "deny"
|
||||||
|
unnecessary_wraps = "deny"
|
||||||
|
|||||||
@@ -84,8 +84,8 @@ pub async fn restart_app() -> CmdResult<()> {
|
|||||||
|
|
||||||
/// 获取便携版标识
|
/// 获取便携版标识
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn get_portable_flag() -> CmdResult<bool> {
|
pub fn get_portable_flag() -> bool {
|
||||||
Ok(*dirs::PORTABLE_FLAG.get().unwrap_or(&false))
|
*dirs::PORTABLE_FLAG.get().unwrap_or(&false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 获取应用目录
|
/// 获取应用目录
|
||||||
@@ -241,16 +241,14 @@ pub async fn copy_icon_file(path: String, icon_info: IconInfo) -> CmdResult<Stri
|
|||||||
|
|
||||||
/// 通知UI已准备就绪
|
/// 通知UI已准备就绪
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn notify_ui_ready() -> CmdResult<()> {
|
pub fn notify_ui_ready() {
|
||||||
logging!(info, Type::Cmd, "前端UI已准备就绪");
|
logging!(info, Type::Cmd, "前端UI已准备就绪");
|
||||||
ui::mark_ui_ready();
|
ui::mark_ui_ready();
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// UI加载阶段
|
/// UI加载阶段
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn update_ui_stage(stage: UiReadyStage) -> CmdResult<()> {
|
pub fn update_ui_stage(stage: UiReadyStage) {
|
||||||
logging!(info, Type::Cmd, "UI加载阶段更新: {:?}", &stage);
|
logging!(info, Type::Cmd, "UI加载阶段更新: {:?}", &stage);
|
||||||
ui::update_ui_ready_stage(stage);
|
ui::update_ui_ready_stage(stage);
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use crate::cmd::StringifyErr as _;
|
|||||||
use crate::core::{EventDrivenProxyManager, async_proxy_query::AsyncProxyQuery};
|
use crate::core::{EventDrivenProxyManager, async_proxy_query::AsyncProxyQuery};
|
||||||
use crate::process::AsyncHandler;
|
use crate::process::AsyncHandler;
|
||||||
use crate::{logging, utils::logging::Type};
|
use crate::{logging, utils::logging::Type};
|
||||||
|
use gethostname::gethostname;
|
||||||
use network_interface::NetworkInterface;
|
use network_interface::NetworkInterface;
|
||||||
use serde_yaml_ng::Mapping;
|
use serde_yaml_ng::Mapping;
|
||||||
|
|
||||||
@@ -61,11 +62,9 @@ pub async fn get_auto_proxy() -> CmdResult<Mapping> {
|
|||||||
|
|
||||||
/// 获取系统主机名
|
/// 获取系统主机名
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn get_system_hostname() -> CmdResult<String> {
|
pub fn get_system_hostname() -> String {
|
||||||
use gethostname::gethostname;
|
|
||||||
|
|
||||||
// 获取系统主机名,处理可能的非UTF-8字符
|
// 获取系统主机名,处理可能的非UTF-8字符
|
||||||
let hostname = match gethostname().into_string() {
|
match gethostname().into_string() {
|
||||||
Ok(name) => name,
|
Ok(name) => name,
|
||||||
Err(os_string) => {
|
Err(os_string) => {
|
||||||
// 对于包含非UTF-8的主机名,使用调试格式化
|
// 对于包含非UTF-8的主机名,使用调试格式化
|
||||||
@@ -73,9 +72,7 @@ pub fn get_system_hostname() -> CmdResult<String> {
|
|||||||
// 去掉可能存在的引号
|
// 去掉可能存在的引号
|
||||||
fallback.trim_matches('"').to_string()
|
fallback.trim_matches('"').to_string()
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
Ok(hostname)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 获取网络接口列表
|
/// 获取网络接口列表
|
||||||
|
|||||||
@@ -53,12 +53,12 @@ pub async fn get_running_mode() -> Result<Arc<RunningMode>, String> {
|
|||||||
|
|
||||||
/// 获取应用的运行时间(毫秒)
|
/// 获取应用的运行时间(毫秒)
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn get_app_uptime() -> CmdResult<u128> {
|
pub fn get_app_uptime() -> u128 {
|
||||||
Ok(APP_START_TIME.elapsed().as_millis())
|
APP_START_TIME.elapsed().as_millis()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 检查应用是否以管理员身份运行
|
/// 检查应用是否以管理员身份运行
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn is_admin() -> CmdResult<bool> {
|
pub fn is_admin() -> bool {
|
||||||
Ok(*APPS_RUN_AS_ADMIN)
|
*APPS_RUN_AS_ADMIN
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ mod platform {
|
|||||||
mod platform {
|
mod platform {
|
||||||
use super::CmdResult;
|
use super::CmdResult;
|
||||||
|
|
||||||
|
#[allow(clippy::unnecessary_wraps)]
|
||||||
pub const fn invoke_uwp_tool() -> CmdResult {
|
pub const fn invoke_uwp_tool() -> CmdResult {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,9 +57,7 @@ impl Config {
|
|||||||
Self::ensure_default_profile_items().await?;
|
Self::ensure_default_profile_items().await?;
|
||||||
|
|
||||||
// init Tun mode
|
// init Tun mode
|
||||||
if !cmd::system::is_admin().unwrap_or_default()
|
if !cmd::system::is_admin() && service::is_service_available().await.is_err() {
|
||||||
&& service::is_service_available().await.is_err()
|
|
||||||
{
|
|
||||||
let verge = Self::verge().await;
|
let verge = Self::verge().await;
|
||||||
verge.edit_draft(|d| {
|
verge.edit_draft(|d| {
|
||||||
d.enable_tun_mode = Some(false);
|
d.enable_tun_mode = Some(false);
|
||||||
|
|||||||
@@ -581,6 +581,7 @@ impl PrfItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 向前兼容,默认为订阅启用自动更新
|
// 向前兼容,默认为订阅启用自动更新
|
||||||
|
#[allow(clippy::unnecessary_wraps)]
|
||||||
const fn default_allow_auto_update() -> Option<bool> {
|
const fn default_allow_auto_update() -> Option<bool> {
|
||||||
Some(true)
|
Some(true)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,10 @@ impl CoreManager {
|
|||||||
|
|
||||||
match *self.get_running_mode() {
|
match *self.get_running_mode() {
|
||||||
RunningMode::Service => self.stop_core_by_service().await,
|
RunningMode::Service => self.stop_core_by_service().await,
|
||||||
RunningMode::Sidecar => self.stop_core_by_sidecar(),
|
RunningMode::Sidecar => {
|
||||||
|
self.stop_core_by_sidecar();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
RunningMode::NotRunning => Ok(()),
|
RunningMode::NotRunning => Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ impl CoreManager {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn stop_core_by_sidecar(&self) -> Result<()> {
|
pub(super) fn stop_core_by_sidecar(&self) {
|
||||||
logging!(info, Type::Core, "Stopping sidecar");
|
logging!(info, Type::Core, "Stopping sidecar");
|
||||||
defer! {
|
defer! {
|
||||||
self.set_running_mode(RunningMode::NotRunning);
|
self.set_running_mode(RunningMode::NotRunning);
|
||||||
@@ -106,7 +106,6 @@ impl CoreManager {
|
|||||||
drop(child);
|
drop(child);
|
||||||
logging!(trace, Type::Core, "Sidecar stopped (PID: {:?})", pid);
|
logging!(trace, Type::Core, "Sidecar stopped (PID: {:?})", pid);
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) async fn start_core_by_service(&self) -> Result<()> {
|
pub(super) async fn start_core_by_service(&self) -> Result<()> {
|
||||||
|
|||||||
@@ -456,12 +456,12 @@ impl ServiceManager {
|
|||||||
Self(ServiceStatus::Unavailable("Need Checks".into()))
|
Self(ServiceStatus::Unavailable("Need Checks".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn config() -> Option<clash_verge_service_ipc::IpcConfig> {
|
pub const fn config() -> clash_verge_service_ipc::IpcConfig {
|
||||||
Some(clash_verge_service_ipc::IpcConfig {
|
clash_verge_service_ipc::IpcConfig {
|
||||||
default_timeout: Duration::from_millis(30),
|
default_timeout: Duration::from_millis(30),
|
||||||
retry_delay: Duration::from_millis(250),
|
retry_delay: Duration::from_millis(250),
|
||||||
max_retries: 6,
|
max_retries: 6,
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn init(&mut self) -> Result<()> {
|
pub async fn init(&mut self) -> Result<()> {
|
||||||
|
|||||||
@@ -100,13 +100,12 @@ impl Sysopt {
|
|||||||
self.initialed.load(Ordering::SeqCst)
|
self.initialed.load(Ordering::SeqCst)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_guard_sysproxy(&self) -> Result<()> {
|
pub fn init_guard_sysproxy(&self) {
|
||||||
// 使用事件驱动代理管理器
|
// 使用事件驱动代理管理器
|
||||||
let proxy_manager = EventDrivenProxyManager::global();
|
let proxy_manager = EventDrivenProxyManager::global();
|
||||||
proxy_manager.notify_app_started();
|
proxy_manager.notify_app_started();
|
||||||
|
|
||||||
logging!(info, Type::Core, "已启用事件驱动代理守卫");
|
logging!(info, Type::Core, "已启用事件驱动代理守卫");
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// init the sysproxy
|
/// init the sysproxy
|
||||||
|
|||||||
@@ -301,8 +301,8 @@ impl Tray {
|
|||||||
let verge = Config::verge().await.latest_arc();
|
let verge = Config::verge().await.latest_arc();
|
||||||
let system_proxy = verge.enable_system_proxy.as_ref().unwrap_or(&false);
|
let system_proxy = verge.enable_system_proxy.as_ref().unwrap_or(&false);
|
||||||
let tun_mode = verge.enable_tun_mode.as_ref().unwrap_or(&false);
|
let tun_mode = verge.enable_tun_mode.as_ref().unwrap_or(&false);
|
||||||
let tun_mode_available = cmd::system::is_admin().unwrap_or_default()
|
let tun_mode_available =
|
||||||
|| service::is_service_available().await.is_ok();
|
cmd::system::is_admin() || service::is_service_available().await.is_ok();
|
||||||
let mode = {
|
let mode = {
|
||||||
Config::clash()
|
Config::clash()
|
||||||
.await
|
.await
|
||||||
@@ -640,7 +640,7 @@ fn create_subcreate_proxy_menu_item(
|
|||||||
current_profile_selected: &[PrfSelected],
|
current_profile_selected: &[PrfSelected],
|
||||||
proxy_group_order_map: Option<HashMap<String, usize>>,
|
proxy_group_order_map: Option<HashMap<String, usize>>,
|
||||||
proxy_nodes_data: Result<Proxies>,
|
proxy_nodes_data: Result<Proxies>,
|
||||||
) -> Result<Vec<Submenu<Wry>>> {
|
) -> Vec<Submenu<Wry>> {
|
||||||
let proxy_submenus: Vec<Submenu<Wry>> = {
|
let proxy_submenus: Vec<Submenu<Wry>> = {
|
||||||
let mut submenus: Vec<(String, usize, Submenu<Wry>)> = Vec::new();
|
let mut submenus: Vec<(String, usize, Submenu<Wry>)> = Vec::new();
|
||||||
|
|
||||||
@@ -767,7 +767,7 @@ fn create_subcreate_proxy_menu_item(
|
|||||||
.map(|(_, _, submenu)| submenu)
|
.map(|(_, _, submenu)| submenu)
|
||||||
.collect()
|
.collect()
|
||||||
};
|
};
|
||||||
Ok(proxy_submenus)
|
proxy_submenus
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_proxy_menu_item(
|
fn create_proxy_menu_item(
|
||||||
@@ -955,7 +955,7 @@ async fn create_tray_menu(
|
|||||||
¤t_profile_selected,
|
¤t_profile_selected,
|
||||||
proxy_group_order_map,
|
proxy_group_order_map,
|
||||||
proxy_nodes_data.map_err(anyhow::Error::from),
|
proxy_nodes_data.map_err(anyhow::Error::from),
|
||||||
)?;
|
);
|
||||||
|
|
||||||
let (proxies_menu, inline_proxy_items) = create_proxy_menu_item(
|
let (proxies_menu, inline_proxy_items) = create_proxy_menu_item(
|
||||||
app_handle,
|
app_handle,
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ mod app_init {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Setup deep link handling
|
/// Setup deep link handling
|
||||||
pub fn setup_deep_links(app: &tauri::App) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn setup_deep_links(app: &tauri::App) {
|
||||||
#[cfg(any(target_os = "linux", all(debug_assertions, windows)))]
|
#[cfg(any(target_os = "linux", all(debug_assertions, windows)))]
|
||||||
{
|
{
|
||||||
logging!(info, Type::Setup, "注册深层链接...");
|
logging!(info, Type::Setup, "注册深层链接...");
|
||||||
@@ -99,8 +99,6 @@ mod app_init {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Setup autostart plugin
|
/// Setup autostart plugin
|
||||||
@@ -246,9 +244,7 @@ pub fn run() {
|
|||||||
logging!(error, Type::Setup, "Failed to setup autostart: {}", e);
|
logging!(error, Type::Setup, "Failed to setup autostart: {}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(e) = app_init::setup_deep_links(app) {
|
app_init::setup_deep_links(app);
|
||||||
logging!(error, Type::Setup, "Failed to setup deep links: {}", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Err(e) = app_init::setup_window_state(app) {
|
if let Err(e) = app_init::setup_window_state(app) {
|
||||||
logging!(error, Type::Setup, "Failed to setup window state: {}", e);
|
logging!(error, Type::Setup, "Failed to setup window state: {}", e);
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ impl PlatformSpecification {
|
|||||||
// 使用默认值避免在同步上下文中执行异步操作
|
// 使用默认值避免在同步上下文中执行异步操作
|
||||||
let running_mode = "NotRunning".to_string();
|
let running_mode = "NotRunning".to_string();
|
||||||
|
|
||||||
let is_admin = system::is_admin().unwrap_or_default();
|
let is_admin = system::is_admin();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
system_name,
|
system_name,
|
||||||
|
|||||||
@@ -1,22 +1,12 @@
|
|||||||
use anyhow::Result;
|
|
||||||
use tauri_plugin_shell::process::CommandChild;
|
use tauri_plugin_shell::process::CommandChild;
|
||||||
|
|
||||||
use crate::{logging, utils::logging::Type};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct CommandChildGuard(Option<CommandChild>);
|
pub struct CommandChildGuard(Option<CommandChild>);
|
||||||
|
|
||||||
impl Drop for CommandChildGuard {
|
impl Drop for CommandChildGuard {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Err(err) = self.kill() {
|
self.kill();
|
||||||
logging!(
|
|
||||||
error,
|
|
||||||
Type::Service,
|
|
||||||
"Failed to kill child process: {}",
|
|
||||||
err
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,11 +17,10 @@ impl CommandChildGuard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn kill(&mut self) -> Result<()> {
|
pub fn kill(&mut self) {
|
||||||
if let Some(child) = self.0.take() {
|
if let Some(child) = self.0.take() {
|
||||||
let _ = child.kill();
|
let _ = child.kill();
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|||||||
@@ -153,7 +153,7 @@ pub(super) async fn init_verge_config() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(super) async fn init_service_manager() {
|
pub(super) async fn init_service_manager() {
|
||||||
clash_verge_service_ipc::set_config(ServiceManager::config()).await;
|
clash_verge_service_ipc::set_config(Some(ServiceManager::config())).await;
|
||||||
if !is_service_ipc_path_exists() {
|
if !is_service_ipc_path_exists() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -174,7 +174,7 @@ pub(super) async fn init_system_proxy() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn init_system_proxy_guard() {
|
pub(super) fn init_system_proxy_guard() {
|
||||||
logging_error!(Type::Setup, sysopt::Sysopt::global().init_guard_sysproxy());
|
sysopt::Sysopt::global().init_guard_sysproxy();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) async fn refresh_tray_menu() {
|
pub(super) async fn refresh_tray_menu() {
|
||||||
|
|||||||
Reference in New Issue
Block a user