use crate::errors::Result;
use quillmark::Report;
use std::fs;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
pub fn write_stdout(bytes: &[u8]) -> Result<()> {
io::stdout().write_all(bytes)?;
Ok(())
}
pub fn write_file(path: &Path, bytes: &[u8]) -> Result<()> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(path, bytes)?;
Ok(())
}
pub fn print_report(report: &Report) {
println!(
"{}",
serde_json::to_string_pretty(report).expect("a report serializes")
);
}
pub fn page_output_path(path: &Path, page: usize) -> PathBuf {
let stem = path.file_stem().map(|s| s.to_string_lossy().into_owned());
let mut name = match stem {
Some(stem) if !stem.is_empty() => format!("{stem}-{page}"),
_ => page.to_string(),
};
if let Some(ext) = path.extension() {
name.push('.');
name.push_str(&ext.to_string_lossy());
}
path.with_file_name(name)
}