pub(crate) const MARKER_START: &str = "# TR-300 Machine Report";
pub(crate) const MARKER_END: &str = "# End TR-300";
#[allow(dead_code)]
pub(crate) const ALIAS_NAME: &str = "report";
#[allow(dead_code)]
pub(crate) const BINARY_NAME: &str = "tr300";
#[allow(dead_code)]
pub(crate) const AUTORUN_SENTINEL_VAR: &str = "TR300_AUTORUN_RAN";
pub(crate) fn remove_delimited_block<'a>(
lines: &[&'a str],
start: &str,
end: &str,
) -> Vec<&'a str> {
let mut result = Vec::new();
let mut in_block = false;
for line in lines {
if line.contains(start) {
in_block = true;
continue;
}
if line.contains(end) {
in_block = false;
continue;
}
if !in_block {
result.push(*line);
}
}
result
}
#[cfg(test)]
mod tests {
use super::{remove_delimited_block, MARKER_END, MARKER_START};
#[test]
fn removes_a_single_well_formed_block() {
let lines: Vec<&str> = vec![
"export FOO=bar",
"",
MARKER_START,
"alias report='tr300'",
MARKER_END,
"alias ll='ls -la'",
];
let kept = remove_delimited_block(&lines, MARKER_START, MARKER_END);
assert_eq!(kept, vec!["export FOO=bar", "", "alias ll='ls -la'"]);
}
#[test]
fn removes_two_blocks() {
let lines: Vec<&str> = vec![
MARKER_START,
"block A",
MARKER_END,
"between",
MARKER_START,
"block B",
MARKER_END,
];
let kept = remove_delimited_block(&lines, MARKER_START, MARKER_END);
assert_eq!(kept, vec!["between"]);
}
#[test]
fn passes_through_a_clean_file() {
let lines: Vec<&str> = vec!["export FOO=bar", "alias ll='ls -la'"];
let kept = remove_delimited_block(&lines, MARKER_START, MARKER_END);
assert_eq!(kept, vec!["export FOO=bar", "alias ll='ls -la'"]);
}
}