From 694a7760b7ae24959d6f96252d1f78f4f7025514 Mon Sep 17 00:00:00 2001 From: GyDi Date: Sat, 18 Dec 2021 02:29:54 +0800 Subject: [PATCH] feat: adjust code --- src-tauri/src/cmds/profile.rs | 11 +++- src-tauri/src/config/mod.rs | 2 - src-tauri/src/config/profiles.rs | 10 +-- src-tauri/src/utils/clash.rs | 8 ++- .../{config/operate.rs => utils/config.rs} | 12 ++-- src-tauri/src/utils/fetch.rs | 61 +++++++++---------- src-tauri/src/utils/mod.rs | 1 + 7 files changed, 52 insertions(+), 53 deletions(-) rename src-tauri/src/{config/operate.rs => utils/config.rs} (94%) diff --git a/src-tauri/src/cmds/profile.rs b/src-tauri/src/cmds/profile.rs index 26d672c..9278151 100644 --- a/src-tauri/src/cmds/profile.rs +++ b/src-tauri/src/cmds/profile.rs @@ -1,7 +1,12 @@ use crate::{ - config::{read_profiles, save_profiles, ProfileItem, ProfilesConfig}, + config::{ProfileItem, ProfilesConfig}, events::state::{ClashInfoState, ProfileLock}, - utils::{app_home_dir, clash::put_clash_profile, fetch::fetch_profile}, + utils::{ + app_home_dir, + clash::put_clash_profile, + config::{read_profiles, save_profiles}, + fetch::fetch_profile, + }, }; use std::fs::File; use std::io::Write; @@ -164,7 +169,7 @@ pub async fn put_profiles( return Err(format!("the index out of bound")); } - profiles.current = Some(current as u32); + profiles.current = Some(current); match save_profiles(&profiles) { Ok(_) => put_clash_profile(&clash_info).await, Err(err) => Err(err), diff --git a/src-tauri/src/config/mod.rs b/src-tauri/src/config/mod.rs index 372723a..1cf8f1e 100644 --- a/src-tauri/src/config/mod.rs +++ b/src-tauri/src/config/mod.rs @@ -1,9 +1,7 @@ mod clash; -mod operate; mod profiles; mod verge; pub use self::clash::*; -pub use self::operate::*; pub use self::profiles::*; pub use self::verge::*; diff --git a/src-tauri/src/config/profiles.rs b/src-tauri/src/config/profiles.rs index 65df3d1..0ad49cf 100644 --- a/src-tauri/src/config/profiles.rs +++ b/src-tauri/src/config/profiles.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; #[derive(Default, Debug, Clone, Deserialize, Serialize)] pub struct ProfilesConfig { /// current profile's name - pub current: Option, + pub current: Option, /// profile list pub items: Option>, @@ -34,10 +34,10 @@ pub struct ProfileSelected { #[derive(Default, Debug, Clone, Copy, Deserialize, Serialize)] pub struct ProfileExtra { - pub upload: u64, - pub download: u64, - pub total: u64, - pub expire: u64, + pub upload: usize, + pub download: usize, + pub total: usize, + pub expire: usize, } #[derive(Default, Debug, Clone, Deserialize, Serialize)] diff --git a/src-tauri/src/utils/clash.rs b/src-tauri/src/utils/clash.rs index e216bc6..cc3148e 100644 --- a/src-tauri/src/utils/clash.rs +++ b/src-tauri/src/utils/clash.rs @@ -1,9 +1,11 @@ extern crate log; use crate::{ - config::{read_clash_controller, read_profiles}, events::emit::{clash_start, ClashInfoPayload}, - utils::app_home_dir, + utils::{ + app_home_dir, + config::{read_clash_controller, read_profiles}, + }, }; use reqwest::header::HeaderMap; use std::{collections::HashMap, env::temp_dir, fs}; @@ -67,7 +69,7 @@ pub fn run_clash_bin(app_handle: &AppHandle) -> ClashInfoPayload { pub async fn put_clash_profile(payload: &ClashInfoPayload) -> Result<(), String> { let profile = { let profiles = read_profiles(); - let current = profiles.current.unwrap_or(0u32) as usize; + let current = profiles.current.unwrap_or(0) as usize; match profiles.items { Some(items) => { if items.len() == 0 { diff --git a/src-tauri/src/config/operate.rs b/src-tauri/src/utils/config.rs similarity index 94% rename from src-tauri/src/config/operate.rs rename to src-tauri/src/utils/config.rs index b060fd5..9569f2c 100644 --- a/src-tauri/src/config/operate.rs +++ b/src-tauri/src/utils/config.rs @@ -1,10 +1,11 @@ +use crate::{ + config::{ClashController, ProfilesConfig}, + utils::app_home_dir, +}; use serde::{de::DeserializeOwned, Serialize}; use serde_yaml::{Mapping, Value}; use std::{fs, path::PathBuf}; -use super::{profiles::ProfilesConfig, ClashController}; -use crate::utils::app_home_dir; - /// read data from yaml as struct T pub fn read_yaml(path: PathBuf) -> T { let yaml_str = fs::read_to_string(path).unwrap(); @@ -106,8 +107,3 @@ pub fn save_profiles(profiles: &ProfilesConfig) -> Result<(), String> { Some("# Profiles Config for Clash Verge\n\n"), ) } - -#[test] -fn test_print_config() { - println!("{:?}", read_clash_controller()); -} diff --git a/src-tauri/src/utils/fetch.rs b/src-tauri/src/utils/fetch.rs index fc58e23..043967e 100644 --- a/src-tauri/src/utils/fetch.rs +++ b/src-tauri/src/utils/fetch.rs @@ -1,15 +1,21 @@ use crate::config::{ProfileExtra, ProfileResponse}; -use std::time::{SystemTime, UNIX_EPOCH}; +use std::{ + str::FromStr, + time::{SystemTime, UNIX_EPOCH}, +}; /// parse the string -fn parse_string<'a>(target: &'a str, key: &'a str) -> Option<&'a str> { +fn parse_string(target: &str, key: &str) -> Option { match target.find(key) { Some(idx) => { let idx = idx + key.len(); let value = &target[idx..]; - match value.split(';').nth(0) { - Some(value) => Some(value.trim()), - None => Some(value.trim()), + match match value.split(';').nth(0) { + Some(value) => value.trim().parse(), + None => value.trim().parse(), + } { + Ok(r) => Some(r), + Err(_) => None, } } None => None, @@ -33,22 +39,10 @@ pub async fn fetch_profile(url: &str) -> Option { .unwrap(); ProfileExtra { - upload: parse_string(sub_info, "upload=") - .unwrap_or("0") - .parse() - .unwrap_or(0u64), - download: parse_string(sub_info, "download=") - .unwrap_or("0") - .parse() - .unwrap_or(0u64), - total: parse_string(sub_info, "total=") - .unwrap_or("0") - .parse() - .unwrap_or(0u64), - expire: parse_string(sub_info, "expire=") - .unwrap_or("0") - .parse() - .unwrap_or(0u64), + upload: parse_string(sub_info, "upload=").unwrap_or(0), + download: parse_string(sub_info, "download=").unwrap_or(0), + total: parse_string(sub_info, "total=").unwrap_or(0), + expire: parse_string(sub_info, "expire=").unwrap_or(0), } }; @@ -60,10 +54,10 @@ pub async fn fetch_profile(url: &str) -> Option { .as_secs(); let file = format!("{}.yaml", now); let name = header.get("Content-Disposition").unwrap().to_str().unwrap(); - let name = parse_string(name, "filename="); + let name = parse_string::(name, "filename="); match name { - Some(f) => (f.to_string(), file), + Some(f) => (f, file), None => (file.clone(), file), } }; @@ -87,14 +81,17 @@ fn test_parse_value() { let test_1 = "upload=111; download=2222; total=3333; expire=444"; let test_2 = "attachment; filename=Clash.yaml"; - assert_eq!(parse_string(test_1, "upload="), Some("111")); - assert_eq!(parse_string(test_1, "download="), Some("2222")); - assert_eq!(parse_string(test_1, "total="), Some("3333")); - assert_eq!(parse_string(test_1, "expire="), Some("444")); - assert_eq!(parse_string(test_2, "filename="), Some("Clash.yaml")); + assert_eq!(parse_string::(test_1, "upload=").unwrap(), 111); + assert_eq!(parse_string::(test_1, "download=").unwrap(), 2222); + assert_eq!(parse_string::(test_1, "total=").unwrap(), 3333); + assert_eq!(parse_string::(test_1, "expire=").unwrap(), 444); + assert_eq!( + parse_string::(test_2, "filename=").unwrap(), + format!("Clash.yaml") + ); - assert_eq!(parse_string(test_1, "aaa="), None); - assert_eq!(parse_string(test_1, "upload1="), None); - assert_eq!(parse_string(test_1, "expire1="), None); - assert_eq!(parse_string(test_2, "attachment="), None); + assert_eq!(parse_string::(test_1, "aaa="), None); + assert_eq!(parse_string::(test_1, "upload1="), None); + assert_eq!(parse_string::(test_1, "expire1="), None); + assert_eq!(parse_string::(test_2, "attachment="), None); } diff --git a/src-tauri/src/utils/mod.rs b/src-tauri/src/utils/mod.rs index 047d7c9..0430f37 100644 --- a/src-tauri/src/utils/mod.rs +++ b/src-tauri/src/utils/mod.rs @@ -2,6 +2,7 @@ mod dirs; pub use self::dirs::*; pub mod clash; +pub mod config; pub mod fetch; pub mod init; pub mod sysopt;