From c3138990a824749343f9fe9944671db1a7943ca7 Mon Sep 17 00:00:00 2001 From: Razvan Dimescu Date: Fri, 10 Apr 2026 21:37:24 +0300 Subject: [PATCH] refactor: extract normalize() for domain lowering + dot stripping Co-Authored-By: Claude Opus 4.6 --- src/blocklist.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/blocklist.rs b/src/blocklist.rs index 8ba2fe7..ef865c4 100644 --- a/src/blocklist.rs +++ b/src/blocklist.rs @@ -86,12 +86,11 @@ impl BlocklistStore { return false; } } - let domain = domain.to_lowercase(); - let domain = domain.trim_end_matches('.'); - if Self::find_in_set(domain, &self.allowlist).is_some() { + let domain = Self::normalize(domain); + if Self::find_in_set(&domain, &self.allowlist).is_some() { 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 { @@ -105,10 +104,9 @@ impl BlocklistStore { } } - let domain = domain.to_lowercase(); - let domain = domain.trim_end_matches('.'); + let domain = Self::normalize(domain); - 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 { "exact match in allowlist" } else { @@ -117,7 +115,7 @@ impl BlocklistStore { 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 { "exact match in blocklist" } else { @@ -129,6 +127,10 @@ impl BlocklistStore { 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) -> Option<&'a str> { if set.contains(domain) { return Some(domain); @@ -174,13 +176,11 @@ impl BlocklistStore { } pub fn add_to_allowlist(&mut self, domain: &str) { - let d = domain.to_lowercase(); - self.allowlist.insert(d.trim_end_matches('.').to_string()); + self.allowlist.insert(Self::normalize(domain)); } pub fn remove_from_allowlist(&mut self, domain: &str) -> bool { - let d = domain.to_lowercase(); - self.allowlist.remove(d.trim_end_matches('.')) + self.allowlist.remove(&Self::normalize(domain)) } pub fn allowlist(&self) -> Vec {