From ef3b10fa8f76bcc244c238680a8ea160a998c04b Mon Sep 17 00:00:00 2001 From: GyDi Date: Tue, 14 Dec 2021 00:40:41 +0800 Subject: [PATCH] feat: change the naming strategy --- src-tauri/resources/profiles_tmp.yaml | 3 + src-tauri/src/config/mod.rs | 6 +- src-tauri/src/config/operate.rs | 68 ++++++++++++++ .../src/config/{verge.rs => profiles.rs} | 10 +-- src-tauri/src/init.rs | 88 +++++++------------ src-tauri/src/profiles.rs | 38 ++++---- 6 files changed, 132 insertions(+), 81 deletions(-) create mode 100644 src-tauri/resources/profiles_tmp.yaml create mode 100644 src-tauri/src/config/operate.rs rename src-tauri/src/config/{verge.rs => profiles.rs} (83%) diff --git a/src-tauri/resources/profiles_tmp.yaml b/src-tauri/resources/profiles_tmp.yaml new file mode 100644 index 0000000..4787f0e --- /dev/null +++ b/src-tauri/resources/profiles_tmp.yaml @@ -0,0 +1,3 @@ +# Profiles Config for Clash Verge + +current: 0 diff --git a/src-tauri/src/config/mod.rs b/src-tauri/src/config/mod.rs index 27a78e3..d1f3817 100644 --- a/src-tauri/src/config/mod.rs +++ b/src-tauri/src/config/mod.rs @@ -1 +1,5 @@ -pub mod verge; +mod operate; +mod profiles; + +pub use self::operate::*; +pub use self::profiles::*; diff --git a/src-tauri/src/config/operate.rs b/src-tauri/src/config/operate.rs new file mode 100644 index 0000000..9c83360 --- /dev/null +++ b/src-tauri/src/config/operate.rs @@ -0,0 +1,68 @@ +use serde::{de::DeserializeOwned, Serialize}; +use std::{fs, path::PathBuf}; + +use super::profiles::ProfilesConfig; +use crate::init::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(); + serde_yaml::from_str::(&yaml_str).unwrap() +} + +/// - save the data to the file +/// - can set `prefix` string to add some comments +pub fn save_yaml( + path: PathBuf, + data: &T, + prefix: Option<&str>, +) -> Result<(), String> { + if let Ok(data_str) = serde_yaml::to_string(data) { + let yaml_str = if prefix.is_some() { + prefix.unwrap().to_string() + &data_str + } else { + data_str + }; + + if fs::write(path.clone(), yaml_str.as_bytes()).is_err() { + Err(format!("can not save file `{:?}`", path)) + } else { + Ok(()) + } + } else { + Err(String::from("can not convert the data to yaml")) + } +} + +// /// Get Clash Core Config +// pub fn read_clash() -> Mapping { +// read_yaml::(app_home_dir().join("config.yaml")) +// } + +// /// Get Verge App Config +// pub fn read_verge() -> ProfilesConfig { +// read_from_yaml::(app_home_dir().join("verge.yaml")) +// } + +// /// Save Verge App Config +// pub fn save_verge(verge_config: &ProfilesConfig) { +// let yaml_path = app_home_dir().join("verge.yaml"); +// let yaml_str = serde_yaml::to_string(&verge_config).unwrap(); +// let yaml_str = String::from("# Config File for Clash Verge\n\n") + &yaml_str; +// fs::write(yaml_path, yaml_str.as_bytes()).unwrap(); +// } + +/// Get Profiles Config +pub fn read_profiles() -> ProfilesConfig { + read_yaml::(app_home_dir().join("profiles.yaml")) +} + +/// Save Verge App Config +pub fn save_profiles(profiles: &ProfilesConfig) { + save_yaml( + app_home_dir().join("profiles.yaml"), + profiles, + Some("# Profiles Config for Clash Verge\n\n"), + ) + .unwrap(); +} diff --git a/src-tauri/src/config/verge.rs b/src-tauri/src/config/profiles.rs similarity index 83% rename from src-tauri/src/config/verge.rs rename to src-tauri/src/config/profiles.rs index 726bacb..1b9f718 100644 --- a/src-tauri/src/config/verge.rs +++ b/src-tauri/src/config/profiles.rs @@ -2,16 +2,16 @@ use serde::{Deserialize, Serialize}; /// Define the verge.yaml's schema #[derive(Default, Debug, Clone, Deserialize, Serialize)] -pub struct VergeConfig { +pub struct ProfilesConfig { /// current profile's name pub current: Option, /// profile list - pub profiles: Option>, + pub items: Option>, } #[derive(Default, Debug, Clone, Deserialize, Serialize)] -pub struct ProfileData { +pub struct ProfileItem { /// profile name pub name: Option, /// profile file @@ -23,7 +23,7 @@ pub struct ProfileData { /// selected infomation pub selected: Option>, /// user info - pub user_info: Option, + pub extra: Option, } #[derive(Default, Debug, Clone, Deserialize, Serialize)] @@ -33,7 +33,7 @@ pub struct ProfileSelected { } #[derive(Default, Debug, Clone, Copy, Deserialize, Serialize)] -pub struct ProfileUserInfo { +pub struct ProfileExtra { pub upload: u64, pub download: u64, pub total: u64, diff --git a/src-tauri/src/init.rs b/src-tauri/src/init.rs index 374a170..f6abd9c 100644 --- a/src-tauri/src/init.rs +++ b/src-tauri/src/init.rs @@ -5,7 +5,6 @@ use log4rs::append::console::ConsoleAppender; use log4rs::append::file::FileAppender; use log4rs::config::{Appender, Config, Root}; use log4rs::encode::pattern::PatternEncoder; -use serde_yaml::Mapping; use std::fs; use std::io::Write; use std::path::{Path, PathBuf}; @@ -13,8 +12,6 @@ use std::time::{SystemTime, UNIX_EPOCH}; use tauri::api::path::{home_dir, resource_dir}; use tauri::PackageInfo; -use crate::config::verge::VergeConfig; - /// get the verge app home dir pub fn app_home_dir() -> PathBuf { home_dir() @@ -53,43 +50,45 @@ fn init_log(log_dir: &PathBuf) { log4rs::init_config(config).unwrap(); } -/// Initialize the clash config file -fn init_clash_config(app_dir: &PathBuf, res_dir: &PathBuf) { - let yaml_path = app_dir.join("config.yaml"); - let yaml_tmpl = res_dir.join("config_tmp.yaml"); - - if !yaml_path.exists() { - if yaml_tmpl.exists() { - fs::copy(yaml_tmpl, yaml_path).unwrap(); - } else { - let content = "mixed-port: 7890\nallow-lan: false\n".as_bytes(); - fs::File::create(yaml_path).unwrap().write(content).unwrap(); - } - } - +/// Initialize all the files from resources +fn init_config_file(app_dir: &PathBuf, res_dir: &PathBuf) { + // target path + let clash_path = app_dir.join("config.yaml"); + let verge_path = app_dir.join("verge.yaml"); + let profile_path = app_dir.join("profiles.yaml"); let mmdb_path = app_dir.join("Country.mmdb"); + + // template path + let clash_tmpl = res_dir.join("config_tmp.yaml"); + let verge_tmpl = res_dir.join("verge_tmp.yaml"); + let profiles_tmpl = res_dir.join("profiles_tmp.yaml"); let mmdb_tmpl = res_dir.join("Country.mmdb"); + if !clash_path.exists() { + if clash_tmpl.exists() { + fs::copy(clash_tmpl, clash_path).unwrap(); + } else { + // make sure that the config.yaml not null + let content = "mixed-port: 7890\nallow-lan: false\n".as_bytes(); + fs::File::create(clash_path) + .unwrap() + .write(content) + .unwrap(); + } + } + + // only copy it + if !verge_path.exists() && verge_tmpl.exists() { + fs::copy(verge_tmpl, verge_path).unwrap(); + } + if !profile_path.exists() && profiles_tmpl.exists() { + fs::copy(profiles_tmpl, profile_path).unwrap(); + } if !mmdb_path.exists() && mmdb_tmpl.exists() { fs::copy(mmdb_tmpl, mmdb_path).unwrap(); } } -/// Initialize the verge app config file -fn init_verge_config(app_dir: &PathBuf, res_dir: &PathBuf) { - let yaml_path = app_dir.join("verge.yaml"); - let yaml_tmpl = res_dir.join("verge_tmp.yaml"); - - if !yaml_path.exists() { - if yaml_tmpl.exists() { - fs::copy(yaml_tmpl, yaml_path).unwrap(); - } else { - let content = "".as_bytes(); - fs::File::create(yaml_path).unwrap().write(content).unwrap(); - } - } -} - /// initialize app pub fn init_app(package_info: &PackageInfo) { // create app dir @@ -110,28 +109,5 @@ pub fn init_app(package_info: &PackageInfo) { } init_log(&log_dir); - init_clash_config(&app_dir, &res_dir); - init_verge_config(&app_dir, &res_dir); -} - -/// Get the user config of clash core -pub fn read_clash_config() -> Mapping { - let yaml_path = app_home_dir().join("config.yaml"); - let yaml_str = fs::read_to_string(yaml_path).unwrap(); - serde_yaml::from_str::(&yaml_str).unwrap() -} - -/// Get the user config of verge -pub fn read_verge_config() -> VergeConfig { - let yaml_path = app_home_dir().join("verge.yaml"); - let yaml_str = fs::read_to_string(yaml_path).unwrap(); - serde_yaml::from_str::(&yaml_str).unwrap() -} - -/// Save the user config of verge -pub fn save_verge_config(verge_config: &VergeConfig) { - let yaml_path = app_home_dir().join("verge.yaml"); - let yaml_str = serde_yaml::to_string(&verge_config).unwrap(); - let yaml_str = String::from("# Config File for Clash Verge\n\n") + &yaml_str; - fs::write(yaml_path, yaml_str.as_bytes()).unwrap(); + init_config_file(&app_dir, &res_dir); } diff --git a/src-tauri/src/profiles.rs b/src-tauri/src/profiles.rs index 4dfab5f..c4f24e4 100644 --- a/src-tauri/src/profiles.rs +++ b/src-tauri/src/profiles.rs @@ -1,7 +1,7 @@ extern crate reqwest; -use crate::config::verge::{ProfileData, ProfileUserInfo}; -use crate::init::{app_home_dir, read_verge_config, save_verge_config}; +use crate::config::{read_profiles, save_profiles, ProfileExtra, ProfileItem}; +use crate::init::app_home_dir; use std::default::Default; use std::fs::File; use std::io::Write; @@ -20,24 +20,24 @@ pub async fn import_profile(profile_url: &str) -> Result<(), reqwest::Error> { .unwrap(); let value: Vec<&str> = value.clone().split(';').collect(); - let mut user_info = ProfileUserInfo::default(); - + // parse the Subscription Userinfo + let mut extra = ProfileExtra::default(); for each in value.iter() { let each = each.clone().trim(); if let Some(val) = each.strip_prefix("upload=") { - user_info.upload = val.parse().unwrap_or(0u64); + extra.upload = val.parse().unwrap_or(0u64); continue; } if let Some(val) = each.strip_prefix("download=") { - user_info.download = val.parse().unwrap_or(0u64); + extra.download = val.parse().unwrap_or(0u64); continue; } if let Some(val) = each.strip_prefix("total=") { - user_info.total = val.parse().unwrap_or(0u64); + extra.total = val.parse().unwrap_or(0u64); continue; } if let Some(val) = each.strip_prefix("expire=") { - user_info.expire = val.parse().unwrap_or(0u64); + extra.expire = val.parse().unwrap_or(0u64); continue; } } @@ -56,35 +56,35 @@ pub async fn import_profile(profile_url: &str) -> Result<(), reqwest::Error> { .write(file_data.as_bytes()) .unwrap(); - let mut verge = read_verge_config(); - - let mut profiles = if verge.profiles.is_some() { - verge.profiles.unwrap() + // update profiles.yaml + let mut profiles = read_profiles(); + let mut items = if profiles.items.is_some() { + profiles.items.unwrap() } else { vec![] }; - let profile = ProfileData { + let profile = ProfileItem { name: Some(file_name.clone()), file: Some(file_name.clone()), mode: Some(String::from("rule")), url: Some(String::from(profile_url)), selected: Some(vec![]), - user_info: Some(user_info), + extra: Some(extra), }; - let target_index = profiles + let target_index = items .iter() .position(|x| x.name.is_some() && x.name.as_ref().unwrap().as_str() == file_name.as_str()); if target_index.is_none() { - profiles.push(profile) + items.push(profile) } else { - profiles[target_index.unwrap()] = profile; + items[target_index.unwrap()] = profile; } - verge.profiles = Some(profiles); - save_verge_config(&verge); + profiles.items = Some(items); + save_profiles(&profiles); Ok(()) }