refactor: extract normalize() for domain lowering + dot stripping

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Razvan Dimescu
2026-04-10 21:37:24 +03:00
parent e5c6caba1f
commit c3138990a8

View File

@@ -86,12 +86,11 @@ impl BlocklistStore {
return false; return false;
} }
} }
let domain = domain.to_lowercase(); let domain = Self::normalize(domain);
let domain = domain.trim_end_matches('.'); if Self::find_in_set(&domain, &self.allowlist).is_some() {
if Self::find_in_set(domain, &self.allowlist).is_some() {
return false; return false;
} }
Self::find_in_set(domain, &self.domains).is_some() Self::find_in_set(&domain, &self.domains).is_some()
} }
pub fn check(&self, domain: &str) -> BlockCheckResult { pub fn check(&self, domain: &str) -> BlockCheckResult {
@@ -105,10 +104,9 @@ impl BlocklistStore {
} }
} }
let domain = domain.to_lowercase(); let domain = Self::normalize(domain);
let domain = domain.trim_end_matches('.');
if let Some(matched) = Self::find_in_set(domain, &self.allowlist) { if let Some(matched) = Self::find_in_set(&domain, &self.allowlist) {
let reason = if matched == domain { let reason = if matched == domain {
"exact match in allowlist" "exact match in allowlist"
} else { } else {
@@ -117,7 +115,7 @@ impl BlocklistStore {
return BlockCheckResult::allowed(matched, reason); return BlockCheckResult::allowed(matched, reason);
} }
if let Some(matched) = Self::find_in_set(domain, &self.domains) { if let Some(matched) = Self::find_in_set(&domain, &self.domains) {
let reason = if matched == domain { let reason = if matched == domain {
"exact match in blocklist" "exact match in blocklist"
} else { } else {
@@ -129,6 +127,10 @@ impl BlocklistStore {
BlockCheckResult::not_blocked() BlockCheckResult::not_blocked()
} }
fn normalize(domain: &str) -> String {
domain.to_lowercase().trim_end_matches('.').to_string()
}
fn find_in_set<'a>(domain: &'a str, set: &HashSet<String>) -> Option<&'a str> { fn find_in_set<'a>(domain: &'a str, set: &HashSet<String>) -> Option<&'a str> {
if set.contains(domain) { if set.contains(domain) {
return Some(domain); return Some(domain);
@@ -174,13 +176,11 @@ impl BlocklistStore {
} }
pub fn add_to_allowlist(&mut self, domain: &str) { pub fn add_to_allowlist(&mut self, domain: &str) {
let d = domain.to_lowercase(); self.allowlist.insert(Self::normalize(domain));
self.allowlist.insert(d.trim_end_matches('.').to_string());
} }
pub fn remove_from_allowlist(&mut self, domain: &str) -> bool { pub fn remove_from_allowlist(&mut self, domain: &str) -> bool {
let d = domain.to_lowercase(); self.allowlist.remove(&Self::normalize(domain))
self.allowlist.remove(d.trim_end_matches('.'))
} }
pub fn allowlist(&self) -> Vec<String> { pub fn allowlist(&self) -> Vec<String> {