1use std::fs::{self, File, OpenOptions};
5use std::io;
6use std::path::PathBuf;
7
8#[derive(Clone, Debug)]
11pub struct Spill(pub PathBuf);
12
13impl Spill {
14 pub fn create(&self, tool: &str) -> io::Result<(PathBuf, File)> {
17 fs::create_dir_all(&self.0)?;
18 let mut n = 1_u64;
19 loop {
20 let path = self.0.join(format!("{tool}-{n}.txt"));
21 match OpenOptions::new().write(true).create_new(true).open(&path) {
22 Ok(file) => return Ok((path, file)),
23 Err(error) if error.kind() == io::ErrorKind::AlreadyExists => n += 1,
24 Err(error) => return Err(error),
25 }
26 }
27 }
28}