#[derive(Debug)]
pub(crate) struct MatchName<'a> {
pub(crate) module: &'a str,
pub(crate) member: &'a str,
}
impl MatchName<'_> {
fn is_match(&self, banned_module: &str) -> bool {
banned_module
.strip_prefix(self.module)
.and_then(|banned_module| banned_module.strip_prefix('.'))
.and_then(|banned_module| banned_module.strip_prefix(self.member))
.is_some_and(str::is_empty)
}
}
#[derive(Debug)]
pub(crate) struct MatchNameOrParent<'a> {
pub(crate) module: &'a str,
}
impl MatchNameOrParent<'_> {
fn is_match(&self, banned_module: &str) -> bool {
if self.module == banned_module {
return true;
}
if self
.module
.strip_prefix(banned_module)
.is_some_and(|suffix| suffix.starts_with('.'))
{
return true;
}
false
}
}
#[derive(Debug)]
pub(crate) enum NameMatchPolicy<'a> {
MatchName(MatchName<'a>),
MatchNameOrParent(MatchNameOrParent<'a>),
}
impl NameMatchPolicy<'_> {
pub(crate) fn find<'a>(&self, banned_modules: impl Iterator<Item = &'a str>) -> Option<String> {
for banned_module in banned_modules {
match self {
NameMatchPolicy::MatchName(matcher) => {
if matcher.is_match(banned_module) {
return Some(banned_module.to_string());
}
}
NameMatchPolicy::MatchNameOrParent(matcher) => {
if matcher.is_match(banned_module) {
return Some(banned_module.to_string());
}
}
}
}
None
}
}