-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.rs
142 lines (120 loc) · 4.14 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
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::{NewConfigError, Partial};
#[derive(Serialize, Deserialize, Debug, Default)]
pub(super) 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::wikilink_pattern`]
#[serde(default)]
pub wikilink_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>,
/// 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 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
}
}