use regex::Regex;
#[derive(Debug, Clone)]
pub struct NamespaceFilter {
plain: Vec<String>,
regexes: Vec<Regex>,
}
impl NamespaceFilter {
pub fn new(lines: &[String], regex: bool) -> Result<Self, String> {
let lines = lines.iter().map(|l| l.trim()).filter(|l| !l.is_empty());
if regex {
let mut regexes = Vec::new();
for line in lines {
regexes.push(compile(line)?);
}
Ok(Self {
plain: Vec::new(),
regexes,
})
} else {
Ok(Self {
plain: lines
.map(|l| l.trim_matches('/').to_lowercase())
.filter(|l| !l.is_empty())
.collect(),
regexes: Vec::new(),
})
}
}
pub fn is_empty(&self) -> bool {
self.plain.is_empty() && self.regexes.is_empty()
}
pub fn matches(&self, path: &str) -> bool {
if self.is_empty() {
return true;
}
let lower = path.trim_matches('/').to_lowercase();
self.plain.iter().any(|ns| {
lower == *ns
|| lower
.strip_prefix(ns.as_str())
.is_some_and(|rest| rest.starts_with('/'))
}) || self.regexes.iter().any(|re| re.is_match(path))
}
}
pub fn compile(line: &str) -> Result<Regex, String> {
Regex::new(line).map_err(|e| format!("{line}: {}", headline(&e.to_string())))
}
fn headline(error: &str) -> String {
error
.lines()
.rev()
.find(|l| l.trim_start().starts_with("error:"))
.map(|l| {
l.trim_start()
.trim_start_matches("error:")
.trim()
.to_string()
})
.unwrap_or_else(|| error.lines().next().unwrap_or("").to_string())
}
pub fn first_regex_error(lines: &[String]) -> Option<String> {
lines
.iter()
.map(|l| l.trim())
.filter(|l| !l.is_empty())
.find_map(|l| compile(l).err())
}
#[cfg(test)]
mod tests {
use super::*;
fn lines(items: &[&str]) -> Vec<String> {
items.iter().map(|s| s.to_string()).collect()
}
fn plain(items: &[&str]) -> NamespaceFilter {
NamespaceFilter::new(&lines(items), false).unwrap()
}
#[test]
fn a_plain_namespace_is_a_path_prefix_at_a_slash() {
let f = plain(&["orbit"]);
assert!(f.matches("orbit/products/x"));
assert!(f.matches("orbit/x"));
assert!(f.matches("orbit"));
assert!(f.matches("ORBIT/x"));
assert!(!f.matches("orbitx/y"));
assert!(!f.matches("training/orbit/x"));
assert!(!f.matches("training/a"));
}
#[test]
fn a_nested_namespace_and_a_full_project_path_match() {
let f = plain(&["orbit/products", "other/repo"]);
assert!(f.matches("orbit/products/x/y"));
assert!(f.matches("other/repo"));
assert!(!f.matches("orbit/training/x"));
assert!(!f.matches("other/repo2"));
assert!(plain(&["/orbit/"]).matches("orbit/x"));
}
#[test]
fn several_lines_are_alternatives_and_none_keeps_everything() {
let f = plain(&["orbit", "acme"]);
assert!(f.matches("acme/a") && f.matches("orbit/b") && !f.matches("training/c"));
let none = plain(&[]);
assert!(none.is_empty() && none.matches("anything/at/all"));
assert!(plain(&[" ", ""]).is_empty());
}
#[test]
fn regex_lines_match_the_full_path_as_written() {
let f = NamespaceFilter::new(&lines(&["^orbit/(products|infra)/", "docs$"]), true).unwrap();
assert!(f.matches("orbit/products/x"));
assert!(f.matches("orbit/infra/y"));
assert!(f.matches("training/docs"));
assert!(!f.matches("orbit/training/x"));
assert!(!f.matches("training/docs2"));
let loose = NamespaceFilter::new(&lines(&["orbit"]), true).unwrap();
assert!(loose.matches("training/orbitx/y"));
}
#[test]
fn an_invalid_regex_is_an_error_naming_the_line_never_a_panic() {
let bad = lines(&["^ok/", "(unclosed"]);
let err = NamespaceFilter::new(&bad, true).unwrap_err();
assert!(err.starts_with("(unclosed: "), "{err}");
assert_eq!(first_regex_error(&bad), Some(err));
assert_eq!(first_regex_error(&lines(&["^ok/"])), None);
assert!(NamespaceFilter::new(&bad, false).is_ok());
}
}