-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.rs
171 lines (147 loc) · 5.27 KB
/
file.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
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::pages_directory`]
pub pages_directory: PathBuf,
/// See [`super::cli::Config::other_directories`]
pub other_directories: Vec<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)
}
}
impl From<MasterConfig> for Config {
fn from(value: MasterConfig) -> Self {
Self {
pages_directory: value.pages_directory,
other_directories: value.other_directories,
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 pages_directory(&self) -> Option<PathBuf> {
Some(self.pages_directory.clone())
}
fn other_directories(&self) -> Option<Vec<PathBuf>> {
let out = self.other_directories.clone();
if out.is_empty() {
None
} else {
Some(out)
}
}
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) -> Option<bool> {
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
}
}