-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconfig.rs
358 lines (321 loc) · 10 KB
/
config.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use std::{
env,
path::{Path, PathBuf},
};
use serde::Deserialize;
use crate::keybind::KeyBind;
const APP_DIR_NAME: &str = "serie";
const CONFIG_FILE_NAME: &str = "config.toml";
const CONFIG_FILE_ENV_NAME: &str = "SERIE_CONFIG_FILE";
const DEFAULT_LIST_SUBJECT_MIN_WIDTH: u16 = 20;
const DEFAULT_LIST_DATE_FORMAT: &str = "%Y-%m-%d";
const DEFAULT_LIST_DATE_WIDTH: u16 = 10;
const DEFAULT_LIST_DATE_LOCAL: bool = true;
const DEFAULT_LIST_NAME_WIDTH: u16 = 20;
const DEFAULT_DETAIL_DATE_FORMAT: &str = "%Y-%m-%d %H:%M:%S %z";
const DEFAULT_DETAIL_DATE_LOCAL: bool = true;
const DEFAULT_DETAIL_HEIGHT: u16 = 20;
const DEFAULT_REFS_WIDTH: u16 = 26;
const DEFAULT_GRAPH_COLOR_BRANCHES: [&str; 6] = [
"#E06C76", "#98C379", "#E5C07B", "#61AFEF", "#C678DD", "#56B6C2",
];
const DEFAULT_GRAPH_COLOR_EDGE: &str = "#00000000";
const DEFAULT_GRAPH_COLOR_BACKGROUND: &str = "#00000000";
pub fn load() -> (UiConfig, GraphConfig, Option<KeyBind>) {
let config = match config_file_path_from_env() {
Some(user_path) => {
if !user_path.exists() {
panic!("Config file not found: {:?}", user_path);
}
read_config_from_path(&user_path)
}
None => {
let default_path = config_file_path();
if default_path.exists() {
read_config_from_path(&default_path)
} else {
Config::default()
}
}
};
(config.ui, config.graph, config.keybind)
}
fn config_file_path_from_env() -> Option<PathBuf> {
env::var(CONFIG_FILE_ENV_NAME).ok().map(PathBuf::from)
}
fn config_file_path() -> PathBuf {
use directories::BaseDirs;
BaseDirs::new()
.expect("Couldn't get the base user directories!")
.config_dir()
.join(APP_DIR_NAME)
.join(CONFIG_FILE_NAME)
}
fn read_config_from_path(path: &Path) -> Config {
let content = std::fs::read_to_string(path).unwrap();
toml::from_str(&content).unwrap()
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize)]
struct Config {
#[serde(default)]
ui: UiConfig,
#[serde(default)]
graph: GraphConfig,
// The user customed keybinds, please ref `assets/default-keybind.toml`
keybind: Option<KeyBind>,
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize)]
pub struct UiConfig {
#[serde(default)]
pub list: UiListConfig,
#[serde(default)]
pub detail: UiDetailConfig,
#[serde(default)]
pub refs: UiRefsConfig,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct UiListConfig {
#[serde(default = "ui_list_subject_min_width_default")]
pub subject_min_width: u16,
#[serde(default = "ui_list_date_format_default")]
pub date_format: String,
#[serde(default = "ui_list_date_width_default")]
pub date_width: u16,
#[serde(default = "ui_list_date_local_default")]
pub date_local: bool,
#[serde(default = "ui_list_name_width_default")]
pub name_width: u16,
}
impl Default for UiListConfig {
fn default() -> Self {
Self {
subject_min_width: ui_list_subject_min_width_default(),
date_format: ui_list_date_format_default(),
date_width: ui_list_date_width_default(),
date_local: ui_list_date_local_default(),
name_width: ui_list_name_width_default(),
}
}
}
fn ui_list_subject_min_width_default() -> u16 {
DEFAULT_LIST_SUBJECT_MIN_WIDTH
}
fn ui_list_date_format_default() -> String {
DEFAULT_LIST_DATE_FORMAT.to_string()
}
fn ui_list_date_width_default() -> u16 {
DEFAULT_LIST_DATE_WIDTH
}
fn ui_list_date_local_default() -> bool {
DEFAULT_LIST_DATE_LOCAL
}
fn ui_list_name_width_default() -> u16 {
DEFAULT_LIST_NAME_WIDTH
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct UiDetailConfig {
#[serde(default = "ui_detail_height_default")]
pub height: u16,
#[serde(default = "ui_detail_date_format_default")]
pub date_format: String,
#[serde(default = "ui_detail_date_local_default")]
pub date_local: bool,
}
impl Default for UiDetailConfig {
fn default() -> Self {
Self {
height: ui_detail_height_default(),
date_format: ui_detail_date_format_default(),
date_local: ui_detail_date_local_default(),
}
}
}
fn ui_detail_height_default() -> u16 {
DEFAULT_DETAIL_HEIGHT
}
fn ui_detail_date_format_default() -> String {
DEFAULT_DETAIL_DATE_FORMAT.to_string()
}
fn ui_detail_date_local_default() -> bool {
DEFAULT_DETAIL_DATE_LOCAL
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct UiRefsConfig {
#[serde(default = "ui_refs_width_default")]
pub width: u16,
}
impl Default for UiRefsConfig {
fn default() -> Self {
Self {
width: ui_refs_width_default(),
}
}
}
fn ui_refs_width_default() -> u16 {
DEFAULT_REFS_WIDTH
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize)]
pub struct GraphConfig {
#[serde(default)]
pub color: GraphColorConfig,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct GraphColorConfig {
#[serde(default = "graph_color_branches_default")]
pub branches: Vec<String>,
#[serde(default = "graph_color_edge_default")]
pub edge: String,
#[serde(default = "graph_color_background_default")]
pub background: String,
}
impl Default for GraphColorConfig {
fn default() -> Self {
Self {
branches: graph_color_branches_default(),
edge: graph_color_edge_default(),
background: graph_color_background_default(),
}
}
}
fn graph_color_branches_default() -> Vec<String> {
DEFAULT_GRAPH_COLOR_BRANCHES
.iter()
.map(|s| s.to_string())
.collect()
}
fn graph_color_edge_default() -> String {
DEFAULT_GRAPH_COLOR_EDGE.into()
}
fn graph_color_background_default() -> String {
DEFAULT_GRAPH_COLOR_BACKGROUND.into()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_config_default() {
let actual = Config::default();
let expected = Config {
ui: UiConfig {
list: UiListConfig {
subject_min_width: 20,
date_format: "%Y-%m-%d".into(),
date_width: 10,
date_local: true,
name_width: 20,
},
detail: UiDetailConfig {
height: 20,
date_format: "%Y-%m-%d %H:%M:%S %z".into(),
date_local: true,
},
refs: UiRefsConfig { width: 26 },
},
graph: GraphConfig {
color: GraphColorConfig {
branches: vec![
"#E06C76".into(),
"#98C379".into(),
"#E5C07B".into(),
"#61AFEF".into(),
"#C678DD".into(),
"#56B6C2".into(),
],
edge: "#00000000".into(),
background: "#00000000".into(),
},
},
keybind: None,
};
assert_eq!(actual, expected);
}
#[test]
fn test_config_complete_toml() {
let toml = r##"
[ui.list]
subject_min_width = 40
date_format = "%Y/%m/%d"
date_width = 20
date_local = false
name_width = 30
[ui.detail]
height = 30
date_format = "%Y/%m/%d %H:%M:%S"
date_local = false
[ui.refs]
width = 40
[graph.color]
branches = ["#ff0000", "#00ff00", "#0000ff"]
edge = "#000000"
background = "#ffffff"
"##;
let actual: Config = toml::from_str(toml).unwrap();
let expected = Config {
ui: UiConfig {
list: UiListConfig {
subject_min_width: 40,
date_format: "%Y/%m/%d".into(),
date_width: 20,
date_local: false,
name_width: 30,
},
detail: UiDetailConfig {
height: 30,
date_format: "%Y/%m/%d %H:%M:%S".into(),
date_local: false,
},
refs: UiRefsConfig { width: 40 },
},
graph: GraphConfig {
color: GraphColorConfig {
branches: vec!["#ff0000".into(), "#00ff00".into(), "#0000ff".into()],
edge: "#000000".into(),
background: "#ffffff".into(),
},
},
keybind: None,
};
assert_eq!(actual, expected);
}
#[test]
fn test_config_partial_toml() {
let toml = r#"
[ui.list]
date_format = "%Y/%m/%d"
"#;
let actual: Config = toml::from_str(toml).unwrap();
let expected = Config {
ui: UiConfig {
list: UiListConfig {
subject_min_width: 20,
date_format: "%Y/%m/%d".into(),
date_width: 10,
date_local: true,
name_width: 20,
},
detail: UiDetailConfig {
height: 20,
date_format: "%Y-%m-%d %H:%M:%S %z".into(),
date_local: true,
},
refs: UiRefsConfig { width: 26 },
},
graph: GraphConfig {
color: GraphColorConfig {
branches: vec![
"#E06C76".into(),
"#98C379".into(),
"#E5C07B".into(),
"#61AFEF".into(),
"#C678DD".into(),
"#56B6C2".into(),
],
edge: "#00000000".into(),
background: "#00000000".into(),
},
},
keybind: None,
};
assert_eq!(actual, expected);
}
}