#[cfg(test)]
extern crate uuid;
use std::fs;
use std::ops::Drop;
use std::path::PathBuf;
pub struct DropDir {
path_buf: PathBuf,
}
impl Drop for DropDir {
fn drop(&mut self) {
let result = fs::remove_dir_all(&self.path_buf);
if result.is_err() {
println!(
"Could not delete directory '{}': {}",
self.path_buf.to_string_lossy(),
result.err().unwrap()
);
}
}
}
impl DropDir {
pub fn new(path_buf: PathBuf) -> Result<DropDir, std::io::Error> {
fs::create_dir_all(&path_buf)?;
Ok(DropDir { path_buf })
}
pub fn path(&self) -> PathBuf {
self.path_buf.clone()
}
}
#[cfg(test)]
mod tests {
use std::path::Path;
use uuid::Uuid;
use super::*;
#[test]
fn test() {
let uuid = Uuid::new_v4().to_string();
let path_buf = std::env::temp_dir().join(uuid);
{
let _drop_dir = DropDir::new(path_buf.clone()).unwrap();
assert!(Path::new(&path_buf).exists())
}
assert!(!Path::new(&path_buf).exists())
}
}