-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmod.rs
More file actions
125 lines (110 loc) · 3.64 KB
/
mod.rs
File metadata and controls
125 lines (110 loc) · 3.64 KB
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
pub mod v1;
pub mod v2;
use std::str::FromStr;
use regex::Regex;
// Submit an issue if you find this out-of-date!
// And assuming that all vars are distro_ver
const REGEX_REPLACEMENTS: &[(&str, &str)] = &[
// https://endoflife.date/debian
(
"${DEBIAN_CURRENT}",
"(?<distro_ver>bullseye|bookworm|trixie)",
),
// https://endoflife.date/ubuntu (excluding ESM)
("${UBUNTU_LTS}", "(?<distro_ver>jammy|noble|resolute)"),
("${UBUNTU_NONLTS}", "(?<distro_ver>questing)"),
// https://endoflife.date/fedora
("${FEDORA_CURRENT}", "(?<distro_ver>42|43)"),
// CentOS is no longer supported -- this regex is replaced to something that could match nothing
(
"${CENTOS_CURRENT}",
"(?<distro_ver>NONEXISTFILENAMESOITCOULDNEVERMATCHANYTHING)",
),
// https://endoflife.date/rhel (excluding ELCS)
("${RHEL_CURRENT}", "(?<distro_ver>8|9|10)"),
// https://endoflife.date/opensuse
("${OPENSUSE_CURRENT}", "(?<distro_ver>15.6|16.0)"),
// https://endoflife.date/sles
("${SLES_CURRENT}", "(?<distro_ver>15|16)"),
];
/// ExpandedRegex contains inner and rev_inner, and would transparently add '/' before string
/// (and convert regex with ^). A warning would be given if text input contains '/' at front.
#[derive(Debug, Clone)]
pub struct ExpandedRegex {
pub inner: Regex,
/// v1 compatibility field
rev_inner: Regex,
}
impl FromStr for ExpandedRegex {
type Err = regex::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
// If starts with ^ and not ^/, change start matching character from ^ to ^/
let s = if s.starts_with('^') && !s.starts_with("^/") {
&format!("^/{}", &s[1..])
} else {
s
};
let mut s1 = s.to_string();
for (from, to) in REGEX_REPLACEMENTS {
s1 = s1.replace(from, to);
}
let mut s2 = s.to_string();
for (from, _) in REGEX_REPLACEMENTS.iter().rev() {
s2 = s2.replace(from, "(?<distro_ver>.+)");
}
Ok(Self {
inner: Regex::new(&s1)?,
rev_inner: Regex::new(&s2)?,
})
}
}
// Delegate to inner
impl ExpandedRegex {
fn text_transform(text: &str) -> String {
if !text.starts_with('/') {
tracing::warn!(
"(unexpected internal input: string given to match_str shall start with /, anything wrong?)"
);
format!("/{}", text)
} else {
text.to_string()
}
}
pub fn is_match(&self, text: &str) -> bool {
self.inner.is_match(&Self::text_transform(text))
}
/// v1 compatibility method
pub fn is_others_match(&self, text: &str) -> bool {
let text = &Self::text_transform(text);
!self.inner.is_match(text) && self.rev_inner.is_match(text)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Comparison {
Stop,
/// v1 compatibility field
ListOnly,
Ok,
}
pub trait ExclusionManagerTrait: Send + Sync {
fn match_str(&self, text: &str) -> Comparison;
}
pub fn get_exclusion_manager_v2(args: &[String]) -> Box<dyn ExclusionManagerTrait> {
Box::new(v2::ExclusionManager::new(args))
}
pub fn get_exclusion_manager_v1(
exclude: &[ExpandedRegex],
include: &[ExpandedRegex],
) -> Box<dyn ExclusionManagerTrait> {
Box::new(v1::ExclusionManager::new(exclude, include))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_expanded_regex() {
let regex = ExpandedRegex::from_str("^/deb/dists/${DEBIAN_CURRENT}").unwrap();
assert!(regex.is_match("/deb/dists/bookworm/Release"));
assert!(!regex.is_match("/deb/dists/wheezy/Release"));
}
}