1use std::fmt::Debug;
2use std::{env, fs};
3use std::env::set_var;
4use std::path::PathBuf;
5use json::{JsonValue, object};
6
7#[derive(Clone, Debug)]
9pub struct Config {
10 pub root_dir: PathBuf,
12 pub config_dir: PathBuf,
14 pub log: bool,
16 pub extend: Vec<String>,
18 pub addon_hide: Vec<String>,
20 pub cache: bool,
22 pub token_exp: u64,
24 pub token: String,
26 pub pub_key_path: String,
28}
29
30impl Config {
31 pub fn new(config_dir: PathBuf) -> Result<Config, String> {
32 if !config_dir.is_dir() {
33 match fs::create_dir_all(&config_dir) {
34 Ok(_) => {}
35 Err(e) => {
36 return Err(format!("配置文件夹不存在: {}", e));
37 }
38 }
39 }
40 let root_dir = config_dir.parent().unwrap().to_path_buf();
42
43 set_var("ROOT_DIR", root_dir.to_str().unwrap());
44 set_var("CONFIG_DIR", config_dir.to_str().unwrap());
45
46 let t = config_dir.join("conf.json");
47 let json = if !t.is_file() {
48 Config::create_json_file(t.clone())?
49 } else {
50 match fs::read_to_string(t.clone()) {
51 Ok(e) => match json::parse(&e) {
52 Ok(e) => e,
53 Err(_) => Config::create_json_file(t.clone())?
54 },
55 Err(e) => return Err(format!("文件加载失败: {e}"))
56 }
57 };
58 Ok(Config::form(json))
59 }
60 fn create_json_file(path_buf: PathBuf) -> Result<JsonValue, String> {
61 let data = Config::default().json();
62 match fs::write(path_buf.clone(), data.dump().clone()) {
63 Ok(_) => {}
64 Err(e) => {
65 return Err(format!("文件写入失败: {}", e));
66 }
67 }
68 Ok(data)
69 }
70 fn form(json: JsonValue) -> Self {
71 let default = json["default"].as_str().unwrap_or("");
72 let json = json["connections"][default].clone();
73 let root_dir = PathBuf::from(env::var("ROOT_DIR").unwrap());
74 let config_dir = PathBuf::from(env::var("CONFIG_DIR").unwrap());
75 Self {
76 root_dir,
77 config_dir,
78 log: json["log"].as_bool().unwrap_or(true),
79 extend: json["extend"].members().map(|x| x.to_string()).collect::<Vec<String>>().clone(),
80 addon_hide: json["addon_hide"].members().map(|x| x.to_string()).collect::<Vec<String>>().clone(),
81 cache: json["cache"].as_bool().unwrap_or(false),
82 token_exp: json["token_exp"].as_u64().unwrap_or(60 * 60 * 24),
83 token: json["token"].as_str().unwrap_or("").to_string(),
84 pub_key_path: json["pub_key_path"].as_str().unwrap_or("").to_string(),
85 }
86 }
87 fn json(self) -> JsonValue {
88 let default = "temp".to_string();
89 let mut json_data = object! {
90 "default":default.clone(),
91 "connections":object! {}
92 };
93 let mut config = object! {};
94 config["log"] = self.log.into();
95 config["extend"] = self.extend.into();
96 config["addon_hide"] = self.addon_hide.into();
97 config["token_exp"] = self.token_exp.into();
98 config["cache"] = self.cache.into();
99 config["token"] = self.token.into();
100 config["pub_key_path"] = self.pub_key_path.into();
101 json_data["connections"][default] = config.clone();
102 json_data
103 }
104}
105
106impl Default for Config {
107 fn default() -> Self {
108 Self {
109 root_dir: Default::default(),
110 config_dir: Default::default(),
111 log: false,
112 extend: vec![],
113 addon_hide: vec![],
114 cache: false,
115 token_exp: 60 * 60 * 24,
116 token: "".to_string(),
117 pub_key_path: "".to_string(),
118 }
119 }
120}