use std::{fs, process};
use std::fmt::Debug;
use std::path::{Path, PathBuf};
use tinyrand::Rand;
use tinyrand_std::thread_rand;
#[derive(Debug)]
pub struct TempPath {
path_buf: PathBuf
}
impl TempPath {
#[allow(clippy::similar_names)]
pub fn with_extension(extension: &str) -> Self {
let path_buf = std::env::temp_dir();
let mut rand = thread_rand();
let pid = process::id();
let tid = thread_id::get();
let random = rand.next_u64();
let path_buf = path_buf.join(format!("test-{pid}-{tid}-{random:X}.{extension}"));
Self { path_buf }
}
}
impl AsRef<Path> for TempPath {
fn as_ref(&self) -> &Path {
&self.path_buf
}
}
impl Drop for TempPath {
fn drop(&mut self) {
let meta = fs::metadata(&self);
if let Ok(meta) = meta {
let _ignore = if meta.is_dir() {
fs::remove_dir(self)
} else {
fs::remove_file(self)
};
}
}
}
#[cfg(test)]
mod tests;