chore: update & fmt & clippy

This commit is contained in:
MystiPanda
2024-06-12 10:00:22 +08:00
parent e70249cb2e
commit c698b24e01
18 changed files with 100 additions and 127 deletions

View File

@@ -327,7 +327,7 @@ pub fn copy_icon_file(path: String, name: String) -> CmdResult<String> {
Err(err) => Err(err.to_string()),
}
} else {
return Err("file not found".to_string());
Err("file not found".to_string())
}
}

View File

@@ -1,4 +1,5 @@
mod clash;
#[allow(clippy::module_inception)]
mod config;
mod draft;
mod prfitem;

View File

@@ -103,7 +103,9 @@ impl PrfOption {
a.user_agent = b.user_agent.or(a.user_agent);
a.with_proxy = b.with_proxy.or(a.with_proxy);
a.self_proxy = b.self_proxy.or(a.self_proxy);
a.danger_accept_invalid_certs = b.danger_accept_invalid_certs.or(a.danger_accept_invalid_certs);
a.danger_accept_invalid_certs = b
.danger_accept_invalid_certs
.or(a.danger_accept_invalid_certs);
a.update_interval = b.update_interval.or(a.update_interval);
Some(a)
}
@@ -182,7 +184,8 @@ impl PrfItem {
let opt_ref = option.as_ref();
let with_proxy = opt_ref.map_or(false, |o| o.with_proxy.unwrap_or(false));
let self_proxy = opt_ref.map_or(false, |o| o.self_proxy.unwrap_or(false));
let accept_invalid_certs = opt_ref.map_or(false, |o| o.danger_accept_invalid_certs.unwrap_or(false));
let accept_invalid_certs =
opt_ref.map_or(false, |o| o.danger_accept_invalid_certs.unwrap_or(false));
let user_agent = opt_ref.and_then(|o| o.user_agent.clone());
let update_interval = opt_ref.and_then(|o| o.update_interval);
@@ -300,7 +303,7 @@ impl PrfItem {
Some(value) => {
let str_value = value.to_str().unwrap_or("");
Some(str_value.to_string())
},
}
None => None,
};

View File

