perf: utilize smartstring for string handling (#5149)

* perf: utilize smartstring for string handling

- Updated various modules to replace standard String with smartstring::alias::String for improved performance and memory efficiency.
- Adjusted string manipulations and conversions throughout the codebase to ensure compatibility with the new smartstring type.
- Enhanced readability and maintainability by using `.into()` for conversions where applicable.
- Ensured that all instances of string handling in configuration, logging, and network management leverage the benefits of smartstring.

* fix: replace wrap_err with stringify_err for better error handling in UWP tool invocation

* refactor: update import path for StringifyErr and adjust string handling in sysopt

* fix: correct import path for CmdResult in UWP module

* fix: update argument type for execute_sysproxy_command to use std::string::String

* fix: add missing CmdResult import in UWP platform module

* fix: improve string handling and error messaging across multiple files

* style: format code for improved readability and consistency across multiple files

* fix: remove unused file
This commit is contained in:
Tunglies
2025-10-22 16:25:44 +08:00
committed by GitHub
parent fe96a7030a
commit a05ea64bcd
50 changed files with 361 additions and 272 deletions
+10 -9
View File
@@ -11,6 +11,7 @@ use anyhow::{Result, anyhow};
use chrono::Utc;
use reqwest_dav::list_cmd::ListFile;
use serde::Serialize;
use smartstring::alias::String;
use std::{fs, path::PathBuf};
#[derive(Debug, Serialize)]
@@ -82,7 +83,7 @@ pub async fn restore_webdav_backup(filename: String) -> Result<()> {
let backup_storage_path = app_home_dir()
.map_err(|e| anyhow::anyhow!("Failed to get app home dir: {e}"))?
.join(&filename);
.join(filename.as_str());
backup::WebDavClient::global()
.download(filename, backup_storage_path.clone())
.await
@@ -128,7 +129,7 @@ pub async fn create_local_backup() -> Result<()> {
})?;
let backup_dir = local_backup_dir()?;
let target_path = backup_dir.join(&file_name);
let target_path = backup_dir.join(file_name.as_str());
if let Err(err) = move_file(temp_file_path.clone(), target_path.clone()) {
logging!(
@@ -196,9 +197,9 @@ pub fn list_local_backup() -> Result<Vec<LocalBackupFile>> {
.map(|time| chrono::DateTime::<Utc>::from(time).to_rfc3339())
.unwrap_or_default();
backups.push(LocalBackupFile {
filename: file_name.to_string(),
path: path.to_string_lossy().to_string(),
last_modified,
filename: file_name.into(),
path: path.to_string_lossy().into(),
last_modified: last_modified.into(),
content_length: metadata.len(),
});
}
@@ -210,7 +211,7 @@ pub fn list_local_backup() -> Result<Vec<LocalBackupFile>> {
/// Delete local backup
pub async fn delete_local_backup(filename: String) -> Result<()> {
let backup_dir = local_backup_dir()?;
let target_path = backup_dir.join(&filename);
let target_path = backup_dir.join(filename.as_str());
if !target_path.exists() {
logging!(
warn,
@@ -227,7 +228,7 @@ pub async fn delete_local_backup(filename: String) -> Result<()> {
/// Restore local backup
pub async fn restore_local_backup(filename: String) -> Result<()> {
let backup_dir = local_backup_dir()?;
let target_path = backup_dir.join(&filename);
let target_path = backup_dir.join(filename.as_str());
if !target_path.exists() {
return Err(anyhow!("Backup file not found: {}", filename));
}
@@ -259,12 +260,12 @@ pub async fn restore_local_backup(filename: String) -> Result<()> {
/// Export local backup file to user selected destination
pub fn export_local_backup(filename: String, destination: String) -> Result<()> {
let backup_dir = local_backup_dir()?;
let source_path = backup_dir.join(&filename);
let source_path = backup_dir.join(filename.as_str());
if !source_path.exists() {
return Err(anyhow!("Backup file not found: {}", filename));
}
let dest_path = PathBuf::from(destination);
let dest_path = PathBuf::from(destination.as_str());
if let Some(parent) = dest_path.parent() {
fs::create_dir_all(parent)?;
}
+3 -2
View File
@@ -6,6 +6,7 @@ use crate::{
utils::{self, logging::Type, resolve},
};
use serde_yaml_ng::{Mapping, Value};
use smartstring::alias::String;
/// Restart the Clash core
pub async fn restart_clash_core() {
@@ -58,7 +59,7 @@ fn after_change_clash_mode() {
/// Change Clash mode (rule/global/direct/script)
pub async fn change_clash_mode(mode: String) {
let mut mapping = Mapping::new();
mapping.insert(Value::from("mode"), mode.clone().into());
mapping.insert(Value::from("mode"), Value::from(mode.as_str()));
// Convert YAML mapping to JSON Value
let json_value = serde_json::json!({
"mode": mode
@@ -112,7 +113,7 @@ pub async fn test_delay(url: String) -> anyhow::Result<u32> {
ProxyType::None
};
let user_agent = Some("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0".to_string());
let user_agent = Some("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0".into());
let start = Instant::now();
+1
View File
@@ -6,6 +6,7 @@ use crate::{
utils::logging::Type,
};
use anyhow::{Result, bail};
use smartstring::alias::String;
/// Toggle proxy profile
pub async fn toggle_proxy_profile(profile_index: String) {
+1 -1
View File
@@ -66,7 +66,7 @@ pub async fn toggle_tun_mode(not_save_file: Option<bool>) {
pub async fn copy_clash_env() {
// 从环境变量获取IP地址,如果没有则从配置中获取 proxy_host,默认为 127.0.0.1
let clash_verge_rev_ip = match env::var("CLASH_VERGE_REV_IP") {
Ok(ip) => ip,
Ok(ip) => ip.into(),
Err(_) => Config::verge()
.await
.latest_ref()