use std::fmt::Display;
use std::io::{Result, Write};
pub fn append_name_value<F: Write, K: Display, V: Display>(
file: &mut F,
name: K,
value: V,
) -> Result<()> {
writeln!(file, "{name}={value}")
}
pub fn append_name_value_ml<F: Write, K: Display, V: Display, D: Display>(
file: &mut F,
name: K,
value: V,
delim: D,
) -> Result<()> {
writeln!(file, "{name}<<{delim}")?;
writeln!(file, "{value}")?;
writeln!(file, "{delim}")
}
pub fn append_path<F: Write, P: Display>(file: &mut F, path: P) -> Result<()> {
writeln!(file, "{path}")
}
pub fn append_summary<F: Write, M: Display>(file: &mut F, markdown: M) -> Result<()> {
writeln!(file, "{markdown}")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn append_line() {
let mut buf = Vec::default();
let res = append_name_value(&mut buf, "test_key", "test_value");
assert!(res.is_ok());
assert_eq!(String::from_utf8(buf).unwrap(), "test_key=test_value\n");
}
#[test]
fn append_multiple_lines() {
let mut buf = Vec::default();
let res = append_name_value_ml(&mut buf, "test_key", "test\nvalue", "EOF");
assert!(res.is_ok());
assert_eq!(
String::from_utf8(buf).unwrap(),
"test_key<<EOF\ntest\nvalue\nEOF\n"
);
}
#[test]
fn append_path_line() {
let mut buf = Vec::default();
let res = append_path(&mut buf, "/home/runner/.local/bin");
assert!(res.is_ok());
assert_eq!(String::from_utf8(buf).unwrap(), "/home/runner/.local/bin\n");
}
#[test]
fn append_summary_markdown() {
let mut buf = Vec::default();
let res = append_summary(&mut buf, "# Hello\n\n- item 1\n- item 2");
assert!(res.is_ok());
assert_eq!(
String::from_utf8(buf).unwrap(),
"# Hello\n\n- item 1\n- item 2\n"
);
}
}