From dc92ee12e8d13677c60b0f238118c2259874b37a Mon Sep 17 00:00:00 2001 From: Tarek Date: Wed, 22 May 2024 15:38:19 +0300 Subject: [PATCH] feat(conf): add method to remove subconfiguration - Added `rm_subconf` method to `CLNConf` for removing subconfigurations by path. - Included a test `subconf_add_rm` to validate the addition and removal of subconfigurations. Signed-off-by: Tarek --- conf/src/lib.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/conf/src/lib.rs b/conf/src/lib.rs index cfce51a..a92f272 100644 --- a/conf/src/lib.rs +++ b/conf/src/lib.rs @@ -169,6 +169,18 @@ impl CLNConf { Ok(()) } + pub fn rm_subconf(&mut self, path: &str) -> Result<(), ParsingError> { + if let Some(index) = self.includes.iter().position(|x| x.path == path) { + self.includes.remove(index); + } else { + return Err(ParsingError { + core: 2, + cause: format!("include {path} not found"), + }); + } + Ok(()) + } + pub fn flush(&self) -> Result<(), std::io::Error> { let content = format!("{self}"); let file = File::new(&self.path); @@ -258,6 +270,30 @@ mod tests { cleanup_file(path.as_str()); } + #[test] + fn subconf_add_rm() { + let path = build_file("plugin=foo\nnetwork=bitcoin"); + assert!(path.is_ok()); + let path = path.unwrap(); + let mut conf = CLNConf::new(path.to_string(), false); + let result = conf.parse(); + assert!(result.is_ok(), "{:#?}", result); + assert_eq!(conf.fields.keys().len(), 2); + assert_eq!(conf.includes.len(), 0); + + assert!(conf.fields.contains_key("plugin")); + assert!(conf.fields.contains_key("network")); + + let subconf = CLNConf::new("/some/path".to_string(), false); + assert!(conf.add_subconf(subconf).is_ok()); + assert_eq!(conf.includes.len(), 1); + + assert!(conf.rm_subconf("/some/path").is_ok()); + assert_eq!(conf.includes.len(), 0); + + cleanup_file(path.as_str()); + } + #[test] fn flush_conf_one() { let path = get_conf_path();