2023-10-14 22:39:48 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2023-10-14 22:39:48 +02:00
|
|
|
|
2023-10-14 22:39:44 +02:00
|
|
|
use crate::CONFIG;
|
2023-10-14 22:39:43 +02:00
|
|
|
|
2023-10-14 22:39:49 +02:00
|
|
|
#[derive(Clone)]
|
2023-10-14 22:39:43 +02:00
|
|
|
pub struct TempDir {
|
|
|
|
path: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TempDir {
|
|
|
|
pub fn new(path: PathBuf) -> TempDir {
|
2023-10-14 22:39:48 +02:00
|
|
|
let pbs: String = path.to_str().unwrap().into();
|
|
|
|
let path = expanduser::expanduser(&pbs).unwrap();
|
2023-10-14 22:39:43 +02:00
|
|
|
if !path.exists() {
|
|
|
|
std::fs::create_dir_all(&path).unwrap();
|
|
|
|
}
|
2023-10-14 22:39:44 +02:00
|
|
|
TempDir { path }
|
2023-10-14 22:39:43 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 22:39:48 +02:00
|
|
|
pub fn path(&self) -> PathBuf {
|
|
|
|
self.path.clone()
|
|
|
|
}
|
|
|
|
|
2023-10-14 22:39:43 +02:00
|
|
|
pub fn push<S: Into<String>>(&mut self, path: S) {
|
2023-10-14 22:39:49 +02:00
|
|
|
let mut path_str = path.into();
|
|
|
|
if path_str.starts_with('/') {
|
|
|
|
path_str = path_str[1..].to_string();
|
|
|
|
}
|
|
|
|
self.path.push(path_str);
|
2023-10-14 22:39:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TempDir {
|
|
|
|
fn default() -> TempDir {
|
|
|
|
TempDir::new({
|
2023-10-14 22:39:44 +02:00
|
|
|
if let Some(d) = CONFIG.with(|c| c.tmp_dir.clone()) {
|
|
|
|
PathBuf::from(d)
|
|
|
|
} else {
|
|
|
|
PathBuf::from("/tmp/pkgr")
|
|
|
|
}
|
2023-10-14 22:39:43 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<PathBuf> for TempDir {
|
|
|
|
fn into(self) -> PathBuf {
|
|
|
|
self.path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<String> for TempDir {
|
|
|
|
fn into(self) -> String {
|
|
|
|
self.path.to_str().unwrap().to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for TempDir {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
self.path.to_str().unwrap().to_string()
|
|
|
|
}
|
2023-10-14 22:39:44 +02:00
|
|
|
}
|