@@ -116,12 +116,12 @@ impl CoreManager {
// 服务模式启动失败就直接运行sidecar
log::debug!(target: "app", "try to run core in service mode");
match (|| async {
let res = async {
service::check_service().await?;
service::run_core_by_service(&config_path).await
})()
.await
{
}
.await;
match res {
Ok(_) => return Ok(()),
Err(err) => {
// 修改这个值免得stop出错

View File

@@ -43,7 +43,7 @@ impl Hotkey {
}
}
}
*self.current.lock() = hotkeys.clone();
self.current.lock().clone_from(hotkeys);
}
Ok(())
@@ -142,7 +142,7 @@ impl Hotkey {
});
new_map.iter().for_each(|(&key, &func)| {
if old_map.get(key).is_none() {
if !old_map.contains_key(key) {
add_list.push((key, func));
}
});

View File

@@ -22,7 +22,8 @@ pub fn grant_permission(core: String) -> anyhow::Result<()> {
#[cfg(target_os = "linux")]
let output = {
let path = path.replace(' ', "\\ "); // 避免路径中有空格
let shell = format!("setcap cap_net_bind_service,cap_net_admin,cap_dac_override=+ep {path}");
let shell =
format!("setcap cap_net_bind_service,cap_net_admin,cap_dac_override=+ep {path}");
let sudo = match Command::new("which").arg("pkexec").output() {
Ok(output) => {

View File

@@ -1,13 +1,14 @@
pub mod clash_api;
#[allow(clippy::module_inception)]
mod core;
pub mod handle;
pub mod hotkey;
pub mod logger;
pub mod manager;
pub mod service;
pub mod sysopt;
pub mod timer;
pub mod tray;
pub mod service;
pub mod win_uwp;
pub use self::core::*;

View File

@@ -44,11 +44,12 @@ static DEFAULT_BYPASS: &str =
fn get_bypass() -> String {
let bypass = DEFAULT_BYPASS.to_string();
let custom_bypass = match {
let res = {
let verge = Config::verge();
let verge = verge.latest();
verge.system_proxy_bypass.clone()
} {
};
let custom_bypass = match res {
Some(bypass) => bypass,
None => "".to_string(),
};
@@ -65,7 +66,7 @@ fn get_bypass() -> String {
format!("{},{}", bypass, custom_bypass)
};
bypass.into()
bypass
}
impl Sysopt {
@@ -152,19 +153,18 @@ impl Sysopt {
verge.proxy_auto_config.unwrap_or(false),
)
};
if pac {
if cur_autoproxy.is_none() || old_autoproxy.is_none() {
drop(cur_autoproxy);
drop(old_autoproxy);
return self.init_sysproxy();
}
} else {
if cur_sysproxy.is_none() || old_sysproxy.is_none() {
drop(cur_sysproxy);
drop(old_sysproxy);
return self.init_sysproxy();
}
if pac && (cur_autoproxy.is_none() || old_autoproxy.is_none()) {
drop(cur_autoproxy);
drop(old_autoproxy);
return self.init_sysproxy();
}
if !pac && (cur_sysproxy.is_none() || old_sysproxy.is_none()) {
drop(cur_sysproxy);
drop(old_sysproxy);
return self.init_sysproxy();
}
let port = Config::verge()
.latest()
.verge_mixed_port

View File

@@ -88,13 +88,13 @@ impl ChainItem {
impl ChainSupport {
pub fn is_support(&self, core: Option<&String>) -> bool {
match core {
Some(core) => match (self, core.as_str()) {
(ChainSupport::All, _) => true,
(ChainSupport::Clash, "clash") => true,
(ChainSupport::ClashMeta, "clash-meta") => true,
(ChainSupport::ClashMetaAlpha, "clash-meta-alpha") => true,
_ => false,
},
Some(core) => matches!(
(self, core.as_str()),
(ChainSupport::All, _)
| (ChainSupport::Clash, "clash")
| (ChainSupport::ClashMeta, "clash-meta")
| (ChainSupport::ClashMetaAlpha, "clash-meta-alpha")
),
None => true,
}
}

View File

@@ -60,11 +60,7 @@ pub fn use_sort(config: Mapping) -> Mapping {
let supported_keys: HashSet<&str> = HANDLE_FIELDS.into_iter().chain(DEFAULT_FIELDS).collect();
let config_keys: HashSet<&str> = config
.keys()
.filter_map(|e| e.as_str())
.into_iter()
.collect();
let config_keys: HashSet<&str> = config.keys().filter_map(|e| e.as_str()).collect();
config_keys.difference(&supported_keys).for_each(|&key| {
let key = Value::from(key);

View File

@@ -12,7 +12,7 @@ const MERGE_FIELDS: [&str; 6] = [
fn deep_merge(a: &mut Value, b: &Value) {
match (a, b) {
(&mut Value::Mapping(ref mut a), &Value::Mapping(ref b)) => {
(&mut Value::Mapping(ref mut a), Value::Mapping(b)) => {
for (k, v) in b {
deep_merge(a.entry(k.clone()).or_insert(Value::Null), v);
}

View File

@@ -136,17 +136,15 @@ pub fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
.map(|(_, c)| c)
.for_each(|item| {
log::debug!(target: "app", "run builtin script {}", item.uid);
match item.data {
ChainType::Script(script) => match use_script(script, config.to_owned()) {
if let ChainType::Script(script) = item.data {
match use_script(script, config.to_owned()) {
Ok((res_config, _)) => {
config = res_config;
}
Err(err) => {
log::error!(target: "app", "builtin script error `{err}`");
}
},
_ => {}
}
}
});
}
@@ -155,7 +153,7 @@ pub fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
config = use_sort(config);
let mut exists_set = HashSet::new();
exists_set.extend(exists_keys.into_iter());
exists_set.extend(exists_keys);
exists_keys = exists_set.into_iter().collect();
(config, exists_keys, result_map)

View File

@@ -16,7 +16,7 @@ pub fn use_script(script: String, config: Mapping) -> Result<(Mapping, Vec<(Stri
2,
NativeFunction::from_closure(
move |_: &JsValue, args: &[JsValue], context: &mut Context| {
let level = args.get(0).unwrap().to_string(context)?;
let level = args.first().unwrap().to_string(context)?;
let level = level.to_std_string().unwrap();
let data = args.get(1).unwrap().to_string(context)?;
let data = data.to_std_string().unwrap();

View File

@@ -106,7 +106,7 @@ pub fn toggle_tun_mode() {
pub async fn patch_clash(patch: Mapping) -> Result<()> {
Config::clash().draft().patch_config(patch.clone());
match {
let res = {
let redir_port = patch.get("redir-port");
let tproxy_port = patch.get("tproxy-port");
let mixed_port = patch.get("mixed-port");
@@ -156,7 +156,8 @@ pub async fn patch_clash(patch: Mapping) -> Result<()> {
Config::runtime().latest().patch_config(patch);
<Result<()>>::Ok(())
} {
};
match res {
Ok(()) => {
Config::clash().apply();
Config::clash().data().save_config()?;
@@ -193,7 +194,7 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> {
let tproxy_enabled = patch.verge_tproxy_enabled;
let socks_enabled = patch.verge_socks_enabled;
let http_enabled = patch.verge_http_enabled;
match {
let res = {
let service_mode = patch.enable_service_mode;
if service_mode.is_some() {
@@ -255,7 +256,8 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> {
}
<Result<()>>::Ok(())
} {
};
match res {
Ok(()) => {
Config::verge().apply();
Config::verge().data().save_file()?;

View File

@@ -137,7 +137,7 @@ pub fn delete_log() -> Result<()> {
}
let service_log_dir = log_dir.join("service");
for file in fs::read_dir(&service_log_dir)?.flatten() {
for file in fs::read_dir(service_log_dir)?.flatten() {
let _ = process_file(file);
}
@@ -318,11 +318,11 @@ pub fn startup_script() -> Result<()> {
Some(dir) => {
let _ = Command::new(shell)
.current_dir(dir.to_path_buf())
.args(&[path])
.args([path])
.output()?;
}
None => {
let _ = Command::new(shell).args(&[path]).output()?;
let _ = Command::new(shell).args([path]).output()?;
}
}
}

View File

@@ -205,7 +205,6 @@ pub fn create_window(app_handle: &AppHandle) {
}
Err(_) => {
log::error!("failed to create window");
return;
}
}
}

View File

@@ -30,7 +30,7 @@ pub fn check_singleton() -> Result<()> {
let param = argvs[1].as_str();
if param.starts_with("clash:") {
reqwest::get(format!(
"http://127.0.0.1:{port}/commands/scheme?param={param}"
"http://127.0.0.1:{port}/commands/scheme?param={param}"
))
.await?
.text()