generated from gleich/rust_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.rs
195 lines (173 loc) · 4.14 KB
/
conf.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
use std::path::Path;
use std::{fmt, fs};
use anyhow::Result;
use serde::Deserialize;
use strum_macros::{Display, EnumString, EnumVariantNames, FromRepr};
use crate::locations;
#[derive(Deserialize, Debug, PartialEq)]
pub struct Config {
pub author: String,
#[serde(default = "defaults::config_delimiter")]
pub delimiter: String,
pub open_with: Option<Vec<String>>,
#[serde(default = "defaults::config_default_format")]
pub default_format: Format,
pub classes: Vec<Class>,
pub view_with: Option<Vec<String>>,
#[serde(default = "defaults::latexmk")]
pub latexmk: bool,
}
#[derive(Deserialize, Debug, PartialEq, Clone)]
pub struct Class {
pub name: String,
pub teacher: String,
#[serde(default = "defaults::class_active")]
pub active: bool,
}
#[derive(PartialEq, Debug, Display, Deserialize, EnumVariantNames, EnumString, Clone, FromRepr)]
pub enum Format {
LaTeX,
Markdown,
}
#[derive(PartialEq, Debug, Display, Deserialize, EnumVariantNames, EnumString, FromRepr)]
pub enum DocumentType {
Worksheet,
Note,
Assessment,
Paper,
Lab,
Other,
}
mod defaults {
use super::Format;
pub fn latexmk() -> bool { false }
pub fn config_delimiter() -> String { String::from(">") }
pub fn config_default_format() -> Format { Format::Markdown }
pub fn class_active() -> bool { true }
}
impl Config {
pub fn read() -> Result<Config> {
let content = fs::read_to_string(locations::files::CONFIG)?;
Ok(toml::from_str::<Config>(&content)?)
}
}
impl Format {
pub fn extension(&self) -> &'static str {
match *self {
Format::LaTeX => ".tex",
Format::Markdown => ".md",
}
}
pub fn from_path(path: &Path) -> Option<Self> {
match path.extension().unwrap_or_default().to_str().unwrap() {
"tex" => Some(Format::LaTeX),
"md" => Some(Format::Markdown),
_ => None,
}
}
}
impl Default for Format {
fn default() -> Self { Self::LaTeX }
}
impl fmt::Display for Class {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} ({})", self.name, self.teacher)
}
}
#[cfg(test)]
mod test {
use toml::de::Error;
use super::Format;
use crate::conf::{Class, Config};
#[test]
fn read_config() -> Result<(), Error> {
// Super basic config file
assert_eq!(
toml::from_str::<Config>(
"
author = \"Matt Gleich\"
[[classes]]
name = \"AP Physics 2\"
teacher = \"Mr. Feynman\"
"
)?,
Config {
author: String::from("Matt Gleich"),
delimiter: String::from(">"),
open_with: None,
view_with: None,
default_format: Format::Markdown,
classes: vec![Class {
name: String::from("AP Physics 2"),
teacher: String::from("Mr. Feynman"),
active: true
}],
latexmk: false
}
);
// Custom default_format
assert_eq!(
toml::from_str::<Config>(
"
author = \"Matt Gleich\"
open_with = [\"code\"]
default_format = \"LaTeX\"
[[classes]]
name = \"AP Physics 2\"
teacher = \"Mr. Feynman\"
"
)?,
Config {
author: String::from("Matt Gleich"),
delimiter: String::from(">"),
open_with: Some(vec![String::from("code")]),
view_with: None,
default_format: Format::LaTeX,
classes: vec![Class {
name: String::from("AP Physics 2"),
teacher: String::from("Mr. Feynman"),
active: true
}],
latexmk: false
}
);
// Multiple classes
assert_eq!(
toml::from_str::<Config>(
"
author = \"Matt Gleich\"
open_with = [\"code\"]
default_format = \"LaTeX\"
[[classes]]
name = \"AP Physics 2\"
teacher = \"Mr. Feynman\"
[[classes]]
name = \"AP Chemistry 2\"
teacher = \"Mr. White\"
active = false
"
)?,
Config {
author: String::from("Matt Gleich"),
delimiter: String::from(">"),
open_with: Some(vec![String::from("code")]),
view_with: None,
default_format: Format::LaTeX,
classes: vec![
Class {
name: String::from("AP Physics 2"),
teacher: String::from("Mr. Feynman"),
active: true
},
Class {
name: String::from("AP Chemistry 2"),
teacher: String::from("Mr. White"),
active: false
}
],
latexmk: false
}
);
Ok(())
}
}