Skip to content

Commit

Permalink
feat(conf): add method to remove subconfiguration
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
tareknaser committed May 22, 2024
1 parent 56b385f commit dc92ee1
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions conf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit dc92ee1

Please sign in to comment.