From d52b580844f12a085489ca2c5cbeb8b086ad7234 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 28 Mar 2026 05:31:23 +0200 Subject: [PATCH] implement list reload method; reload list on enable request --- src/main.rs | 23 +++++++++++++++++++++-- src/rules.rs | 9 +++++---- src/rules/list.rs | 32 ++++++++++++++++++++------------ src/rules/list/source.rs | 9 +++++++++ 4 files changed, 55 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 75adad3..bf47419 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,16 @@ async fn api_list_enable( rules: &State>>, ) -> Result>, Status> { Ok(Json( - rules.write().await.set_enabled(alias, true).map(|old| !old), + rules + .write() + .await + .set_enabled(alias, true) + .await + .map_err(|err| { + error!("List status change error: `{err}`"); + Status::InternalServerError + })? + .map(|old| !old), )) } @@ -43,7 +52,17 @@ async fn api_list_disable( alias: &str, rules: &State>>, ) -> Result>, Status> { - Ok(Json(rules.write().await.set_enabled(alias, false))) + Ok(Json( + rules + .write() + .await + .set_enabled(alias, false) + .await + .map_err(|err| { + error!("List status change error: `{err}`"); + Status::InternalServerError + })?, + )) } #[rocket::launch] diff --git a/src/rules.rs b/src/rules.rs index 38efa71..dc67470 100644 --- a/src/rules.rs +++ b/src/rules.rs @@ -17,10 +17,11 @@ impl Rules { .is_none()) } /// Change rule set status by list ID - pub fn set_enabled(&mut self, list_alias: &str, status: bool) -> Option { - self.0 - .get_mut(list_alias) - .map(|list| list.set_enabled(status)) + pub async fn set_enabled(&mut self, list_alias: &str, status: bool) -> Result> { + Ok(match self.0.get_mut(list_alias) { + Some(list) => Some(list.set_enabled(status).await?), + None => None, + }) } /// Check if rule is exist in the index pub fn any(&self, value: &str) -> bool { diff --git a/src/rules/list.rs b/src/rules/list.rs index 2087333..828378f 100644 --- a/src/rules/list.rs +++ b/src/rules/list.rs @@ -9,8 +9,8 @@ use std::collections::HashSet; pub struct List { pub is_enabled: bool, - //pub source: Source, pub rules: HashSet, + pub source: Source, } impl List { @@ -18,29 +18,37 @@ impl List { let mut rules = HashSet::new(); let source = Source::from_str(src)?; if is_enabled { - for line in source.get().await?.lines() { - if line.starts_with("/") || line.starts_with("#") || line.is_empty() { - continue; // skip comments - } - if !rules.insert(Rule::from_line(line)) { - warn!("List `{src}` contains duplicated entry: `{line}`") - } - } + reload(&mut rules, &source).await?; } Ok(Self { rules, - //source, + source, is_enabled, }) } /// Change rule set status by list ID - pub fn set_enabled(&mut self, is_enabled: bool) -> bool { + pub async fn set_enabled(&mut self, is_enabled: bool) -> Result { let was_enabled = self.is_enabled; self.is_enabled = is_enabled; - was_enabled + if is_enabled { + reload(&mut self.rules, &self.source).await?; + } + Ok(was_enabled) } /// Check if rule is exist in the items index pub fn contains(&self, value: &str) -> bool { self.is_enabled && self.rules.iter().any(|rule| rule.contains(value)) } } +async fn reload(rules: &mut HashSet, source: &Source) -> Result<()> { + rules.clear(); + for line in source.get().await?.lines() { + if line.starts_with("/") || line.starts_with("#") || line.is_empty() { + continue; // skip comments + } + if !rules.insert(Rule::from_line(line)) { + warn!("List `{source}` contains duplicated entry: `{line}`") + } + } + Ok(()) +} diff --git a/src/rules/list/source.rs b/src/rules/list/source.rs index f2e63c6..8eff324 100644 --- a/src/rules/list/source.rs +++ b/src/rules/list/source.rs @@ -38,3 +38,12 @@ impl FromStr for Source { Source::from_str(s) } } + +impl std::fmt::Display for Source { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Path(path) => write!(f, "{}", path.to_string_lossy()), + Self::Url(url) => write!(f, "{url}"), + } + } +}