✨ feat(config): enhance rule management and label handling
- change rules storage from BTreeMap<String, EolRule> to BTreeMap<usize, EolRule> - 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
This commit is contained in:
committed by
Jeremiah Russell
parent
0ba04b2077
commit
662d08ba4f
@@ -17,7 +17,7 @@ use crate::{Error, MessageAge, Retention, eol_cmd::EolAction};
|
|||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
credentials: Option<String>,
|
credentials: Option<String>,
|
||||||
rules: BTreeMap<String, EolRule>,
|
rules: BTreeMap<usize, EolRule>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
@@ -57,11 +57,6 @@ impl Config {
|
|||||||
label: Option<&String>,
|
label: Option<&String>,
|
||||||
delete: bool,
|
delete: bool,
|
||||||
) -> &mut Self {
|
) -> &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();
|
let mut current_labels = Vec::new();
|
||||||
for rule in self.rules.values() {
|
for rule in self.rules.values() {
|
||||||
let mut ls = rule.labels().clone();
|
let mut ls = rule.labels().clone();
|
||||||
@@ -88,10 +83,66 @@ impl Config {
|
|||||||
rule.set_command(EolAction::Delete);
|
rule.set_command(EolAction::Delete);
|
||||||
}
|
}
|
||||||
log::info!("added rule: {rule}");
|
log::info!("added rule: {rule}");
|
||||||
self.rules.insert(rule.retention().to_string(), rule);
|
self.rules.insert(rule.id(), rule);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the labels from the rules
|
||||||
|
pub fn labels(&self) -> Vec<String> {
|
||||||
|
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<String, EolRule> {
|
||||||
|
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
|
/// Save the current configuration to the file
|
||||||
pub fn save(&self) -> Result<(), Error> {
|
pub fn save(&self) -> Result<(), Error> {
|
||||||
let home_dir = env::home_dir().unwrap();
|
let home_dir = env::home_dir().unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user