xref: /StarryEngine/starry_server/src/config.rs (revision bee61dca287acb4b9fd6d747ba3f687aebacab90)
1*bee61dcaSR0ronoa use std::{fs::File, io::Read};
2*bee61dcaSR0ronoa 
3*bee61dcaSR0ronoa use log::debug;
4*bee61dcaSR0ronoa use serde_derive::Deserialize;
5*bee61dcaSR0ronoa 
6*bee61dcaSR0ronoa /// TODO: 了解serde_derive::Deserialize及依赖
7*bee61dcaSR0ronoa /// 配置信息
8*bee61dcaSR0ronoa #[derive(Clone, Deserialize)]
9*bee61dcaSR0ronoa pub struct Config {
10*bee61dcaSR0ronoa     // TODO: 补充注释
11*bee61dcaSR0ronoa     pub normal: String,
12*bee61dcaSR0ronoa     pub bottom_left_corner: String,
13*bee61dcaSR0ronoa     pub bottom_right_corner: String,
14*bee61dcaSR0ronoa     pub bottom_side: String,
15*bee61dcaSR0ronoa     pub left_side: String,
16*bee61dcaSR0ronoa     pub right_side: String,
17*bee61dcaSR0ronoa     pub window_max: String,
18*bee61dcaSR0ronoa     pub window_max_unfocused: String,
19*bee61dcaSR0ronoa     pub window_close: String,
20*bee61dcaSR0ronoa     pub window_close_unfocused: String,
21*bee61dcaSR0ronoa     // TODO: 实现Color反序列化
22*bee61dcaSR0ronoa     // #[serde(default = "background_color_default")]
23*bee61dcaSR0ronoa     // pub background_color: Color,
24*bee61dcaSR0ronoa     // #[serde(default = "bar_color_default")]
25*bee61dcaSR0ronoa     // pub bar_color: Color,
26*bee61dcaSR0ronoa     // #[serde(default = "bar_highlight_color_default")]
27*bee61dcaSR0ronoa     // pub bar_highlight_color: Color,
28*bee61dcaSR0ronoa     // #[serde(default = "text_color_default")]
29*bee61dcaSR0ronoa     // pub text_color: Color,
30*bee61dcaSR0ronoa     // #[serde(default = "text_highlight_color_default")]
31*bee61dcaSR0ronoa     // pub text_highlight_color: Color,
32*bee61dcaSR0ronoa }
33*bee61dcaSR0ronoa 
34*bee61dcaSR0ronoa // fn background_color_default() -> Color { Color::rgb(0, 0, 0) }
35*bee61dcaSR0ronoa // fn bar_color_default() -> Color { Color::rgba(47, 52, 63, 224) }
36*bee61dcaSR0ronoa // fn bar_highlight_color_default() -> Color { Color::rgba(80, 86, 102, 224) }
37*bee61dcaSR0ronoa // fn text_color_default() -> Color { Color::rgb(204, 210, 224) }
38*bee61dcaSR0ronoa // fn text_highlight_color_default() -> Color { Color::rgb(204, 210, 224) }
39*bee61dcaSR0ronoa 
40*bee61dcaSR0ronoa impl Default for Config {
default() -> Self41*bee61dcaSR0ronoa     fn default() -> Self {
42*bee61dcaSR0ronoa         Config {
43*bee61dcaSR0ronoa             normal: String::default(),
44*bee61dcaSR0ronoa             bottom_left_corner: String::default(),
45*bee61dcaSR0ronoa             bottom_right_corner: String::default(),
46*bee61dcaSR0ronoa             bottom_side: String::default(),
47*bee61dcaSR0ronoa             left_side: String::default(),
48*bee61dcaSR0ronoa             right_side: String::default(),
49*bee61dcaSR0ronoa             window_max: String::default(),
50*bee61dcaSR0ronoa             window_max_unfocused: String::default(),
51*bee61dcaSR0ronoa             window_close: String::default(),
52*bee61dcaSR0ronoa             window_close_unfocused: String::default(),
53*bee61dcaSR0ronoa             // background_color: background_color_default(),
54*bee61dcaSR0ronoa             // bar_color: bar_color_default(),
55*bee61dcaSR0ronoa             // bar_highlight_color: bar_highlight_color_default(),
56*bee61dcaSR0ronoa             // text_color: text_color_default(),
57*bee61dcaSR0ronoa             // text_highlight_color: text_highlight_color_default(),
58*bee61dcaSR0ronoa         }
59*bee61dcaSR0ronoa     }
60*bee61dcaSR0ronoa }
61*bee61dcaSR0ronoa 
62*bee61dcaSR0ronoa impl Config {
63*bee61dcaSR0ronoa     /// 通过字符串解析配置
config_from_string(config: &str) -> Config64*bee61dcaSR0ronoa     fn config_from_string(config: &str) -> Config {
65*bee61dcaSR0ronoa         match toml::from_str(config) {
66*bee61dcaSR0ronoa             Ok(config) => config,
67*bee61dcaSR0ronoa             Err(err) => {
68*bee61dcaSR0ronoa                 println!("[Error] Config failed to parse config '{}'", err);
69*bee61dcaSR0ronoa                 Config::default()
70*bee61dcaSR0ronoa             }
71*bee61dcaSR0ronoa         }
72*bee61dcaSR0ronoa     }
73*bee61dcaSR0ronoa 
74*bee61dcaSR0ronoa     /// 通过文件路径解析配置
config_from_path(path: &str) -> Config75*bee61dcaSR0ronoa     pub fn config_from_path(path: &str) -> Config {
76*bee61dcaSR0ronoa         let mut string = String::new();
77*bee61dcaSR0ronoa 
78*bee61dcaSR0ronoa         match File::open(path) {
79*bee61dcaSR0ronoa             Ok(mut file) => match file.read_to_string(&mut string) {
80*bee61dcaSR0ronoa                 Ok(_) => debug!("[Info] Reading config from path: '{}'", path),
81*bee61dcaSR0ronoa                 Err(err) => debug!("[Error] Config failed to read config '{}': {}", path, err),
82*bee61dcaSR0ronoa             },
83*bee61dcaSR0ronoa             Err(err) => debug!("[Error] Config failed to open config '{}': {}", path, err),
84*bee61dcaSR0ronoa         }
85*bee61dcaSR0ronoa 
86*bee61dcaSR0ronoa         Self::config_from_string(&string)
87*bee61dcaSR0ronoa     }
88*bee61dcaSR0ronoa }
89