use std::fs::{self, File, OpenOptions};
use std::io;
use std::path::PathBuf;
#[derive(Clone, Debug)]
pub struct Spill(pub PathBuf);
impl Spill {
pub fn create(&self, tool: &str) -> io::Result<(PathBuf, File)> {
fs::create_dir_all(&self.0)?;
let mut n = 1_u64;
loop {
let path = self.0.join(format!("{tool}-{n}.txt"));
match OpenOptions::new().write(true).create_new(true).open(&path) {
Ok(file) => return Ok((path, file)),
Err(error) if error.kind() == io::ErrorKind::AlreadyExists => n += 1,
Err(error) => return Err(error),
}
}
}
}