mithril_client/utils/
fs.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use std::{
    fs,
    path::{Path, PathBuf},
};

use anyhow::anyhow;

use crate::MithrilResult;

/// Create a directory if it does not exist
pub fn create_directory_if_not_exists(dir: &Path) -> MithrilResult<()> {
    if dir.exists() {
        return Ok(());
    }

    fs::create_dir_all(dir).map_err(|e| anyhow!("Failed creating directory: {e}"))
}

/// Delete a directory if it exists
pub fn delete_directory(dir: &Path) -> MithrilResult<()> {
    if dir.exists() {
        fs::remove_dir_all(dir).map_err(|e| anyhow!("Failed deleting directory: {e}"))?;
    }

    Ok(())
}

/// Read files in a directory
pub fn read_files_in_directory(dir: &Path) -> MithrilResult<Vec<PathBuf>> {
    let mut files = vec![];
    for entry in fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();
        if path.is_file() {
            files.push(path);
        }
    }

    Ok(files)
}