refactor: simplify function return types and remove unnecessary wraps

This commit is contained in:
Tunglies
2025-11-15 08:56:58 +08:00
parent 2b0ba4dc95
commit a6cb903fe6
16 changed files with 37 additions and 55 deletions

View File

@@ -84,8 +84,8 @@ pub async fn restart_app() -> CmdResult<()> {
/// 获取便携版标识
#[tauri::command]
pub fn get_portable_flag() -> CmdResult<bool> {
Ok(*dirs::PORTABLE_FLAG.get().unwrap_or(&false))
pub fn get_portable_flag() -> bool {
*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已准备就绪
#[tauri::command]
pub fn notify_ui_ready() -> CmdResult<()> {
pub fn notify_ui_ready() {
logging!(info, Type::Cmd, "前端UI已准备就绪");
ui::mark_ui_ready();
Ok(())
}
/// UI加载阶段
#[tauri::command]
pub fn update_ui_stage(stage: UiReadyStage) -> CmdResult<()> {
pub fn update_ui_stage(stage: UiReadyStage) {
logging!(info, Type::Cmd, "UI加载阶段更新: {:?}", &stage);
ui::update_ui_ready_stage(stage);
Ok(())
}

View File

@@ -3,6 +3,7 @@ use crate::cmd::StringifyErr as _;
use crate::core::{EventDrivenProxyManager, async_proxy_query::AsyncProxyQuery};
use crate::process::AsyncHandler;
use crate::{logging, utils::logging::Type};
use gethostname::gethostname;
use network_interface::NetworkInterface;
use serde_yaml_ng::Mapping;
@@ -61,11 +62,9 @@ pub async fn get_auto_proxy() -> CmdResult<Mapping> {
/// 获取系统主机名
#[tauri::command]
pub fn get_system_hostname() -> CmdResult<String> {
use gethostname::gethostname;
pub fn get_system_hostname() -> String {
// 获取系统主机名处理可能的非UTF-8字符
let hostname = match gethostname().into_string() {
match gethostname().into_string() {
Ok(name) => name,
Err(os_string) => {
// 对于包含非UTF-8的主机名使用调试格式化
@@ -73,9 +72,7 @@ pub fn get_system_hostname() -> CmdResult<String> {
// 去掉可能存在的引号
fallback.trim_matches('"').to_string()
}
};
Ok(hostname)
}
}
/// 获取网络接口列表

View File

@@ -53,12 +53,12 @@ pub async fn get_running_mode() -> Result<Arc<RunningMode>, String> {
/// 获取应用的运行时间(毫秒)
#[tauri::command]
pub fn get_app_uptime() -> CmdResult<u128> {
Ok(APP_START_TIME.elapsed().as_millis())
pub fn get_app_uptime() -> u128 {
APP_START_TIME.elapsed().as_millis()
}
/// 检查应用是否以管理员身份运行
#[tauri::command]
pub fn is_admin() -> CmdResult<bool> {
Ok(*APPS_RUN_AS_ADMIN)
pub fn is_admin() -> bool {
*APPS_RUN_AS_ADMIN
}

View File

@@ -17,6 +17,7 @@ mod platform {
mod platform {
use super::CmdResult;
#[allow(clippy::unnecessary_wraps)]
pub const fn invoke_uwp_tool() -> CmdResult {
Ok(())
}

View File

@@ -57,9 +57,7 @@ impl Config {
Self::ensure_default_profile_items().await?;
// init Tun mode
if !cmd::system::is_admin().unwrap_or_default()
&& service::is_service_available().await.is_err()
{
if !cmd::system::is_admin() && service::is_service_available().await.is_err() {
let verge = Self::verge().await;
verge.edit_draft(|d| {
d.enable_tun_mode = Some(false);

View File

@@ -581,6 +581,7 @@ impl PrfItem {
}
// 向前兼容,默认为订阅启用自动更新
#[allow(clippy::unnecessary_wraps)]
const fn default_allow_auto_update() -> Option<bool> {
Some(true)
}

View File

@@ -27,7 +27,10 @@ impl CoreManager {
match *self.get_running_mode() {
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(()),
}
}

View File

@@ -96,7 +96,7 @@ impl CoreManager {
Ok(())
}
pub(super) fn stop_core_by_sidecar(&self) -> Result<()> {
pub(super) fn stop_core_by_sidecar(&self) {
logging!(info, Type::Core, "Stopping sidecar");
defer! {
self.set_running_mode(RunningMode::NotRunning);
@@ -106,7 +106,6 @@ impl CoreManager {
drop(child);
logging!(trace, Type::Core, "Sidecar stopped (PID: {:?})", pid);
}
Ok(())
}
pub(super) async fn start_core_by_service(&self) -> Result<()> {

View File

@@ -456,12 +456,12 @@ impl ServiceManager {
Self(ServiceStatus::Unavailable("Need Checks".into()))
}
pub const fn config() -> Option<clash_verge_service_ipc::IpcConfig> {
Some(clash_verge_service_ipc::IpcConfig {
pub const fn config() -> clash_verge_service_ipc::IpcConfig {
clash_verge_service_ipc::IpcConfig {
default_timeout: Duration::from_millis(30),
retry_delay: Duration::from_millis(250),
max_retries: 6,
})
}
}
pub async fn init(&mut self) -> Result<()> {

View File

@@ -100,13 +100,12 @@ impl Sysopt {
self.initialed.load(Ordering::SeqCst)
}
pub fn init_guard_sysproxy(&self) -> Result<()> {
pub fn init_guard_sysproxy(&self) {
// 使用事件驱动代理管理器
let proxy_manager = EventDrivenProxyManager::global();
proxy_manager.notify_app_started();
logging!(info, Type::Core, "已启用事件驱动代理守卫");
Ok(())
}
/// init the sysproxy

View File

@@ -301,8 +301,8 @@ impl Tray {
let verge = Config::verge().await.latest_arc();
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_available = cmd::system::is_admin().unwrap_or_default()
|| service::is_service_available().await.is_ok();
let tun_mode_available =
cmd::system::is_admin() || service::is_service_available().await.is_ok();
let mode = {
Config::clash()
.await
@@ -640,7 +640,7 @@ fn create_subcreate_proxy_menu_item(
current_profile_selected: &[PrfSelected],
proxy_group_order_map: Option<HashMap<String, usize>>,
proxy_nodes_data: Result<Proxies>,
) -> Result<Vec<Submenu<Wry>>> {
) -> Vec<Submenu<Wry>> {
let proxy_submenus: Vec<Submenu<Wry>> = {
let mut submenus: Vec<(String, usize, Submenu<Wry>)> = Vec::new();
@@ -767,7 +767,7 @@ fn create_subcreate_proxy_menu_item(
.map(|(_, _, submenu)| submenu)
.collect()
};
Ok(proxy_submenus)
proxy_submenus
}
fn create_proxy_menu_item(
@@ -955,7 +955,7 @@ async fn create_tray_menu(
&current_profile_selected,
proxy_group_order_map,
proxy_nodes_data.map_err(anyhow::Error::from),
)?;
);
let (proxies_menu, inline_proxy_items) = create_proxy_menu_item(
app_handle,

View File

@@ -82,7 +82,7 @@ mod app_init {
}
/// 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)))]
{
logging!(info, Type::Setup, "注册深层链接...");
@@ -99,8 +99,6 @@ mod app_init {
}
});
});
Ok(())
}
/// Setup autostart plugin
@@ -246,9 +244,7 @@ pub fn run() {
logging!(error, Type::Setup, "Failed to setup autostart: {}", e);
}
if let Err(e) = app_init::setup_deep_links(app) {
logging!(error, Type::Setup, "Failed to setup deep links: {}", e);
}
app_init::setup_deep_links(app);
if let Err(e) = app_init::setup_window_state(app) {
logging!(error, Type::Setup, "Failed to setup window state: {}", e);

View File

@@ -44,7 +44,7 @@ impl PlatformSpecification {
// 使用默认值避免在同步上下文中执行异步操作
let running_mode = "NotRunning".to_string();
let is_admin = system::is_admin().unwrap_or_default();
let is_admin = system::is_admin();
Self {
system_name,

View File

@@ -1,22 +1,12 @@
use anyhow::Result;
use tauri_plugin_shell::process::CommandChild;
use crate::{logging, utils::logging::Type};
#[derive(Debug)]
pub struct CommandChildGuard(Option<CommandChild>);
impl Drop for CommandChildGuard {
#[inline]
fn drop(&mut self) {
if let Err(err) = self.kill() {
logging!(
error,
Type::Service,
"Failed to kill child process: {}",
err
);
}
self.kill();
}
}
@@ -27,11 +17,10 @@ impl CommandChildGuard {
}
#[inline]
pub fn kill(&mut self) -> Result<()> {
pub fn kill(&mut self) {
if let Some(child) = self.0.take() {
let _ = child.kill();
}
Ok(())
}
#[inline]

View File

@@ -153,7 +153,7 @@ pub(super) async fn init_verge_config() {
}
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() {
return;
}
@@ -174,7 +174,7 @@ pub(super) async fn init_system_proxy() {
}
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() {