#![forbid(unsafe_code)]
use rand::RngCore;
use std::{
fs, io,
path::{Path, PathBuf},
};
#[derive(Debug, PartialEq)]
pub struct TempPath {
path_buf: PathBuf,
persist: bool,
}
impl Drop for TempPath {
fn drop(&mut self) {
if !self.persist {
fs::remove_dir_all(&self.path_buf)
.or_else(|_| fs::remove_file(&self.path_buf))
.unwrap_or(());
}
}
}
impl TempPath {
pub fn new() -> Self { Self::new_with_temp_dir(std::env::temp_dir()) }
pub fn new_with_temp_dir(temp_dir: PathBuf) -> Self {
let mut temppath = temp_dir;
let mut rng = rand::thread_rng();
let mut bytes = [0_u8; 16];
rng.fill_bytes(&mut bytes);
temppath.push(hex::encode(&bytes));
TempPath {
path_buf: temppath,
persist: false,
}
}
pub fn path(&self) -> &Path { &self.path_buf }
pub fn persist(&mut self) { self.persist = true; }
pub fn create_as_file(&self) -> io::Result<()> {
let mut builder = fs::OpenOptions::new();
builder.write(true).create_new(true);
builder.open(self.path())?;
Ok(())
}
pub fn create_as_dir(&self) -> io::Result<()> {
let builder = fs::DirBuilder::new();
builder.create(self.path())?;
Ok(())
}
}
impl std::convert::AsRef<Path> for TempPath {
fn as_ref(&self) -> &Path { self.path() }
}
impl Default for TempPath {
fn default() -> Self { Self::new() }
}