From 2a6eab45c57be8bbc05d7f63f751ea9287bfa9fd Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sun, 31 Jul 2022 00:17:46 +0300 Subject: [PATCH] add disable variable util --- src/domain/switch.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) 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")] + )); + } } }