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