Skip to content

Commit

Permalink
Make ignore_class_notfound_regexp config fields private
Browse files Browse the repository at this point in the history
Instead of exposing the fields (which must be kept in sync) directly, we
expose a getter & setter pair. The setter ensures that a new RegexSet is
compiled after the internal `ignore_class_notfound_regexp` field is
updated.

We still expose the field read-only to Python through a generated PyO3
getter.
  • Loading branch information
simu committed Feb 20, 2024
1 parent 26ae7b5 commit 7973d58
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
33 changes: 31 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ pub struct Config {
pub compose_node_name: bool,
/// Python Reclass compatibility flags. See `CompatFlag` for available flags.
#[pyo3(get)]
pub ignore_class_notfound_regexp: Vec<String>,
pub ignore_class_notfound_regexset: RegexSet,
ignore_class_notfound_regexp: Vec<String>,
ignore_class_notfound_regexset: RegexSet,
#[pyo3(get)]
pub compatflags: HashSet<CompatFlag>,
}
Expand Down Expand Up @@ -211,6 +211,22 @@ impl Config {
Ok(())
}

/// Returns the currently configured `ignore_class_notfound_regexp` pattern list.
pub fn get_ignore_class_notfound_regexp(&self) -> &Vec<String> {
&self.ignore_class_notfound_regexp
}

/// Updates the saved ignore_class_notfound_regexp pattern list with the provided list and
/// ensures that the precompiled RegexSet is updated to match the new pattern list.
pub fn set_ignore_class_notfound_regexp(&mut self, patterns: Vec<String>) -> Result<()> {
self.ignore_class_notfound_regexp = patterns;
self.compile_ignore_class_notfound_patterns()
}

pub(crate) fn is_class_ignored(&self, cls: &str) -> bool {
self.ignore_class_notfound && self.ignore_class_notfound_regexset.is_match(cls)
}

fn compile_ignore_class_notfound_patterns(&mut self) -> Result<()> {
self.ignore_class_notfound_regexset = RegexSet::new(&self.ignore_class_notfound_regexp)
.map_err(|e| anyhow!("while compiling ignore_class_notfound regex patterns: {e}"))?;
Expand Down Expand Up @@ -315,4 +331,17 @@ mod tests {
assert_eq!(cfg.classes_path, "./inventory/classes");
assert_eq!(cfg.ignore_class_notfound, false);
}

#[test]
fn test_config_update_ignore_class_notfound_patterns() {
let mut cfg = Config::new(Some("./inventory"), None, None, None).unwrap();
assert_eq!(cfg.ignore_class_notfound_regexp, vec![".*"]);

cfg.set_ignore_class_notfound_regexp(vec![".*foo".into(), "bar.*".into()])
.unwrap();

assert!(cfg.ignore_class_notfound_regexset.is_match("thefooer"));
assert!(cfg.ignore_class_notfound_regexset.is_match("baring"));
assert!(!cfg.ignore_class_notfound_regexset.is_match("bazzer"));
}
}
7 changes: 3 additions & 4 deletions src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,17 @@ impl Node {
// ignore_class_notfound_regexp is only applied if ignore_class_notfound == true.
// By default the regexset has a single pattern for .* so that all missing classes are
// ignored.
if r.config.ignore_class_notfound
&& r.config.ignore_class_notfound_regexset.is_match(&cls)
{
if r.config.is_class_ignored(&cls) {
return Ok(None);
}

if r.config.ignore_class_notfound {
// return an error informing the user that we didn't ignore the missing class
// based on the configured regex patterns.
eprintln!(
"Missing class '{cls}' not ignored due to configured regex patterns: [{}]",
r.config
.ignore_class_notfound_regexp
.get_ignore_class_notfound_regexp()
.iter()
.map(|s| format!("'{s}'"))
.collect::<Vec<_>>()
Expand Down

0 comments on commit 7973d58

Please sign in to comment.