diff --git a/src/domain/switch.rs b/src/domain/switch.rs index 7c1df5e..4c54b5d 100644 --- a/src/domain/switch.rs +++ b/src/domain/switch.rs @@ -54,8 +54,10 @@ where let trimmed_line = line.trim_start_matches(['#', ' ']); if should_enable_variable(&choose_sections, &cur_sections) { String::from(trimmed_line) - } else { + } else if should_disable_variable(&choose_sections, &cur_sections) { format!("# {}", trimmed_line) + } else { + line.to_string() } } else { line.to_string() @@ -97,6 +99,19 @@ fn should_enable_variable(choose_sections: &[Section], current_sections: &[Secti } } +fn should_disable_variable(choose_sections: &[Section], current_sections: &[Section]) -> bool { + choose_sections.is_empty() + || choose_sections.iter().any(|s| s.namespace.is_none()) + || !choose_sections + .iter() + .filter(|s| s.namespace.is_some()) + .any(|s| { + current_sections + .iter() + .any(|s2| s.namespace == s2.namespace) + }) +} + #[cfg(test)] mod tests { use super::*; @@ -221,5 +236,13 @@ mod tests { ] )); } + + #[test] + fn should_disable_variables() { + assert!(should_disable_variable( + &[Section::with_namespace("debug", "on")], + &[Section::new("local")] + )); + } } }