-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.rs
102 lines (87 loc) · 2.24 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
use serde::Deserialize;
use std::{error, fs, path::Path};
use toml::{self};
#[derive(Clone, Deserialize, Debug)]
pub struct Config {
pub theme: Option<Theme>,
pub core: Option<Core>,
pub info: Info,
}
#[derive(Clone, Deserialize, Debug)]
pub struct Core {
pub buffer_size: i8,
pub auto_commit: bool,
}
#[derive(Clone, Deserialize, Debug)]
pub struct Info {
pub name: String,
pub maintainors: Vec<String>,
pub input_method: String,
pub homepage: String,
}
#[derive(Clone, Deserialize, Debug)]
pub struct Theme {
pub header: SectionTheme,
pub body: SectionTheme,
}
#[derive(Clone, Deserialize, Debug)]
pub struct SectionTheme {
pub background: String,
pub foreground: String,
pub font: ThemeFont,
}
#[derive(Clone, Deserialize, Debug)]
pub struct ThemeFont {
pub family: String,
pub size: u64,
pub weight: String,
}
impl Default for &Core {
fn default() -> Self {
&Core {
buffer_size: 64,
auto_commit: false,
}
}
}
impl Default for Theme {
fn default() -> Self {
let font = ThemeFont {
family: "Charis-SIL".to_owned(),
size: 10,
weight: "bold".to_owned(),
};
let header = SectionTheme {
background: "#252320".to_owned(),
foreground: "#dedddd".to_owned(),
font: font.clone(),
};
let body = SectionTheme {
background: "#dedddd".to_owned(),
foreground: "#252320".to_owned(),
font,
};
Self { header, body }
}
}
impl Config {
pub fn from_file(filepath: &Path) -> Result<Self, Box<dyn error::Error>> {
let content = fs::read_to_string(filepath)?;
let config: Self = toml::from_str(&content)?;
Ok(config)
}
}
#[cfg(test)]
mod tests {
#[test]
fn from_file() {
use crate::config::Config;
use std::path::Path;
let config = Config::from_file(Path::new("./data/sample.toml"));
assert!(config.is_ok());
let config = Config::from_file(Path::new("./data/full_sample.toml"));
assert!(config.is_ok());
let config = Config::from_file(Path::new("./data/blank_sample.toml"));
assert!(config.is_err());
}
}