pub fn append_content(existing: &str, append: &str) -> String {
if append.is_empty() {
return existing.to_string();
}
let mut combined = existing.to_string();
if !combined.is_empty() && !combined.ends_with('\n') {
combined.push('\n');
}
combined.push_str(append);
combined
}
pub fn prepend_content(existing: &str, prepend: &str) -> String {
let mut combined = prepend.to_string();
if !combined.is_empty() && !combined.ends_with('\n') && !existing.is_empty() {
combined.push('\n');
}
combined.push_str(existing);
combined
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn append_adds_newline_separator() {
assert_eq!(append_content("existing", "new"), "existing\nnew");
}
#[test]
fn append_no_double_newline() {
assert_eq!(append_content("existing\n", "new"), "existing\nnew");
}
#[test]
fn append_empty_existing() {
assert_eq!(append_content("", "new"), "new");
}
#[test]
fn prepend_adds_newline_separator() {
assert_eq!(prepend_content("existing", "new"), "new\nexisting");
}
#[test]
fn prepend_no_double_newline() {
assert_eq!(prepend_content("existing", "new\n"), "new\nexisting");
}
#[test]
fn prepend_empty_existing() {
assert_eq!(prepend_content("", "new"), "new");
}
#[test]
fn prepend_empty_prepend() {
assert_eq!(prepend_content("existing", ""), "existing");
}
#[test]
fn append_empty_append() {
assert_eq!(append_content("existing", ""), "existing");
}
#[test]
fn append_empty_both() {
assert_eq!(append_content("", ""), "");
}
#[test]
fn prepend_empty_both() {
assert_eq!(prepend_content("", ""), "");
}
#[test]
fn prepend_symmetry_with_append() {
let a = append_content("base", "added");
assert!(a.contains('\n'));
let p = prepend_content("base", "added");
assert!(p.contains('\n'));
}
}