feat(config): add functionality to set action on rule

- add `set_action_on_rule` function to modify rule action
- allow users to update action (e.g., delete) for specific rules
This commit is contained in:
Jeremiah Russell
2025-10-09 14:02:25 +01:00
committed by Jeremiah Russell
parent 6b4f0bf214
commit fdc36096dc

View File

@@ -11,7 +11,7 @@ mod eol_rule;
use eol_rule::EolRule;
use crate::{Error, MessageAge, Result, Retention, eol_cmd::EolAction};
use crate::{EolAction, Error, MessageAge, Result, Retention};
/// Configuration file for the program
#[derive(Debug, Serialize, Deserialize)]
@@ -85,7 +85,7 @@ impl Config {
rule.add_label(l);
}
if delete {
rule.set_command(EolAction::Delete);
rule.set_command(&EolAction::Delete);
}
log::info!("added rule: {rule}");
self.rules.insert(rule.id().to_string(), rule);
@@ -173,6 +173,18 @@ impl Config {
Ok(())
}
/// Set the action on the rule identified by the id
pub fn set_action_on_rule(&mut self, id: usize, action: &EolAction) -> Result<()> {
let Some(rule) = self.rules.get_mut(id.to_string().as_str()) else {
return Err(Error::RuleNotFound(id));
};
rule.set_command(action);
self.save()?;
println!("Action set to `{action}` on rule `#{id}`");
Ok(())
}
/// Save the current configuration to the file
pub fn save(&self) -> Result<()> {
let home_dir = env::home_dir().unwrap();