Skip to main content

fast_clean/
remover.rs

1use std::cell::RefCell;
2use std::io;
3use std::path::{Path, PathBuf};
4
5pub trait Remover {
6    fn remove(&mut self, path: &Path) -> io::Result<()>;
7}
8
9/// 真正删除文件
10#[allow(unused)]
11pub struct RealRemover{
12    pub(crate) is_skip_dir: bool,
13}
14impl Remover for RealRemover {
15    fn remove(&mut self, path: &Path,) -> io::Result<()> {
16        if path.is_file() {
17            std::fs::remove_file(path)
18        }else {
19            if self.is_skip_dir {
20                return Ok(())
21            }
22            std::fs::remove_dir_all(path)
23        }
24    }
25}
26
27/// MockRemover,用于测试
28pub struct MockRemover {
29    pub removed: RefCell<Vec<PathBuf>>,
30}
31
32impl MockRemover {
33    pub fn new() -> Self {
34        Self {
35            removed: RefCell::new(Vec::new()),
36        }
37    }
38}
39
40impl Remover for MockRemover {
41    fn remove(&mut self, path: &Path) -> io::Result<()> {
42        self.removed.borrow_mut().push(path.to_path_buf());
43        Ok(())
44    }
45}