From e5268e49f10a4ac968196b1ebac6d85d10bef09f Mon Sep 17 00:00:00 2001 From: postscriptum Date: Sun, 22 Mar 2026 10:06:26 +0200 Subject: [PATCH] reorganize List from enum to struct; separate features to children mods --- src/list.rs | 59 +++++++++++++++--------------------------------- src/list/item.rs | 19 ++++++++++++++++ 2 files changed, 37 insertions(+), 41 deletions(-) create mode 100644 src/list/item.rs diff --git a/src/list.rs b/src/list.rs index 733b5a9..8ff493c 100644 --- a/src/list.rs +++ b/src/list.rs @@ -1,28 +1,13 @@ +mod item; + use anyhow::Result; +use item::Item; use log::*; use std::collections::HashSet; use tokio::sync::RwLock; -#[derive(PartialEq, Eq, Hash)] -pub enum Item { - Ending(String), - Exact(String), -} - -impl Item { - pub fn from_line(rule: &str) -> Self { - if let Some(item) = rule.strip_prefix(".") { - debug!("Add `{rule}` to the whitelist"); - Self::Ending(item.to_string()) - } else { - debug!("Add `{rule}` exact match to the whitelist"); - Self::Exact(rule.to_string()) - } - } -} - -pub enum List { - Allow(RwLock>), +pub struct List { + index: RwLock>, } impl List { @@ -45,34 +30,26 @@ impl List { } } info!("Total whitelist entries parsed: {}", this.len()); - Ok(Self::Allow(RwLock::new(this))) + Ok(Self { + index: RwLock::new(this), + }) } pub async fn any(&self, values: &[&str]) -> bool { - match self { - Self::Allow(list) => { - let guard = list.read().await; - values.iter().any(|&value| { - guard.iter().any(|item| match item { - Item::Exact(v) => v == value, - Item::Ending(v) => value.ends_with(v), - }) - }) - } - } + let guard = self.index.read().await; + values.iter().any(|&value| { + guard.iter().any(|item| match item { + Item::Exact(v) => v == value, + Item::Ending(v) => value.ends_with(v), + }) + }) } pub async fn entries(&self) -> u64 { - match self { - Self::Allow(list) => list.read().await.len() as u64, - } + self.index.read().await.len() as u64 } pub async fn allow(&self, rule: &str) -> bool { - match self { - List::Allow(list) => list.write().await.insert(Item::from_line(rule)), - } + self.index.write().await.insert(Item::from_line(rule)) } pub async fn block(&self, rule: &str) -> bool { - match self { - List::Allow(list) => list.write().await.remove(&Item::from_line(rule)), - } + self.index.write().await.remove(&Item::from_line(rule)) } } diff --git a/src/list/item.rs b/src/list/item.rs new file mode 100644 index 0000000..9e7093b --- /dev/null +++ b/src/list/item.rs @@ -0,0 +1,19 @@ +use log::debug; + +#[derive(PartialEq, Eq, Hash)] +pub enum Item { + Ending(String), + Exact(String), +} + +impl Item { + pub fn from_line(rule: &str) -> Self { + if let Some(item) = rule.strip_prefix(".") { + debug!("Add `{rule}` to the whitelist"); + Self::Ending(item.to_string()) + } else { + debug!("Add `{rule}` exact match to the whitelist"); + Self::Exact(rule.to_string()) + } + } +}