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
pub mod action;
use self::action::{Action, Extract, Find, Replace};
use super::transform::field::TransformField;
use crate::{
action::{
filter::Filter,
transform::{field::Field, result::TransformResult},
},
entry::Entry,
error::transform::RegexError,
};
use ExtractionResult::{Extracted, Matched, NotMatched};
use std::{borrow::Cow, convert::Infallible};
#[allow(missing_docs)]
#[derive(Debug)]
pub struct Regex<A> {
pub re: regex::Regex,
pub action: A,
}
impl<A: Action> Regex<A> {
pub fn new(re: &str, action: A) -> Result<Self, RegexError> {
Ok(Self {
re: regex::Regex::new(re)?,
action,
})
}
}
impl Regex<Extract> {
#[must_use]
pub fn extract<'a>(&self, text: &'a str) -> Option<&'a str> {
match find(&self.re, text) {
Extracted(s) => Some(s),
Matched | NotMatched => None,
}
}
}
impl TransformField for Regex<Extract> {
type Error = RegexError;
fn transform_field(&self, field: Option<&str>) -> Result<TransformResult<String>, RegexError> {
let field = match field {
Some(v) => v,
None => return Ok(TransformResult::Old(None)),
};
let transformed = match self.extract(field) {
Some(s) => s,
None if self.action.passthrough_if_not_found => field,
None => return Err(RegexError::CaptureGroupMissing),
};
Ok(TransformResult::New(Some(transformed.to_owned())))
}
}
impl Filter for Regex<Find> {
fn filter(&self, entries: &mut Vec<Entry>) {
entries.retain(|ent| {
let s = match self.action.in_field {
Field::Title => ent.msg.title.as_deref().map(Cow::Borrowed),
Field::Body => ent.msg.body.as_deref().map(Cow::Borrowed),
Field::Link => ent.msg.link.as_ref().map(|s| Cow::Owned(s.to_string())),
};
match s {
None => false,
Some(s) => match find(&self.re, &s) {
Matched | Extracted(_) => true,
NotMatched => false,
},
}
});
}
}
impl Regex<Replace> {
#[must_use]
pub fn replace<'a>(&self, text: &'a str) -> Cow<'a, str> {
self.re.replace(text, &self.action.with)
}
}
impl TransformField for Regex<Replace> {
type Error = Infallible;
fn transform_field(&self, field: Option<&str>) -> Result<TransformResult<String>, Self::Error> {
Ok(TransformResult::New(
field.map(|field| self.replace(field).into_owned()),
))
}
}
#[derive(Debug)]
pub(crate) enum ExtractionResult<'a> {
NotMatched,
Matched,
Extracted(&'a str),
}
pub(crate) fn find<'a>(re: ®ex::Regex, text: &'a str) -> ExtractionResult<'a> {
match re.captures(text) {
Some(capture_groups) => match capture_groups.name("s") {
Some(s) => ExtractionResult::Extracted(s.as_str()),
None => ExtractionResult::Matched,
},
None => ExtractionResult::NotMatched,
}
}
#[allow(clippy::unwrap_used)]
#[allow(unused)]
#[cfg(test)]
mod tests {
use super::action::*;
use super::*;
use assert_matches::assert_matches;
}