-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.rs
More file actions
219 lines (193 loc) · 7.06 KB
/
file.rs
File metadata and controls
219 lines (193 loc) · 7.06 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
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use crate::{
file::{
content::wikilink::Alias,
name::{Filename, FilenameLowercase},
},
rules::ErrorCode,
sed::{ReplacePair, ReplacePairCompilationError},
};
use super::{Config as MasterConfig, NewConfigError, Partial};
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Config {
/// See [`super::cli::Config::files`]
#[serde(default)]
pub files: Option<Vec<String>>,
/// See [`super::cli::Config::new_files_directory`]
#[serde(default)]
pub new_files_directory: Option<PathBuf>,
/// See [`super::cli::Config::ngram_size`]
#[serde(default)]
pub ngram_size: Option<usize>,
/// See [`super::cli::Config::boundary_pattern`]
#[serde(default)]
pub boundary_pattern: Option<String>,
/// See [`super::cli::Config::filename_spacing_pattern`]
#[serde(default)]
pub filename_spacing_pattern: Option<String>,
/// See [`super::cli::Config::filename_match_threshold`]
#[serde(default)]
pub filename_match_threshold: Option<i64>,
/// See [`super::cli::Config::exclude`]
#[serde(default)]
pub exclude: Vec<String>,
/// In the [`crate::rules::similar_filename::SimilarFilename`] rule, ignore certain word pairs
/// Prevents some annoying and frequent false positives
#[serde(default)]
pub ignore_word_pairs: Vec<(String, String)>,
/// Convert an alias to a filename
/// Kinda like a sed command
#[serde(default)]
pub alias_to_filename: (String, String),
/// Convert a filename to an alias
/// Kinda like a sed command
#[serde(default)]
pub filename_to_alias: (String, String),
}
impl Config {
pub fn new(path: &Path) -> Result<Self, NewConfigError> {
let contents =
std::fs::read_to_string(path).map_err(NewConfigError::FileDoesNotReadError)?;
toml::from_str(&contents).map_err(NewConfigError::FileDoesNotParseError)
}
#[must_use]
pub fn original_file_globs(&self) -> Option<Vec<String>> {
self.files.clone()
}
}
impl From<MasterConfig> for Config {
fn from(value: MasterConfig) -> Self {
Self {
files: value.original_file_globs,
new_files_directory: Some(value.new_files_directory),
ngram_size: Some(value.ngram_size),
boundary_pattern: Some(value.boundary_pattern),
filename_spacing_pattern: Some(value.filename_spacing_pattern),
filename_match_threshold: Some(value.filename_match_threshold),
exclude: value.exclude.into_iter().map(|x| x.0).collect(),
ignore_word_pairs: value.ignore_word_pairs,
alias_to_filename: value.alias_to_filename.into(),
filename_to_alias: value.filename_to_alias.into(),
}
}
}
impl Partial for Config {
fn files(&self) -> Option<Vec<PathBuf>> {
let mut out = Vec::new();
match &self.files {
None => return None,
Some(files) if files.is_empty() => return None,
Some(files) => {
for file in files {
if file.contains('*') {
let globs = glob::glob(file);
match globs {
Ok(globs) => {
for glob in globs {
match glob {
Ok(path) => {
out.push(path);
}
Err(e) => {
eprintln!("Error processing glob '{file}': {e}",);
}
}
}
}
Err(e) => {
eprintln!("Error parsing glob pattern '{file}': {e}");
return None;
}
}
} else {
let path = PathBuf::from(file);
if path.exists() {
out.push(path);
} else {
eprintln!("File does not exist: {file}");
}
}
}
}
}
if out.is_empty() {
eprintln!("No valid files found in the configuration.");
None
} else {
Some(out)
}
}
fn new_files_directory(&self) -> Option<PathBuf> {
self.new_files_directory.clone()
}
fn ngram_size(&self) -> Option<usize> {
self.ngram_size
}
fn boundary_pattern(&self) -> Option<String> {
self.boundary_pattern.clone()
}
fn filename_spacing_pattern(&self) -> Option<String> {
self.filename_spacing_pattern.clone()
}
fn filename_match_threshold(&self) -> Option<i64> {
self.filename_match_threshold
}
fn exclude(&self) -> Option<Vec<ErrorCode>> {
let out = self.exclude.clone();
if out.is_empty() {
None
} else {
Some(out.into_iter().map(ErrorCode::new).collect())
}
}
fn alias_to_filename(
&self,
) -> Option<Result<ReplacePair<Alias, FilenameLowercase>, ReplacePairCompilationError>> {
let (to, from) = self.alias_to_filename.clone();
match (to.is_empty(), from.is_empty()) {
(true, true) => None,
(false, false) => Some(ReplacePair::new(&to, &from)),
(true, false) => Some(Err(ReplacePairCompilationError::ToError(
regex::Error::Syntax("To is empty".to_string()),
))),
(false, true) => Some(Err(ReplacePairCompilationError::FromError(
regex::Error::Syntax("From is empty".to_string()),
))),
}
}
fn filename_to_alias(
&self,
) -> Option<Result<ReplacePair<Filename, Alias>, ReplacePairCompilationError>> {
let (to, from) = self.alias_to_filename.clone();
match (to.is_empty(), from.is_empty()) {
(true, true) => None,
(false, false) => Some(ReplacePair::new(&to, &from)),
(true, false) => Some(Err(ReplacePairCompilationError::ToError(
regex::Error::Syntax("To is empty".to_string()),
))),
(false, true) => Some(Err(ReplacePairCompilationError::FromError(
regex::Error::Syntax("From is empty".to_string()),
))),
}
}
fn fix(&self) -> Option<bool> {
None
}
fn allow_dirty(&self) -> Result<Option<bool>, NewConfigError> {
Ok(None)
}
fn allow_staged(&self) -> Result<Option<bool>, NewConfigError> {
Ok(None)
}
fn ignore_word_pairs(&self) -> Option<Vec<(String, String)>> {
if self.ignore_word_pairs.is_empty() {
None
} else {
Some(self.ignore_word_pairs.clone())
}
}
fn ignore_remaining(&self) -> Option<bool> {
None
}
}