From 0a537e4df7e40e126efed60fc98224918cda849a Mon Sep 17 00:00:00 2001 From: Jeremiah Russell Date: Thu, 9 Oct 2025 11:13:51 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(config):=20add=20label=20funct?= =?UTF-8?q?ionality=20to=20rules?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add function to add labels to existing rules - implement save function to persist changes to the config file --- src/config.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index ae6410d..0edc293 100644 --- a/src/config.rs +++ b/src/config.rs @@ -11,7 +11,7 @@ mod eol_rule; use eol_rule::EolRule; -use crate::{Error, MessageAge, Retention, eol_cmd::EolAction}; +use crate::{Error, MessageAge, Result, Retention, eol_cmd::EolAction}; /// Configuration file for the program #[derive(Debug, Serialize, Deserialize)] @@ -114,7 +114,7 @@ impl Config { /// Remove a rule by the ID specified pub fn remove_rule_by_id(&mut self, id: usize) -> crate::Result<()> { self.rules.remove(&id.to_string()); - log::info!("Rule `{id}` has been removed."); + println!("Rule `{id}` has been removed."); Ok(()) } @@ -149,8 +149,20 @@ impl Config { rbl } + /// Add a label to the rule identified by the id + pub fn add_label_to_rule(&mut self, id: usize, label: &str) -> Result<()> { + let Some(rule) = self.rules.get_mut(id.to_string().as_str()) else { + return Err(Error::RuleNotFound(id)); + }; + rule.add_label(label); + self.save()?; + println!("Label `{label}` added to rule `#{id}`"); + + Ok(()) + } + /// Save the current configuration to the file - pub fn save(&self) -> Result<(), Error> { + pub fn save(&self) -> Result<()> { let home_dir = env::home_dir().unwrap(); let path = PathBuf::new() .join(home_dir) @@ -168,7 +180,7 @@ impl Config { } /// Load the current configuration - pub fn load() -> Result { + pub fn load() -> Result { let home_dir = env::home_dir().unwrap(); let path = PathBuf::new() .join(home_dir) @@ -190,7 +202,7 @@ impl Config { } /// List the end of life rules set in the configuration - pub fn list_rules(&self) -> Result<(), Error> { + pub fn list_rules(&self) -> Result<()> { for rule in self.rules.values() { println!("{rule}"); }