pub fn clean_empty_message(dry_run: bool) -> &'static str {
if dry_run {
"Would remove: nothing to clean"
} else {
"Nothing to clean"
}
}
pub fn clean_paths_header(dry_run: bool) -> &'static str {
if dry_run { "Would remove:" } else { "Removed:" }
}
pub fn clean_path_line(path: &str) -> String {
format!(" {path}")
}
pub fn clean_result_lines(removed: &[String], dry_run: bool) -> Vec<String> {
if removed.is_empty() {
return vec![clean_empty_message(dry_run).to_string()];
}
let mut lines = Vec::with_capacity(removed.len() + 1);
lines.push(clean_paths_header(dry_run).to_string());
for path in removed {
lines.push(clean_path_line(path));
}
lines
}
pub fn clean_result_text(removed: &[String], dry_run: bool) -> String {
clean_result_lines(removed, dry_run).join("\n")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_messages() {
assert_eq!(clean_empty_message(true), "Would remove: nothing to clean");
assert_eq!(clean_empty_message(false), "Nothing to clean");
assert_eq!(
clean_result_lines(&[], true),
vec!["Would remove: nothing to clean".to_string()]
);
assert_eq!(
clean_result_lines(&[], false),
vec!["Nothing to clean".to_string()]
);
}
#[test]
fn dry_run_and_removed_lists() {
let paths = vec!["a.txt".into(), "dir/b".into()];
let dry = clean_result_lines(&paths, true);
assert_eq!(dry[0], "Would remove:");
assert_eq!(dry[1], " a.txt");
assert_eq!(dry[2], " dir/b");
let done = clean_result_text(&paths, false);
assert!(done.starts_with("Removed:\n"));
assert!(done.contains(" a.txt"));
assert!(done.contains(" dir/b"));
assert_eq!(clean_paths_header(false), "Removed:");
assert_eq!(clean_path_line("x"), " x");
}
}