-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregex_parts.rs
272 lines (251 loc) · 9.75 KB
/
regex_parts.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
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
//! Parts of a [`Regex`] for easy portability and lazy compilation.
use std::str::FromStr;
use serde::{Serialize, Deserialize};
use regex::{Regex, RegexBuilder};
#[expect(unused_imports, reason = "Used in a doc comment.")]
use super::RegexWrapper;
use crate::types::*;
use crate::util::*;
/// Config on how to make a [`Regex`].
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(remote = "Self")]
pub struct RegexParts {
/// The regex pattern to use.
///
/// For performance, validity is only checked at [`Self::build`] time.
pub pattern: String,
/// Config to pass into [`RegexBuilder`].
#[serde(flatten)]
pub config: RegexConfig
}
impl RegexParts {
/// Compile the regex.
/// # Errors
/// If the call to [`RegexBuilder::build`] returns an error, that error is returned.
pub fn build(&self) -> Result<Regex, regex::Error> {
debug!(RegexParts::build, self);
RegexBuilder::new(&self.pattern)
.case_insensitive (self.config.case_insensitive )
.crlf (self.config.crlf )
.dot_matches_new_line(self.config.dot_matches_new_line)
.ignore_whitespace (self.config.ignore_whitespace )
.line_terminator (self.config.line_terminator )
.multi_line (self.config.multi_line )
.octal (self.config.octal )
.swap_greed (self.config.swap_greed )
.unicode (self.config.unicode )
.build()
}
}
crate::util::string_or_struct_magic!(RegexParts);
impl FromStr for RegexParts {
type Err = std::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(s.into())
}
}
impl From<&str> for RegexParts {
fn from(s: &str) -> Self {
s.to_string().into()
}
}
impl From<String> for RegexParts {
fn from(value: String) -> Self {
Self {
pattern: value,
config: Default::default()
}
}
}
impl Suitability for RegexParts {
fn assert_suitability(&self, config: &Config) {
self.build().unwrap_or_else(|_| panic!("Regex to be buildable: {self:?}"));
self.config.assert_suitability(config);
}
}
impl TryFrom<&RegexParts> for Regex {
type Error = regex::Error;
fn try_from(value: &RegexParts) -> Result<Self, Self::Error> {
value.build()
}
}
impl TryFrom<RegexParts> for Regex {
type Error = regex::Error;
fn try_from(value: RegexParts) -> Result<Self, Self::Error> {
(&value).try_into()
}
}
/// Configuration given to [`RegexBuilder`].
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Suitability)]
pub struct RegexConfig {
/// The value passed to [`RegexBuilder::case_insensitive`].
///
/// The character for this flag is `i`
///
/// Defaults to [`false`].
#[serde(default , skip_serializing_if = "is_false")] pub case_insensitive : bool,
/// The value passed to [`RegexBuilder::crlf`].
///
/// The character for this flag is `R`
///
/// Defaults to [`false`].
#[serde(default , skip_serializing_if = "is_false")] pub crlf : bool,
/// The value passed to [`RegexBuilder::dot_matches_new_line`].
///
/// The character for this flag is `s`
///
/// Defaults to [`false`].
#[serde(default , skip_serializing_if = "is_false")] pub dot_matches_new_line: bool,
/// The value passed to [`RegexBuilder::ignore_whitespace`].
///
/// The character for this flag is `x`
///
/// Defaults to [`false`].
#[serde(default , skip_serializing_if = "is_false")] pub ignore_whitespace : bool,
/// The value passed to [`RegexBuilder::line_terminator`].
///
/// Defaults to `b'\n'` (`10`).
#[serde(default = "newline_u8", skip_serializing_if = "is_nlu8" )] pub line_terminator : u8,
/// The value passed to [`RegexBuilder::multi_line`].
///
/// The character for this flag is `m`
///
/// Defaults to [`false`].
#[serde(default , skip_serializing_if = "is_false")] pub multi_line : bool,
/// The value passed to [`RegexBuilder::octal`].
///
/// The character for this flag is `o`
///
/// Defaults to [`false`].
#[serde(default , skip_serializing_if = "is_false")] pub octal : bool,
/// The value passed to [`RegexBuilder::swap_greed`].
///
/// The character for this flag is `U`
///
/// Defaults to [`false`].
#[serde(default , skip_serializing_if = "is_false")] pub swap_greed : bool,
/// The value passed to [`RegexBuilder::unicode`].
///
/// The character for this flag is `u`
///
/// Defaults to [`true`].
#[serde(default = "get_true" , skip_serializing_if = "is_true" )] pub unicode : bool
}
/// Serde helper function.
const fn is_nlu8(x: &u8) -> bool {*x==b'\n'}
/// Serde helper function.
const fn newline_u8() -> u8 {b'\n'}
impl Default for RegexConfig {
fn default() -> Self {
Self {
case_insensitive : false,
crlf : false,
dot_matches_new_line: false,
ignore_whitespace : false,
line_terminator : b'\n',
multi_line : false,
octal : false,
swap_greed : false,
unicode : true
}
}
}
impl RegexConfig {
/// Sets the flags whose characters are in `flags` and unsets the flags whose characters aren't in `flags`.
///
/// Flags do not have to be "in order", as returned by [`Self::get_flags`].
/// # Examples
/// ```
/// # use url_cleaner::glue::*;
/// let mut config = RegexConfig::default();
///
/// config.set_flags("i");
///
/// assert!( config.case_insensitive);
/// assert!(!config.unicode); // Unicode is enabled by default, but was disabled.
/// ```
pub fn set_flags(&mut self, flags: &str) {
self.case_insensitive = flags.contains('i');
self.crlf = flags.contains('R');
self.dot_matches_new_line = flags.contains('s');
self.ignore_whitespace = flags.contains('x');
self.multi_line = flags.contains('m');
self.octal = flags.contains('o');
self.swap_greed = flags.contains('U');
self.unicode = flags.contains('u');
}
/// Sets the flags whose characters are in `flags` and leavs the others unchanged.
///
/// Flags do not have to be "in order", as returned by [`Self::get_flags`].
/// # Examples
/// ```
/// # use url_cleaner::glue::*;
/// let mut config = RegexConfig::default();
///
/// config.add_flags("i");
///
/// assert!(config.case_insensitive);
/// assert!(config.unicode); // Was enabled by default and left unchanged.
/// ```
pub fn add_flags(&mut self, flags: &str) {
if flags.contains('i') {self.case_insensitive =true;}
if flags.contains('R') {self.crlf =true;}
if flags.contains('s') {self.dot_matches_new_line=true;}
if flags.contains('x') {self.ignore_whitespace =true;}
if flags.contains('m') {self.multi_line =true;}
if flags.contains('o') {self.octal =true;}
if flags.contains('U') {self.swap_greed =true;}
if flags.contains('u') {self.unicode =true;}
}
/// Unsets the flags whose characters are in `flags` and leaves the others unchanged.
///
/// Flags do not have to be "in order", as returned by [`Self::get_flags`].
/// # Examples
/// ```
/// # use url_cleaner::glue::*;
/// let mut config = RegexConfig::default();
///
/// config.add_flags("i");
/// config.remove_flags("u");
///
/// assert!( config.case_insensitive); // Set by the call to [`Self::add_flags`] and left unchanged by the call to [`Self::remove_flags`].
/// assert!(!config.unicode); // Set by default, left unchanged by the call to [`Self::add_flags`], and unset by the call to [`Self::remove_flags`].
/// ```
pub fn remove_flags(&mut self, flags: &str) {
if flags.contains('i') {self.case_insensitive =false;}
if flags.contains('R') {self.crlf =false;}
if flags.contains('s') {self.dot_matches_new_line=false;}
if flags.contains('x') {self.ignore_whitespace =false;}
if flags.contains('m') {self.multi_line =false;}
if flags.contains('o') {self.octal =false;}
if flags.contains('U') {self.swap_greed =false;}
if flags.contains('u') {self.unicode =false;}
}
/// Gets the set flags.
///
/// Exact order is not officially stable, but is unlikely to ever be changed from `iRsxmoUu` and VERY unlikely to ever be changed after that.
/// # Examples
/// ```
/// # use url_cleaner::glue::*;
/// let mut config = RegexConfig::default();
///
/// assert_eq!(config.get_flags(), "u");
///
/// config.case_insensitive = true;
///
/// assert_eq!(config.get_flags(), "iu");
/// ```
#[must_use]
pub fn get_flags(&self) -> String {
let mut ret=String::new();
if self.case_insensitive {ret.push('i');}
if self.crlf {ret.push('R');}
if self.dot_matches_new_line{ret.push('s');}
if self.ignore_whitespace {ret.push('x');}
if self.multi_line {ret.push('m');}
if self.octal {ret.push('o');}
if self.swap_greed {ret.push('U');}
if self.unicode {ret.push('u');}
ret
}
}