mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 17:15:38 +08:00
feat: change the naming strategy
This commit is contained in:
@@ -1 +1,5 @@
|
||||
pub mod verge;
|
||||
mod operate;
|
||||
mod profiles;
|
||||
|
||||
pub use self::operate::*;
|
||||
pub use self::profiles::*;
|
||||
|
||||
68
src-tauri/src/config/operate.rs
Normal file
68
src-tauri/src/config/operate.rs
Normal file
@@ -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<T: DeserializeOwned>(path: PathBuf) -> T {
|
||||
let yaml_str = fs::read_to_string(path).unwrap();
|
||||
serde_yaml::from_str::<T>(&yaml_str).unwrap()
|
||||
}
|
||||
|
||||
/// - save the data to the file
|
||||
/// - can set `prefix` string to add some comments
|
||||
pub fn save_yaml<T: Serialize>(
|
||||
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::<Mapping>(app_home_dir().join("config.yaml"))
|
||||
// }
|
||||
|
||||
// /// Get Verge App Config
|
||||
// pub fn read_verge() -> ProfilesConfig {
|
||||
// read_from_yaml::<ProfilesConfig>(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::<ProfilesConfig>(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();
|
||||
}
|
||||
@@ -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<u32>,
|
||||
|
||||
/// profile list
|
||||
pub profiles: Option<Vec<ProfileData>>,
|
||||
pub items: Option<Vec<ProfileItem>>,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct ProfileData {
|
||||
pub struct ProfileItem {
|
||||
/// profile name
|
||||
pub name: Option<String>,
|
||||
/// profile file
|
||||
@@ -23,7 +23,7 @@ pub struct ProfileData {
|
||||
/// selected infomation
|
||||
pub selected: Option<Vec<ProfileSelected>>,
|
||||
/// user info
|
||||
pub user_info: Option<ProfileUserInfo>,
|
||||
pub extra: Option<ProfileExtra>,
|
||||
}
|
||||
|
||||
#[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,
|
||||
Reference in New Issue
Block a user