reorganize List from enum to struct; separate features to children mods

This commit is contained in:
postscriptum 2026-03-22 10:06:26 +02:00
parent 0b6b0ebd0e
commit e5268e49f1
2 changed files with 37 additions and 41 deletions

View file

@ -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<HashSet<Item>>),
pub struct List {
index: RwLock<HashSet<Item>>,
}
impl List {
@ -45,12 +30,12 @@ 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;
let guard = self.index.read().await;
values.iter().any(|&value| {
guard.iter().any(|item| match item {
Item::Exact(v) => v == value,
@ -58,21 +43,13 @@ impl List {
})
})
}
}
}
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))
}
}

19
src/list/item.rs Normal file
View file

@ -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())
}
}
}