add namespaces #8

Merged
pleshevskiy merged 4 commits from namespace into main 2022-07-31 00:55:31 +03:00
Showing only changes of commit 2a6eab45c5 - Show all commits

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