use std::fs;
use std::path::{Path, PathBuf};
use crate::errors::Result;
static mut FILES: Vec<tempfile::NamedTempFile> = vec![];
static mut DIRS: Vec<tempfile::TempDir> = vec![];
fn data_dir() -> Option<PathBuf> {
directories::BaseDirs::new().map(|d| d.data_dir().to_path_buf())
}
pub fn dir() -> Result<PathBuf> {
data_dir()
.map(|p| p.join("cross-rs").join("tmp"))
.ok_or(eyre::eyre!("unable to get data directory"))
}
pub(crate) fn has_tempfiles() -> bool {
unsafe { !FILES.is_empty() || !DIRS.is_empty() }
}
pub(crate) unsafe fn clean() {
FILES.clear();
DIRS.clear();
}
unsafe fn push_tempfile() -> Result<&'static mut tempfile::NamedTempFile> {
let parent = dir()?;
fs::create_dir_all(&parent).ok();
let file = tempfile::NamedTempFile::new_in(&parent)?;
FILES.push(file);
Ok(FILES.last_mut().expect("file list should not be empty"))
}
unsafe fn pop_tempfile() -> Option<tempfile::NamedTempFile> {
FILES.pop()
}
#[derive(Debug)]
pub struct TempFile {
file: &'static mut tempfile::NamedTempFile,
}
impl TempFile {
pub unsafe fn new() -> Result<Self> {
Ok(Self {
file: push_tempfile()?,
})
}
pub fn file(&mut self) -> &mut tempfile::NamedTempFile {
self.file
}
#[must_use]
pub fn path(&self) -> &Path {
self.file.path()
}
}
impl Drop for TempFile {
fn drop(&mut self) {
unsafe {
pop_tempfile();
}
}
}
unsafe fn push_tempdir() -> Result<&'static Path> {
let parent = dir()?;
fs::create_dir_all(&parent).ok();
let dir = tempfile::TempDir::new_in(&parent)?;
DIRS.push(dir);
Ok(DIRS.last().expect("should not be empty").path())
}
unsafe fn pop_tempdir() -> Option<tempfile::TempDir> {
DIRS.pop()
}
#[derive(Debug)]
pub struct TempDir {
path: &'static Path,
}
impl TempDir {
pub unsafe fn new() -> Result<Self> {
Ok(Self {
path: push_tempdir()?,
})
}
#[must_use]
pub fn path(&self) -> &'static Path {
self.path
}
}
impl Drop for TempDir {
fn drop(&mut self) {
unsafe {
pop_tempdir();
}
}
}