-
Notifications
You must be signed in to change notification settings - Fork 3
/
forbidden_regex.rs
51 lines (44 loc) · 1.23 KB
/
forbidden_regex.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
use svlint::config::ConfigOption;
use svlint::linter::{TextRule, TextRuleEvent, TextRuleResult};
use regex::Regex;
#[derive(Default)]
pub struct ForbiddenRegex {
re: Option<Regex>,
}
impl TextRule for ForbiddenRegex {
fn check(
&mut self,
event: TextRuleEvent,
_option: &ConfigOption,
) -> TextRuleResult {
let line: &str = match event {
TextRuleEvent::StartOfFile => {
return TextRuleResult::Pass;
}
TextRuleEvent::Line(x) => x,
};
if self.re.is_none() {
let r = format!(r"XXX");
self.re = Some(Regex::new(&r).unwrap());
}
let re = self.re.as_ref().unwrap();
let is_match: bool = re.is_match(line);
if is_match {
TextRuleResult::Fail {
offset: 0,
len: line.len(),
}
} else {
TextRuleResult::Pass
}
}
fn name(&self) -> String {
String::from("forbidden_regex")
}
fn hint(&self, _option: &ConfigOption) -> String {
String::from("Remove the string 'XXX' from all lines.")
}
fn reason(&self) -> String {
String::from("XXX is not meaningful enough.")
}
}