packager/pkgr/src/tmpfs.rs

61 lines
1.3 KiB
Rust

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