-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.rs
227 lines (202 loc) · 6.3 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
//! # In this module, you can find code that helps in collecting cli arguments and determining properties of a file containing vocab data.
use crate::*;
use clap::Parser;
use std::{collections::HashMap, path::PathBuf};
#[derive(Parser, Debug, PartialEq)]
#[command(version, about, author, long_about = None)]
pub struct Config {
/// Path of the file to learn
#[arg()]
file_path: PathBuf,
/// Swap terms and definitions of cards
#[arg(short = 's', long, default_value_t = false)]
pub swap: bool,
/// Sometimes ask the term, sometimes definition of cards
#[arg(short, long, default_value_t = false)]
pub ask_both: bool,
/// Convert valid verbs to cards
#[arg(long, default_value_t = false)]
pub convert: bool,
/// Delimiter used in file to seperate terms and definitions
#[arg(short, long)]
delim: Option<char>,
/// Don't shuffle card order
#[arg(long, default_value_t = false)]
pub no_shuffle: bool,
/// Don't load previous state
#[arg(long, default_value_t = false)]
pub no_state: bool,
/// Only check file syntax don't actually start learning deck
#[arg(long = "check", default_value_t = false)]
pub only_check: bool,
}
impl Config {
/// Fixing properties by opening file that contains vocab data.
///
/// # Errors
///
/// - `fs::create()`
/// - `get_prog_path()`
/// - `writeln!()`
/// - `fs::read_to_string()`
/// - `get_delim()`
///
/// # Panics
///
/// `delim` is empty
pub fn fix_from_file() -> AnyErr<Self> {
let conf = Config::parse();
let content = state::get_content(&conf)?;
trace!("file content: {content}");
let delim = if let Some(dlm) = conf.delim {
info!("got delimiter: {dlm} as arg");
dlm
} else {
info!("found delimiter in file");
get_delim(&content)?
};
info!("delimiter: \'{delim}\'");
Ok(Config {
delim: Some(delim),
..conf
})
}
/// Path for statefile of filepath got, or if doesn't exist, self
///
/// # Panics
///
/// `get_prog_path()`
pub fn file_path(&self) -> PathBuf {
if state::prog_exists(&self.file_path_orig()) && !self.no_state {
state::prog_path(&self.file_path_orig()).expect("Couldn't get progress path")
} else {
self.file_path.clone()
}
}
/// Get original [`Config::file_path`]
pub fn file_path_orig(&self) -> PathBuf {
self.file_path.clone()
}
/// Get delimiter
///
/// # Panics
///
/// `delim` is `None`
pub fn delim(&self) -> char {
self.delim.expect("oh my! no valid delimiter found")
}
}
/// Get delimiter from content
fn get_delim(content: &str) -> AnyErr<char> {
const DELIMS: [char; 5] = [';', '|', '\t', '=', ':' /*',', '-'*/];
info!("currently supported delimiters: {DELIMS:?}");
if let Ok(delim) = get_prop(content, "delim") {
info!("delim: {delim} was written to file as a property");
return Ok(delim.chars().next().unwrap());
}
let mut delims_counts: HashMap<char, u32> = HashMap::new();
for delim in &DELIMS {
let mut delim_count = 0;
content
.lines()
.filter(|line| !line.trim().starts_with('#') && !line.is_empty())
.for_each(|line| delim_count += line.trim().chars().filter(|c| c == delim).count());
if delim_count > 0 {
delims_counts.insert(*delim, delim_count.try_into()?);
}
}
trace!("possible delimiters and their counts: {delims_counts:?}");
if delims_counts.is_empty() {
error!("couldn't determine delimiter");
Err(format!("couldn't determine delimiter, should be one of: {DELIMS:?}").into())
} else {
Ok(*delims_counts.iter().max_by_key(|x| x.1).unwrap().0)
}
}
/// Get property from content
fn get_prop(content: &str, prop: &str) -> AnyErr<String> {
if content.contains("[crablit]") {
trace!("text contains [crablit]!");
let prop = &format!("{prop} = ");
if !content.contains(prop) {
error!("Couldn't find \"{prop}\"");
return Err(format!("Couldn't find \"{prop}\"").into());
}
Ok(content
.lines()
.find(|line| line.contains(prop))
.unwrap()
.split('=')
.last()
.unwrap()
.trim()
.trim_matches(|c| c == '"' || c == '\'')
.into())
} else {
Err(format!("Couldn't find {prop}").into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_delim_config() {
let content = "\
# [crablit]
# mode = \"cards\"
# delim = ':'
or : ||
and : &&
no command : ;;;;;;
";
assert_eq!(':', get_delim(content).unwrap());
}
#[test]
fn get_delim_correct() {
let content = "rot ; narancssárga";
assert_eq!(';', get_delim(content).unwrap());
}
#[test]
fn get_delim_hard() {
let content = "barn\ta ; braun\nfluxus ; bohókás ármány";
assert_eq!(';', get_delim(content).unwrap());
}
#[test]
#[should_panic]
fn get_delim_incorrect() {
let content = "# barna , braun";
assert_eq!(';', get_delim(content).unwrap());
}
// #[test]
// fn configg() {
// let mut command = Command::cargo_bin("crablit").unwrap();
// command.arg("test.txt");
// }
// #[test]
// fn basic_correct_cards() {
// let orig_conf = Config {
// file_path: "test.txt".to_owned(),
// card_swap: false,
// ask_both: false,
// mode: "".to_string(),
// delim: "".to_string(),
// no_shuffle: true,
// };
// let content = "\
// # test deck, cards
// term1 ; def1
// term2 ; def2
// ";
// assert_eq!(
// Config {
// file_path: "test".to_string(),
// card_swap: false,
// ask_both: false,
// mode: "cards".to_string(),
// delim: ";".to_string(),
// no_shuffle: true
// },
// Config::fix_from_file().unwrap()
// );
// }
}