feat: adjust code
This commit is contained in:
parent
98378e6261
commit
694a7760b7
@ -1,7 +1,12 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
config::{read_profiles, save_profiles, ProfileItem, ProfilesConfig},
|
config::{ProfileItem, ProfilesConfig},
|
||||||
events::state::{ClashInfoState, ProfileLock},
|
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::fs::File;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
@ -164,7 +169,7 @@ pub async fn put_profiles(
|
|||||||
return Err(format!("the index out of bound"));
|
return Err(format!("the index out of bound"));
|
||||||
}
|
}
|
||||||
|
|
||||||
profiles.current = Some(current as u32);
|
profiles.current = Some(current);
|
||||||
match save_profiles(&profiles) {
|
match save_profiles(&profiles) {
|
||||||
Ok(_) => put_clash_profile(&clash_info).await,
|
Ok(_) => put_clash_profile(&clash_info).await,
|
||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
mod clash;
|
mod clash;
|
||||||
mod operate;
|
|
||||||
mod profiles;
|
mod profiles;
|
||||||
mod verge;
|
mod verge;
|
||||||
|
|
||||||
pub use self::clash::*;
|
pub use self::clash::*;
|
||||||
pub use self::operate::*;
|
|
||||||
pub use self::profiles::*;
|
pub use self::profiles::*;
|
||||||
pub use self::verge::*;
|
pub use self::verge::*;
|
||||||
|
@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct ProfilesConfig {
|
pub struct ProfilesConfig {
|
||||||
/// current profile's name
|
/// current profile's name
|
||||||
pub current: Option<u32>,
|
pub current: Option<usize>,
|
||||||
|
|
||||||
/// profile list
|
/// profile list
|
||||||
pub items: Option<Vec<ProfileItem>>,
|
pub items: Option<Vec<ProfileItem>>,
|
||||||
@ -34,10 +34,10 @@ pub struct ProfileSelected {
|
|||||||
|
|
||||||
#[derive(Default, Debug, Clone, Copy, Deserialize, Serialize)]
|
#[derive(Default, Debug, Clone, Copy, Deserialize, Serialize)]
|
||||||
pub struct ProfileExtra {
|
pub struct ProfileExtra {
|
||||||
pub upload: u64,
|
pub upload: usize,
|
||||||
pub download: u64,
|
pub download: usize,
|
||||||
pub total: u64,
|
pub total: usize,
|
||||||
pub expire: u64,
|
pub expire: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
extern crate log;
|
extern crate log;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::{read_clash_controller, read_profiles},
|
|
||||||
events::emit::{clash_start, ClashInfoPayload},
|
events::emit::{clash_start, ClashInfoPayload},
|
||||||
utils::app_home_dir,
|
utils::{
|
||||||
|
app_home_dir,
|
||||||
|
config::{read_clash_controller, read_profiles},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use reqwest::header::HeaderMap;
|
use reqwest::header::HeaderMap;
|
||||||
use std::{collections::HashMap, env::temp_dir, fs};
|
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> {
|
pub async fn put_clash_profile(payload: &ClashInfoPayload) -> Result<(), String> {
|
||||||
let profile = {
|
let profile = {
|
||||||
let profiles = read_profiles();
|
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 {
|
match profiles.items {
|
||||||
Some(items) => {
|
Some(items) => {
|
||||||
if items.len() == 0 {
|
if items.len() == 0 {
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
|
use crate::{
|
||||||
|
config::{ClashController, ProfilesConfig},
|
||||||
|
utils::app_home_dir,
|
||||||
|
};
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
use serde_yaml::{Mapping, Value};
|
use serde_yaml::{Mapping, Value};
|
||||||
use std::{fs, path::PathBuf};
|
use std::{fs, path::PathBuf};
|
||||||
|
|
||||||
use super::{profiles::ProfilesConfig, ClashController};
|
|
||||||
use crate::utils::app_home_dir;
|
|
||||||
|
|
||||||
/// read data from yaml as struct T
|
/// read data from yaml as struct T
|
||||||
pub fn read_yaml<T: DeserializeOwned>(path: PathBuf) -> T {
|
pub fn read_yaml<T: DeserializeOwned>(path: PathBuf) -> T {
|
||||||
let yaml_str = fs::read_to_string(path).unwrap();
|
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"),
|
Some("# Profiles Config for Clash Verge\n\n"),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_print_config() {
|
|
||||||
println!("{:?}", read_clash_controller());
|
|
||||||
}
|
|
@ -1,15 +1,21 @@
|
|||||||
use crate::config::{ProfileExtra, ProfileResponse};
|
use crate::config::{ProfileExtra, ProfileResponse};
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::{
|
||||||
|
str::FromStr,
|
||||||
|
time::{SystemTime, UNIX_EPOCH},
|
||||||
|
};
|
||||||
|
|
||||||
/// parse the string
|
/// parse the string
|
||||||
fn parse_string<'a>(target: &'a str, key: &'a str) -> Option<&'a str> {
|
fn parse_string<T: FromStr>(target: &str, key: &str) -> Option<T> {
|
||||||
match target.find(key) {
|
match target.find(key) {
|
||||||
Some(idx) => {
|
Some(idx) => {
|
||||||
let idx = idx + key.len();
|
let idx = idx + key.len();
|
||||||
let value = &target[idx..];
|
let value = &target[idx..];
|
||||||
match value.split(';').nth(0) {
|
match match value.split(';').nth(0) {
|
||||||
Some(value) => Some(value.trim()),
|
Some(value) => value.trim().parse(),
|
||||||
None => Some(value.trim()),
|
None => value.trim().parse(),
|
||||||
|
} {
|
||||||
|
Ok(r) => Some(r),
|
||||||
|
Err(_) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => None,
|
None => None,
|
||||||
@ -33,22 +39,10 @@ pub async fn fetch_profile(url: &str) -> Option<ProfileResponse> {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
ProfileExtra {
|
ProfileExtra {
|
||||||
upload: parse_string(sub_info, "upload=")
|
upload: parse_string(sub_info, "upload=").unwrap_or(0),
|
||||||
.unwrap_or("0")
|
download: parse_string(sub_info, "download=").unwrap_or(0),
|
||||||
.parse()
|
total: parse_string(sub_info, "total=").unwrap_or(0),
|
||||||
.unwrap_or(0u64),
|
expire: parse_string(sub_info, "expire=").unwrap_or(0),
|
||||||
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),
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -60,10 +54,10 @@ pub async fn fetch_profile(url: &str) -> Option<ProfileResponse> {
|
|||||||
.as_secs();
|
.as_secs();
|
||||||
let file = format!("{}.yaml", now);
|
let file = format!("{}.yaml", now);
|
||||||
let name = header.get("Content-Disposition").unwrap().to_str().unwrap();
|
let name = header.get("Content-Disposition").unwrap().to_str().unwrap();
|
||||||
let name = parse_string(name, "filename=");
|
let name = parse_string::<String>(name, "filename=");
|
||||||
|
|
||||||
match name {
|
match name {
|
||||||
Some(f) => (f.to_string(), file),
|
Some(f) => (f, file),
|
||||||
None => (file.clone(), 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_1 = "upload=111; download=2222; total=3333; expire=444";
|
||||||
let test_2 = "attachment; filename=Clash.yaml";
|
let test_2 = "attachment; filename=Clash.yaml";
|
||||||
|
|
||||||
assert_eq!(parse_string(test_1, "upload="), Some("111"));
|
assert_eq!(parse_string::<usize>(test_1, "upload=").unwrap(), 111);
|
||||||
assert_eq!(parse_string(test_1, "download="), Some("2222"));
|
assert_eq!(parse_string::<usize>(test_1, "download=").unwrap(), 2222);
|
||||||
assert_eq!(parse_string(test_1, "total="), Some("3333"));
|
assert_eq!(parse_string::<usize>(test_1, "total=").unwrap(), 3333);
|
||||||
assert_eq!(parse_string(test_1, "expire="), Some("444"));
|
assert_eq!(parse_string::<usize>(test_1, "expire=").unwrap(), 444);
|
||||||
assert_eq!(parse_string(test_2, "filename="), Some("Clash.yaml"));
|
assert_eq!(
|
||||||
|
parse_string::<String>(test_2, "filename=").unwrap(),
|
||||||
|
format!("Clash.yaml")
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(parse_string(test_1, "aaa="), None);
|
assert_eq!(parse_string::<usize>(test_1, "aaa="), None);
|
||||||
assert_eq!(parse_string(test_1, "upload1="), None);
|
assert_eq!(parse_string::<usize>(test_1, "upload1="), None);
|
||||||
assert_eq!(parse_string(test_1, "expire1="), None);
|
assert_eq!(parse_string::<usize>(test_1, "expire1="), None);
|
||||||
assert_eq!(parse_string(test_2, "attachment="), None);
|
assert_eq!(parse_string::<usize>(test_2, "attachment="), None);
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ mod dirs;
|
|||||||
pub use self::dirs::*;
|
pub use self::dirs::*;
|
||||||
|
|
||||||
pub mod clash;
|
pub mod clash;
|
||||||
|
pub mod config;
|
||||||
pub mod fetch;
|
pub mod fetch;
|
||||||
pub mod init;
|
pub mod init;
|
||||||
pub mod sysopt;
|
pub mod sysopt;
|
||||||
|
Loading…
Reference in New Issue
Block a user