✨ feat(config): enhance rule management with BTreeMap
- migrate rules from Vec to BTreeMap for efficient access - add rule existence check to prevent duplicates - improve rule listing by iterating over values
This commit is contained in:
committed by
Jeremiah Russell
parent
b637d249a9
commit
7610f2a9dc
@@ -1,4 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
|
collections::BTreeMap,
|
||||||
env,
|
env,
|
||||||
fs::{self, read_to_string},
|
fs::{self, read_to_string},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
@@ -16,12 +17,12 @@ use crate::{Error, MessageAge, Retention};
|
|||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
credentials: Option<String>,
|
credentials: Option<String>,
|
||||||
rules: Vec<EolRule>,
|
rules: BTreeMap<String, EolRule>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let rules = Vec::new();
|
let rules = BTreeMap::new();
|
||||||
|
|
||||||
let mut cfg = Self {
|
let mut cfg = Self {
|
||||||
credentials: Some("credential.json".to_string()),
|
credentials: Some("credential.json".to_string()),
|
||||||
@@ -51,7 +52,15 @@ impl Config {
|
|||||||
|
|
||||||
/// Add a new rule to the rule set by setting the retention age
|
/// Add a new rule to the rule set by setting the retention age
|
||||||
pub fn add_rule(&mut self, retention: Retention) -> &mut Self {
|
pub fn add_rule(&mut self, retention: Retention) -> &mut Self {
|
||||||
let id = if let Some(max) = self.rules.iter().max_by_key(|r| r.id()) {
|
if self
|
||||||
|
.rules
|
||||||
|
.contains_key(retention.age().to_string().as_str())
|
||||||
|
{
|
||||||
|
log::warn!("rule already exists");
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
let id = if let Some((_, max)) = self.rules.iter().max_by_key(|(_, r)| r.id()) {
|
||||||
max.id() + 1
|
max.id() + 1
|
||||||
} else {
|
} else {
|
||||||
1
|
1
|
||||||
@@ -59,7 +68,8 @@ impl Config {
|
|||||||
|
|
||||||
let mut rule = EolRule::new(id);
|
let mut rule = EolRule::new(id);
|
||||||
rule.set_retention(retention);
|
rule.set_retention(retention);
|
||||||
self.rules.push(rule);
|
log::info!("added rule: {rule}");
|
||||||
|
self.rules.insert(rule.retention().to_string(), rule);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,7 +110,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<(), Error> {
|
||||||
for rule in &self.rules {
|
for rule in self.rules.values() {
|
||||||
println!("{rule}");
|
println!("{rule}");
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user