-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig.rs
More file actions
211 lines (186 loc) · 5.9 KB
/
config.rs
File metadata and controls
211 lines (186 loc) · 5.9 KB
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
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use crate::error::Fallacy;
use crate::utils::{expand_tilde, expand_tilde_str};
pub static MAN: &str = include_str!("../man/config.md");
#[derive(Serialize, Deserialize, Default)]
pub struct Config {
pub storage: StorageConfig,
pub filter: FilterConfig,
pub output: OutputConfig,
}
#[derive(Serialize, Deserialize)]
pub struct StorageConfig {
pub paper_metadata: PathBuf,
pub command_history: PathBuf,
pub max_history_size: usize,
pub file_dir: PathBuf,
pub note_dir: PathBuf,
}
#[derive(Serialize, Deserialize)]
pub struct FilterConfig {
pub case_insensitive_regex: bool,
}
#[derive(Serialize, Deserialize)]
pub struct OutputConfig {
pub table_columns: Vec<String>,
pub viewer_command: Vec<String>,
pub viewer_batch: bool,
pub editor_command: Vec<String>,
pub editor_batch: bool,
pub browser_command: Vec<String>,
pub label_colors: Option<HashMap<String, String>>,
pub exclusive_label_groups: Option<Vec<HashSet<String>>>,
}
impl Config {
pub fn validate(&mut self) -> Result<(), Fallacy> {
self.storage.validate()?;
self.filter.validate()?;
self.output.validate()?;
Ok(())
}
}
impl StorageConfig {
fn validate(&mut self) -> Result<(), Fallacy> {
self.paper_metadata = expand_tilde(&self.paper_metadata)?;
self.command_history = expand_tilde(&self.command_history)?;
self.file_dir = expand_tilde(&self.file_dir)?;
std::fs::create_dir_all(&self.file_dir)?;
self.note_dir = expand_tilde(&self.note_dir)?;
std::fs::create_dir_all(&self.note_dir)?;
Ok(())
}
}
impl FilterConfig {
fn validate(&mut self) -> Result<(), Fallacy> {
Ok(())
}
}
impl OutputConfig {
fn validate(&mut self) -> Result<(), Fallacy> {
let allowed_columns = vec![
"title",
"authors",
"first author",
"venue",
"year",
"labels",
];
// Convert everything to lowercase.
for field in &mut self.table_columns {
*field = field.to_lowercase();
}
// Check table columns.
for col in self.table_columns.iter() {
if !allowed_columns.contains(&&col[..]) {
return Err(Fallacy::ConfigAuditError(format!(
"Table column name {} is not supported.",
col
)));
}
}
// Check viewer command and expand tilde.
if self.viewer_command.is_empty() {
return Err(Fallacy::ConfigAuditError(
"Viewer command cannot be empty.".to_owned(),
));
}
for path in self.viewer_command.iter_mut() {
*path = expand_tilde_str(path)?;
}
// Check editor command and expand tilde.
if self.editor_command.is_empty() {
return Err(Fallacy::ConfigAuditError(
"Editor command cannot be empty.".to_owned(),
));
}
for path in self.editor_command.iter_mut() {
*path = expand_tilde_str(path)?;
}
// Check browser command and expand tilde.
if self.browser_command.is_empty() {
return Err(Fallacy::ConfigAuditError(
"Browser command cannot be emtpy.".to_owned(),
));
}
for path in self.browser_command.iter_mut() {
*path = expand_tilde_str(path)?;
}
Ok(())
}
}
impl Default for StorageConfig {
fn default() -> Self {
let data_dir = match home::home_dir() {
Some(mut p) => {
p.push(".local/share/reason");
p
}
None => {
eprintln!("Failed to find your home directory. Using the current directory to save state and history.");
PathBuf::from(".")
}
};
let paper_metadata = {
let mut path = data_dir.clone();
path.push("metadata.yaml");
path
};
let command_history = {
let mut path = data_dir.clone();
path.push("history.txt");
path
};
let max_history_size = 1000;
let file_base_dir = {
let mut path = data_dir.clone();
path.push("files");
path
};
let note_dir = {
let mut path = data_dir;
path.push("notes");
path
};
Self {
paper_metadata,
command_history,
max_history_size,
file_dir: file_base_dir,
note_dir,
}
}
}
impl Default for FilterConfig {
fn default() -> Self {
Self {
case_insensitive_regex: false,
}
}
}
impl Default for OutputConfig {
fn default() -> Self {
let table_columns = vec!["title", "first author", "venue", "year", "labels"];
let table_columns = table_columns.into_iter().map(|s| s.to_string()).collect();
let viewer_command = vec![String::from("zathura")];
let viewer_batch = false;
let editor_command = vec![String::from("vim"), String::from("-p")];
let editor_batch = true;
let browser_command = vec![String::from("google-chrome-stable")];
let mut label_colors = HashMap::new();
label_colors.insert(String::from("done"), String::from("Green"));
label_colors.insert(String::from("active"), String::from("Yellow"));
let exclusive_label_groups = vec![["done".to_owned(), "active".to_owned()].into()];
Self {
table_columns,
viewer_command,
viewer_batch,
editor_command,
editor_batch,
browser_command,
label_colors: Some(label_colors),
exclusive_label_groups: Some(exclusive_label_groups),
}
}
}