-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathsettings.rs
More file actions
481 lines (415 loc) · 16 KB
/
settings.rs
File metadata and controls
481 lines (415 loc) · 16 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use std::{
io,
path::{self, Path, PathBuf},
sync::Arc,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
pub struct CustomDictionariesEntry {
/// The name of the custom dictionary
#[serde(default)]
pub name: String,
/// An absolute or relative path to the custom dictionary
#[serde(default)]
pub path: String,
/// Allow adding words to this dictionary
#[serde(default)]
pub allow_add_words: bool,
/// For internal use to track the coodbook.toml that originated this entry
#[serde(skip)]
pub config_file_path: Option<Arc<Path>>,
}
impl CustomDictionariesEntry {
pub fn resolve_full_path(&self) -> Result<PathBuf, io::Error> {
let full_path = if let Some(config_file_path) = &self.config_file_path {
config_file_path
.parent()
.ok_or(io::Error::from(io::ErrorKind::NotFound))?
.join(Path::new(&self.path))
} else {
PathBuf::from(&self.path)
};
path::absolute(&full_path)
}
}
#[derive(Debug, Serialize, Clone, PartialEq)]
pub struct ConfigSettings {
/// List of dictionaries to use for spell checking
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub dictionaries: Vec<String>,
/// List of custom dictionaries to use for spell checking
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub custom_dictionaries_definitions: Vec<CustomDictionariesEntry>,
/// Custom allowlist of words
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub words: Vec<String>,
/// Words that should always be flagged
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub flag_words: Vec<String>,
/// Glob patterns for paths to ignore
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub ignore_paths: Vec<String>,
/// Regex patterns for text to ignore
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub ignore_patterns: Vec<String>,
/// Whether to use global configuration
#[serde(
default = "default_use_global",
skip_serializing_if = "is_default_use_global"
)]
pub use_global: bool,
/// Minimum word length to check (words shorter than this are ignored)
#[serde(
default = "default_min_word_length",
skip_serializing_if = "is_default_min_word_length"
)]
pub min_word_length: usize,
}
fn default_use_global() -> bool {
true
}
fn is_default_use_global(value: &bool) -> bool {
*value == default_use_global()
}
fn default_min_word_length() -> usize {
3
}
fn is_default_min_word_length(value: &usize) -> bool {
*value == default_min_word_length()
}
impl Default for ConfigSettings {
fn default() -> Self {
Self {
dictionaries: vec![],
custom_dictionaries_definitions: vec![],
words: Vec::new(),
flag_words: Vec::new(),
ignore_paths: Vec::new(),
ignore_patterns: Vec::new(),
use_global: true,
min_word_length: default_min_word_length(),
}
}
}
impl<'de> Deserialize<'de> for ConfigSettings {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
fn to_lowercase_vec(v: Vec<String>) -> Vec<String> {
v.into_iter().map(|s| s.to_ascii_lowercase()).collect()
}
#[derive(Deserialize)]
struct Helper {
#[serde(default)]
dictionaries: Vec<String>,
#[serde(default)]
custom_dictionaries_definitions: Vec<CustomDictionariesEntry>,
#[serde(default)]
words: Vec<String>,
#[serde(default)]
flag_words: Vec<String>,
#[serde(default)]
ignore_paths: Vec<String>,
#[serde(default)]
ignore_patterns: Vec<String>,
#[serde(default = "default_use_global")]
use_global: bool,
#[serde(default = "default_min_word_length")]
min_word_length: usize,
}
let helper = Helper::deserialize(deserializer)?;
Ok(ConfigSettings {
dictionaries: to_lowercase_vec(helper.dictionaries),
custom_dictionaries_definitions: helper
.custom_dictionaries_definitions
.into_iter()
.map(|mut c| {
c.name.make_ascii_lowercase();
c
})
.collect(),
words: to_lowercase_vec(helper.words),
flag_words: to_lowercase_vec(helper.flag_words),
ignore_paths: helper.ignore_paths,
ignore_patterns: helper.ignore_patterns,
use_global: helper.use_global,
min_word_length: helper.min_word_length,
})
}
}
impl ConfigSettings {
/// Merge another config settings into this one, sorting and deduplicating all collections, prioritizing self when possible
pub fn merge(&mut self, other: ConfigSettings) {
// Add items from the other config
self.dictionaries.extend(other.dictionaries);
self.custom_dictionaries_definitions
.extend(other.custom_dictionaries_definitions);
self.words.extend(other.words);
self.flag_words.extend(other.flag_words);
self.ignore_paths.extend(other.ignore_paths);
self.ignore_patterns.extend(other.ignore_patterns);
// The use_global setting from the other config is ignored during merging
// as this is a per-config setting
// Override min_word_length if the other config has a non-default value
if other.min_word_length != default_min_word_length() {
self.min_word_length = other.min_word_length;
}
// Sort and deduplicate each collection
self.sort_and_dedup();
}
/// Sort and deduplicate all collections in the config
pub fn sort_and_dedup(&mut self) {
// Sort and deduplicate each Vec
sort_and_dedup(&mut self.dictionaries);
sort_and_dedup_by(&mut self.custom_dictionaries_definitions, |d1, d2| {
d1.name.cmp(&d2.name)
});
sort_and_dedup(&mut self.words);
sort_and_dedup(&mut self.flag_words);
sort_and_dedup(&mut self.ignore_paths);
sort_and_dedup(&mut self.ignore_patterns);
}
pub fn set_config_file_paths(&mut self, config_path: &Path) {
let config_path: Arc<Path> = Arc::from(config_path);
for custom_directory in &mut self.custom_dictionaries_definitions {
custom_directory.config_file_path = Some(config_path.clone());
}
}
}
/// Helper function to sort and deduplicate a Vec of strings
fn sort_and_dedup(vec: &mut Vec<String>) {
vec.sort();
vec.dedup();
}
pub fn sort_and_dedup_by<T, F>(vec: &mut Vec<T>, f: F)
where
F: Fn(&T, &T) -> std::cmp::Ordering,
{
vec.sort_by(&f);
vec.dedup_by(|d1, d2| f(d1, d2) == std::cmp::Ordering::Equal);
}
#[cfg(test)]
mod tests {
use super::*;
fn build_fake_custom_dict(name: &str) -> CustomDictionariesEntry {
CustomDictionariesEntry {
name: name.into(),
path: name.into(),
..Default::default()
}
}
#[test]
fn test_default() {
let config = ConfigSettings::default();
assert_eq!(config.dictionaries, Vec::<String>::new());
assert_eq!(config.words, Vec::<String>::new());
assert_eq!(config.flag_words, Vec::<String>::new());
assert_eq!(config.ignore_paths, Vec::<String>::new());
assert_eq!(config.ignore_patterns, Vec::<String>::new());
assert!(config.use_global);
assert_eq!(config.min_word_length, 3);
}
#[test]
fn test_deserialization() {
let toml_str = r#"
dictionaries = ["EN_US", "en_GB"]
words = ["CodeBook", "Rust"]
flag_words = ["TODO", "FIXME"]
ignore_paths = ["**/*.md", "target/"]
ignore_patterns = ["^```.*$", "^//.*$"]
use_global = false
"#;
let config: ConfigSettings = toml::from_str(toml_str).unwrap();
assert_eq!(config.dictionaries, vec!["en_us", "en_gb"]);
assert_eq!(config.words, vec!["codebook", "rust"]);
assert_eq!(config.flag_words, vec!["todo", "fixme"]);
assert_eq!(config.ignore_paths, vec!["**/*.md", "target/"]);
// Don't test the exact order, just check that both elements are present
assert_eq!(config.ignore_patterns.len(), 2);
assert!(config.ignore_patterns.contains(&"^```.*$".to_string()));
assert!(config.ignore_patterns.contains(&"^//.*$".to_string()));
assert!(!config.use_global);
}
#[test]
fn test_min_word_length_deserialization() {
// Test with explicit value
let toml_str = r#"
min_word_length = 2
"#;
let config: ConfigSettings = toml::from_str(toml_str).unwrap();
assert_eq!(config.min_word_length, 2);
// Test with default value (when not specified)
let toml_str = r#"
dictionaries = ["en_us"]
"#;
let config: ConfigSettings = toml::from_str(toml_str).unwrap();
assert_eq!(config.min_word_length, 3);
}
#[test]
fn test_serialization() {
let config = ConfigSettings {
dictionaries: vec!["en_us".to_string()],
words: vec!["rust".to_string()],
..Default::default()
};
let serialized = toml::to_string(&config).unwrap();
assert!(serialized.contains("dictionaries = [\"en_us\"]"));
assert!(serialized.contains("words = [\"rust\"]"));
// Defaults should not be there
assert!(!serialized.contains("use_global = true"));
assert!(!serialized.contains("min_word_length = 3"));
}
#[test]
fn test_merge() {
let mut duplicate_custom_dict = build_fake_custom_dict("duplicate");
let mut base = ConfigSettings {
dictionaries: vec!["en_us".to_string()],
custom_dictionaries_definitions: vec![
build_fake_custom_dict("base_unique"),
duplicate_custom_dict.clone(),
],
words: vec!["codebook".to_string()],
flag_words: vec!["todo".to_string()],
ignore_paths: vec!["**/*.md".to_string()],
ignore_patterns: vec!["^```.*$".to_string()],
use_global: true,
min_word_length: 3,
};
// flip allow_add_words to true, to create a disparity between the dictionaries
duplicate_custom_dict.allow_add_words = !duplicate_custom_dict.allow_add_words;
let other = ConfigSettings {
dictionaries: vec!["en_gb".to_string(), "en_us".to_string()],
custom_dictionaries_definitions: vec![
duplicate_custom_dict.clone(),
build_fake_custom_dict("other_unique"),
],
words: vec!["rust".to_string()],
flag_words: vec!["fixme".to_string()],
ignore_paths: vec!["target/".to_string()],
ignore_patterns: vec!["^//.*$".to_string()],
use_global: false,
min_word_length: 2,
};
base.merge(other);
// After merging and deduplicating, we should have combined items
assert_eq!(base.dictionaries, vec!["en_gb", "en_us"]);
assert_eq!(
base.custom_dictionaries_definitions
.iter()
.map(|d| d.name.clone())
.collect::<Vec<String>>(),
vec!["base_unique", "duplicate", "other_unique"]
);
assert_eq!(base.words, vec!["codebook", "rust"]);
assert_eq!(base.flag_words, vec!["fixme", "todo"]);
assert_eq!(base.ignore_paths, vec!["**/*.md", "target/"]);
// Don't test the exact order, just check that both elements are present
assert_eq!(base.ignore_patterns.len(), 2);
assert!(base.ignore_patterns.contains(&"^```.*$".to_string()));
assert!(base.ignore_patterns.contains(&"^//.*$".to_string()));
// use_global from the base should be preserved
assert!(base.use_global);
// min_word_length from other should override base (since it's non-default)
assert_eq!(base.min_word_length, 2);
// Assert that base custom_dictionaries_definitions took priority
assert_ne!(
base.custom_dictionaries_definitions.iter().find(|d| d.name == "duplicate").expect("custom_dictionaries_definitions duplicate must be present if set in ether of the merged dictionaries").allow_add_words
,duplicate_custom_dict.allow_add_words
);
}
#[test]
fn test_merge_min_word_length_default() {
let mut base = ConfigSettings {
dictionaries: vec!["en_us".to_string()],
min_word_length: 5,
..Default::default()
};
let other = ConfigSettings {
dictionaries: vec!["en_gb".to_string()],
min_word_length: 3, // default value
..Default::default()
};
base.merge(other);
// min_word_length from base should be preserved when other has default
assert_eq!(base.min_word_length, 5);
}
#[test]
fn test_sort_and_dedup() {
let mut config = ConfigSettings {
dictionaries: vec![
"en_gb".to_string(),
"en_us".to_string(),
"en_gb".to_string(),
],
custom_dictionaries_definitions: vec![
build_fake_custom_dict("custom_1"),
build_fake_custom_dict("custom_2"),
build_fake_custom_dict("custom_1"),
],
words: vec![
"rust".to_string(),
"codebook".to_string(),
"rust".to_string(),
],
flag_words: vec!["fixme".to_string(), "todo".to_string(), "fixme".to_string()],
ignore_paths: vec![
"target/".to_string(),
"**/*.md".to_string(),
"target/".to_string(),
],
ignore_patterns: vec![
"^//.*$".to_string(),
"^```.*$".to_string(),
"^//.*$".to_string(),
],
use_global: true,
min_word_length: 3,
};
config.sort_and_dedup();
assert_eq!(config.dictionaries, vec!["en_gb", "en_us"]);
assert_eq!(
config
.custom_dictionaries_definitions
.iter()
.map(|d| d.name.clone())
.collect::<Vec<String>>(),
vec!["custom_1", "custom_2"]
);
assert_eq!(config.words, vec!["codebook", "rust"]);
assert_eq!(config.flag_words, vec!["fixme", "todo"]);
assert_eq!(config.ignore_paths, vec!["**/*.md", "target/"]);
// Don't test the exact order, just check that both elements are present and duplicates removed
assert_eq!(config.ignore_patterns.len(), 2);
assert!(config.ignore_patterns.contains(&"^```.*$".to_string()));
assert!(config.ignore_patterns.contains(&"^//.*$".to_string()));
}
#[test]
fn test_use_global_default() {
let toml_str = r#"
dictionaries = ["EN_US"]
"#;
let config: ConfigSettings = toml::from_str(toml_str).unwrap();
assert!(config.use_global);
}
#[test]
fn test_empty_deserialization() {
let toml_str = "";
let config: ConfigSettings = toml::from_str(toml_str).unwrap();
assert_eq!(config, ConfigSettings::default());
}
#[test]
fn test_partial_deserialization() {
let toml_str = r#"
dictionaries = ["EN_US"]
words = ["CodeBook"]
"#;
let config: ConfigSettings = toml::from_str(toml_str).unwrap();
assert_eq!(config.dictionaries, vec!["en_us"]);
assert_eq!(config.words, vec!["codebook"]);
assert_eq!(config.flag_words, Vec::<String>::new());
assert_eq!(config.ignore_paths, Vec::<String>::new());
assert_eq!(config.ignore_patterns, Vec::<String>::new());
assert!(config.use_global);
}
}