use std::{
env::temp_dir,
fs,
io::{BufReader, Result, Write},
path::Path,
};
const INPUT_PATH: &str = "linurgy-input.txt";
const OUTPUT_PATH: &str = "linurgy-output.txt";
fn main() -> Result<()> {
let editor = linurgy::factory::appender("___\n", 2);
let input_path = temp_dir().join(INPUT_PATH);
let output_path = temp_dir().join(OUTPUT_PATH);
let input = "example line\n\nanother line\n";
create_input_file(input, &input_path)?;
let input_file = fs::File::open(&input_path)?;
let mut output_file = fs::File::create(&output_path)?;
let mut input_buf = BufReader::new(input_file);
editor.edit_buffered(&mut input_buf, &mut output_file)?;
let expected = "example line\n\n___\nanother line\n";
assert_and_print(input, expected, &output_path)?;
remove_files(input_buf, output_file, &input_path, &output_path)?;
Ok(())
}
fn create_input_file(input: &str, path: &Path) -> Result<()> {
let mut input_file = fs::File::create(path)?;
input_file.write_all(input.as_bytes())?;
Ok(())
}
fn assert_and_print(input: &str, expected: &str, path: &Path) -> Result<()> {
let output = fs::read_to_string(path)?;
assert_eq!(expected, output);
println!("input:\n{}\noutput:\n{}", input, output);
Ok(())
}
fn remove_files(
input_buf: BufReader<fs::File>,
output_file: fs::File,
input_path: &Path,
output_path: &Path,
) -> Result<()> {
drop(input_buf);
output_file.sync_all()?;
drop(output_file);
fs::remove_file(input_path)?;
fs::remove_file(output_path)?;
Ok(())
}