From e24d8b1e475499f5b9e62252a6983fa86738ea83 Mon Sep 17 00:00:00 2001 From: Jeremiah Russell Date: Wed, 8 Oct 2025 17:13:48 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(config):=20use=20?= =?UTF-8?q?string=20keys=20for=20rules=20in=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - changed the type of the key used in the rules BTreeMap from usize to String - this change makes it easier to serialize and deserialize the configuration - updated the remove_rule_by_id and remove_rule_by_label functions to use string keys --- src/config.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index 55488c9..d3de420 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,7 +17,7 @@ use crate::{Error, MessageAge, Retention, eol_cmd::EolAction}; #[derive(Debug, Serialize, Deserialize)] pub struct Config { credentials: Option, - rules: BTreeMap, + rules: BTreeMap, } impl Default for Config { @@ -83,7 +83,7 @@ impl Config { rule.set_command(EolAction::Delete); } log::info!("added rule: {rule}"); - self.rules.insert(rule.id(), rule); + self.rules.insert(rule.id().to_string(), rule); self } @@ -108,7 +108,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); + self.rules.remove(&id.to_string()); Ok(()) } @@ -126,7 +126,7 @@ impl Config { return Err(Error::NoRuleFoundForLabel(label.to_string())); } - self.rules.remove(&rule_id); + self.rules.remove(&rule_id.to_string()); Ok(()) } @@ -150,8 +150,12 @@ impl Config { .join(home_dir) .join(".cull-gmail/cull-gmail.toml"); - if let Ok(output) = toml::to_string(self) { - fs::write(path, output)?; + let res = toml::to_string(self); + log::trace!("toml conversion result: {res:#?}"); + + if let Ok(output) = res { + fs::write(&path, output)?; + log::trace!("Config saved to {}", path.display()); } Ok(()) @@ -163,6 +167,7 @@ impl Config { let path = PathBuf::new() .join(home_dir) .join(".cull-gmail/cull-gmail.toml"); + log::trace!("Loading config from {}", path.display()); let input = read_to_string(path)?; let config = toml::from_str::(&input)?;