gitpoulpi_hosting/account/
namespace.rs1use regex::Regex;
13
14#[derive(Debug, Clone)]
16pub struct NamespaceFilter {
17 plain: Vec<String>,
18 regexes: Vec<Regex>,
19}
20
21impl NamespaceFilter {
22 pub fn new(lines: &[String], regex: bool) -> Result<Self, String> {
25 let lines = lines.iter().map(|l| l.trim()).filter(|l| !l.is_empty());
26 if regex {
27 let mut regexes = Vec::new();
28 for line in lines {
29 regexes.push(compile(line)?);
30 }
31 Ok(Self {
32 plain: Vec::new(),
33 regexes,
34 })
35 } else {
36 Ok(Self {
37 plain: lines
38 .map(|l| l.trim_matches('/').to_lowercase())
39 .filter(|l| !l.is_empty())
40 .collect(),
41 regexes: Vec::new(),
42 })
43 }
44 }
45
46 pub fn is_empty(&self) -> bool {
48 self.plain.is_empty() && self.regexes.is_empty()
49 }
50
51 pub fn matches(&self, path: &str) -> bool {
53 if self.is_empty() {
54 return true;
55 }
56 let lower = path.trim_matches('/').to_lowercase();
57 self.plain.iter().any(|ns| {
58 lower == *ns
59 || lower
60 .strip_prefix(ns.as_str())
61 .is_some_and(|rest| rest.starts_with('/'))
62 }) || self.regexes.iter().any(|re| re.is_match(path))
63 }
64}
65
66pub fn compile(line: &str) -> Result<Regex, String> {
68 Regex::new(line).map_err(|e| format!("{line}: {}", headline(&e.to_string())))
69}
70
71fn headline(error: &str) -> String {
73 error
74 .lines()
75 .rev()
76 .find(|l| l.trim_start().starts_with("error:"))
77 .map(|l| {
78 l.trim_start()
79 .trim_start_matches("error:")
80 .trim()
81 .to_string()
82 })
83 .unwrap_or_else(|| error.lines().next().unwrap_or("").to_string())
84}
85
86pub fn first_regex_error(lines: &[String]) -> Option<String> {
88 lines
89 .iter()
90 .map(|l| l.trim())
91 .filter(|l| !l.is_empty())
92 .find_map(|l| compile(l).err())
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 fn lines(items: &[&str]) -> Vec<String> {
100 items.iter().map(|s| s.to_string()).collect()
101 }
102
103 fn plain(items: &[&str]) -> NamespaceFilter {
104 NamespaceFilter::new(&lines(items), false).unwrap()
105 }
106
107 #[test]
108 fn a_plain_namespace_is_a_path_prefix_at_a_slash() {
109 let f = plain(&["ptm"]);
110 assert!(f.matches("ptm/produits/x"));
111 assert!(f.matches("ptm/x"));
112 assert!(f.matches("ptm"));
113 assert!(f.matches("PTM/x"));
114 assert!(!f.matches("ptmx/y"));
115 assert!(!f.matches("formation/ptm/x"));
116 assert!(!f.matches("formation/a"));
117 }
118
119 #[test]
120 fn a_nested_namespace_and_a_full_project_path_match() {
121 let f = plain(&["ptm/produits", "other/repo"]);
122 assert!(f.matches("ptm/produits/x/y"));
123 assert!(f.matches("other/repo"));
124 assert!(!f.matches("ptm/formation/x"));
125 assert!(!f.matches("other/repo2"));
126 assert!(plain(&["/ptm/"]).matches("ptm/x"));
127 }
128
129 #[test]
130 fn several_lines_are_alternatives_and_none_keeps_everything() {
131 let f = plain(&["ptm", "acme"]);
132 assert!(f.matches("acme/a") && f.matches("ptm/b") && !f.matches("formation/c"));
133 let none = plain(&[]);
134 assert!(none.is_empty() && none.matches("anything/at/all"));
135 assert!(plain(&[" ", ""]).is_empty());
136 }
137
138 #[test]
139 fn regex_lines_match_the_full_path_as_written() {
140 let f = NamespaceFilter::new(&lines(&["^ptm/(produits|infra)/", "docs$"]), true).unwrap();
141 assert!(f.matches("ptm/produits/x"));
142 assert!(f.matches("ptm/infra/y"));
143 assert!(f.matches("formation/docs"));
144 assert!(!f.matches("ptm/formation/x"));
145 assert!(!f.matches("formation/docs2"));
146 let loose = NamespaceFilter::new(&lines(&["ptm"]), true).unwrap();
148 assert!(loose.matches("formation/ptmx/y"));
149 }
150
151 #[test]
152 fn an_invalid_regex_is_an_error_naming_the_line_never_a_panic() {
153 let bad = lines(&["^ok/", "(unclosed"]);
154 let err = NamespaceFilter::new(&bad, true).unwrap_err();
155 assert!(err.starts_with("(unclosed: "), "{err}");
156 assert_eq!(first_regex_error(&bad), Some(err));
157 assert_eq!(first_regex_error(&lines(&["^ok/"])), None);
158 assert!(NamespaceFilter::new(&bad, false).is_ok());
160 }
161}