From 662d08ba4fb9285c76dac3d92f1cb126e6ac898b Mon Sep 17 00:00:00 2001 From: Jeremiah Russell Date: Wed, 8 Oct 2025 16:14:42 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(config):=20enhance=20rule=20ma?= =?UTF-8?q?nagement=20and=20label=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - change rules storage from BTreeMap to BTreeMap - implement functions to remove rules by ID or label - add helper functions to retrieve labels and rules associated with labels - improve error handling for missing labels or rules --- src/config.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/src/config.rs b/src/config.rs index 5d4f017..55488c9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,7 +17,7 @@ use crate::{Error, MessageAge, Retention, eol_cmd::EolAction}; #[derive(Debug, Serialize, Deserialize)] pub struct Config { credentials: Option, - rules: BTreeMap, + rules: BTreeMap, } impl Default for Config { @@ -57,11 +57,6 @@ impl Config { label: Option<&String>, delete: bool, ) -> &mut Self { - if self.rules.contains_key(&retention.age().to_string()) && label.is_none() { - log::warn!("rule already exists"); - return self; - } - let mut current_labels = Vec::new(); for rule in self.rules.values() { let mut ls = rule.labels().clone(); @@ -88,10 +83,66 @@ impl Config { rule.set_command(EolAction::Delete); } log::info!("added rule: {rule}"); - self.rules.insert(rule.retention().to_string(), rule); + self.rules.insert(rule.id(), rule); self } + /// Get the labels from the rules + pub fn labels(&self) -> Vec { + let mut labels = Vec::new(); + for rule in self.rules.values() { + labels.append(&mut rule.labels().clone()); + } + labels + } + + /// Find the id of the rule that contains a label + fn find_label(&self, label: &str) -> usize { + let rules_by_label = self.get_rules_by_label(); + if let Some(rule) = rules_by_label.get(label) { + rule.id() + } else { + 0 + } + } + + /// Remove a rule by the ID specified + pub fn remove_rule_by_id(&mut self, id: usize) -> crate::Result<()> { + self.rules.remove(&id); + + Ok(()) + } + + /// Remove a rule by the Label specified + pub fn remove_rule_by_label(&mut self, label: &str) -> crate::Result<()> { + let labels = self.labels(); + + if labels.contains(&label.to_string()) { + return Err(Error::LabelNotFoundInRules); + } + + let rule_id = self.find_label(label); + if rule_id == 0 { + return Err(Error::NoRuleFoundForLabel(label.to_string())); + } + + self.rules.remove(&rule_id); + + Ok(()) + } + + fn get_rules_by_label(&self) -> BTreeMap { + let mut rbl = BTreeMap::new(); + + for rule in self.rules.values() { + for label in rule.labels() { + rbl.insert(label, rule.clone()); + } + } + + rbl + } + /// Save the current configuration to the file pub fn save(&self) -> Result<(), Error> { let home_dir = env::home_dir().unwrap();