From 8586a5b036246b63fa013fb9f77e7060278e5f49 Mon Sep 17 00:00:00 2001 From: postscriptum Date: Mon, 23 Mar 2026 09:05:05 +0200 Subject: [PATCH] rename some members; add documentation --- src/list.rs | 9 ++++++++- src/main.rs | 6 +++--- src/stats.rs | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/list.rs b/src/list.rs index c6e0797..3a3b4da 100644 --- a/src/list.rs +++ b/src/list.rs @@ -16,6 +16,7 @@ pub struct List { } impl List { + /// Build new List object pub async fn from_opt(list: &Vec, cache: Option) -> Result { fn handle(this: &mut HashSet, line: &str) -> Option { if line.starts_with("/") || line.starts_with("#") || line.is_empty() { @@ -73,19 +74,25 @@ impl List { cache, }) } + /// Check if rule is exist in the (allow) index pub async fn any(&self, value: &str) -> bool { self.index.read().await.iter().any(|item| match item { Item::Exact(v) => v == value, Item::Ending(v) => value.ends_with(v), }) } - pub async fn entries(&self) -> u64 { + /// Get total rules from the current session + pub async fn rules(&self) -> u64 { self.index.read().await.len() as u64 } + /// Allow given `rule` + /// * return `false` if the `rule` is exist pub async fn allow(&self, rule: &str) -> Result { self.cache.allow(rule).await?; Ok(self.index.write().await.insert(Item::from_line(rule))) } + /// Block given `rule` + /// * return `false` if the `rule` is not exist pub async fn block(&self, rule: &str) -> Result { self.cache.block(rule).await?; Ok(self.index.write().await.remove(&Item::from_line(rule))) diff --git a/src/main.rs b/src/main.rs index feb733c..24bac33 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ async fn api_allow( totals: &State>, ) -> Result, Status> { let result = list.allow(rule).await; - totals.set_entries(list.entries().await); + totals.set_entries(list.rules().await); info!("Delete `{rule}` from the in-memory rules (operation status: {result:?})"); Ok(Json(result.map_err(|e| { error!("Allow request handle error for `{rule}`: `{e}`"); @@ -48,7 +48,7 @@ async fn api_block( totals: &State>, ) -> Result, Status> { let result = list.block(rule).await; - totals.set_entries(list.entries().await); + totals.set_entries(list.rules().await); info!("Add `{rule}` to the in-memory rules (operation status: {result:?})"); Ok(Json(result.map_err(|e| { error!("Block request handle error for `{rule}`: `{e}`"); @@ -85,7 +85,7 @@ async fn rocket() -> _ { .unwrap(), ); - let totals = Arc::new(Total::with_entries(list.entries().await)); + let totals = Arc::new(Total::with_rules(list.rules().await)); tokio::spawn({ let socks_list = list.clone(); diff --git a/src/stats.rs b/src/stats.rs index 56c09ee..e79e1e6 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -12,7 +12,7 @@ pub struct Total { } impl Total { - pub fn with_entries(entries: u64) -> Self { + pub fn with_rules(entries: u64) -> Self { Self { entries: entries.into(), ..Self::default()