✨ feat(config): add label functionality to rules
- add function to add labels to existing rules - implement save function to persist changes to the config file
This commit is contained in:
committed by
Jeremiah Russell
parent
091dc360c6
commit
0a537e4df7
@@ -11,7 +11,7 @@ mod eol_rule;
|
|||||||
|
|
||||||
use eol_rule::EolRule;
|
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
|
/// Configuration file for the program
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
@@ -114,7 +114,7 @@ impl Config {
|
|||||||
/// Remove a rule by the ID specified
|
/// Remove a rule by the ID specified
|
||||||
pub fn remove_rule_by_id(&mut self, id: usize) -> crate::Result<()> {
|
pub fn remove_rule_by_id(&mut self, id: usize) -> crate::Result<()> {
|
||||||
self.rules.remove(&id.to_string());
|
self.rules.remove(&id.to_string());
|
||||||
log::info!("Rule `{id}` has been removed.");
|
println!("Rule `{id}` has been removed.");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,8 +149,20 @@ impl Config {
|
|||||||
rbl
|
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
|
/// 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 home_dir = env::home_dir().unwrap();
|
||||||
let path = PathBuf::new()
|
let path = PathBuf::new()
|
||||||
.join(home_dir)
|
.join(home_dir)
|
||||||
@@ -168,7 +180,7 @@ impl Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Load the current configuration
|
/// Load the current configuration
|
||||||
pub fn load() -> Result<Config, Error> {
|
pub fn load() -> Result<Config> {
|
||||||
let home_dir = env::home_dir().unwrap();
|
let home_dir = env::home_dir().unwrap();
|
||||||
let path = PathBuf::new()
|
let path = PathBuf::new()
|
||||||
.join(home_dir)
|
.join(home_dir)
|
||||||
@@ -190,7 +202,7 @@ impl Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// List the end of life rules set in the configuration
|
/// 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() {
|
for rule in self.rules.values() {
|
||||||
println!("{rule}");
|
println!("{rule}");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user