mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 00:35:38 +08:00
refactor: impl structs methods
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
use crate::{
|
||||
config::{ClashController, VergeConfig},
|
||||
utils::app_home_dir,
|
||||
};
|
||||
use crate::utils::dirs;
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
use serde_yaml::{Mapping, Value};
|
||||
use serde_yaml::Mapping;
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
/// read data from yaml as struct T
|
||||
@@ -19,100 +16,33 @@ pub fn save_yaml<T: Serialize>(
|
||||
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
|
||||
};
|
||||
match serde_yaml::to_string(data) {
|
||||
Ok(data_str) => {
|
||||
let yaml_str = match prefix {
|
||||
Some(prefix) => format!("{}{}", prefix, data_str),
|
||||
None => data_str,
|
||||
};
|
||||
|
||||
if fs::write(path.clone(), yaml_str.as_bytes()).is_err() {
|
||||
Err(format!("can not save file `{:?}`", path))
|
||||
} else {
|
||||
Ok(())
|
||||
let path_str = path.as_os_str().to_string_lossy().to_string();
|
||||
match fs::write(path, yaml_str.as_bytes()) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(format!("can not save file `{}`", path_str)),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Err(String::from("can not convert the data to yaml"))
|
||||
Err(_) => Err("can not convert the data to yaml".into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get Clash Core Config `config.yaml`
|
||||
pub fn read_clash() -> Mapping {
|
||||
read_yaml::<Mapping>(app_home_dir().join("config.yaml"))
|
||||
read_yaml::<Mapping>(dirs::app_home_dir().join("config.yaml"))
|
||||
}
|
||||
|
||||
/// Save the clash core Config `config.yaml`
|
||||
pub fn save_clash(config: &Mapping) -> Result<(), String> {
|
||||
save_yaml(
|
||||
app_home_dir().join("config.yaml"),
|
||||
dirs::app_home_dir().join("config.yaml"),
|
||||
config,
|
||||
Some("# Default Config For Clash Core\n\n"),
|
||||
)
|
||||
}
|
||||
|
||||
/// Get infomation of the clash's `external-controller` and `secret`
|
||||
pub fn read_clash_controller() -> ClashController {
|
||||
let config = read_clash();
|
||||
|
||||
let key_port_1 = Value::String("port".to_string());
|
||||
let key_port_2 = Value::String("mixed-port".to_string());
|
||||
let key_server = Value::String("external-controller".to_string());
|
||||
let key_secret = Value::String("secret".to_string());
|
||||
|
||||
let port = match config.get(&key_port_1) {
|
||||
Some(value) => match value {
|
||||
Value::String(val_str) => Some(val_str.clone()),
|
||||
Value::Number(val_num) => Some(val_num.to_string()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
let port = match port {
|
||||
Some(_) => port,
|
||||
None => match config.get(&key_port_2) {
|
||||
Some(value) => match value {
|
||||
Value::String(val_str) => Some(val_str.clone()),
|
||||
Value::Number(val_num) => Some(val_num.to_string()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
},
|
||||
};
|
||||
|
||||
let server = match config.get(&key_server) {
|
||||
Some(value) => match value {
|
||||
Value::String(val_str) => Some(val_str.clone()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
let secret = match config.get(&key_secret) {
|
||||
Some(value) => match value {
|
||||
Value::String(val_str) => Some(val_str.clone()),
|
||||
Value::Bool(val_bool) => Some(val_bool.to_string()),
|
||||
Value::Number(val_num) => Some(val_num.to_string()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
|
||||
ClashController {
|
||||
port,
|
||||
server,
|
||||
secret,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the `verge.yaml`
|
||||
pub fn read_verge() -> VergeConfig {
|
||||
read_yaml::<VergeConfig>(app_home_dir().join("verge.yaml"))
|
||||
}
|
||||
|
||||
/// Save Verge App Config
|
||||
pub fn save_verge(verge: &VergeConfig) -> Result<(), String> {
|
||||
save_yaml(
|
||||
app_home_dir().join("verge.yaml"),
|
||||
verge,
|
||||
Some("# The Config for Clash Verge App\n\n"),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user