add disable variable util

This commit is contained in:
Dmitriy Pleshevskiy 2022-07-31 00:17:46 +03:00
parent 8fed9f979b
commit 2a6eab45c5
Signed by: pleshevskiy
GPG Key ID: 1B59187B161C0215
1 changed files with 24 additions and 1 deletions

View File

@ -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")]
));
}
}
}