mithril_client/utils/
fs.rs

1use std::{
2    fs,
3    path::{Path, PathBuf},
4};
5
6use anyhow::anyhow;
7
8use crate::MithrilResult;
9
10/// Create a directory if it does not exist
11pub fn create_directory_if_not_exists(dir: &Path) -> MithrilResult<()> {
12    if dir.exists() {
13        return Ok(());
14    }
15
16    fs::create_dir_all(dir).map_err(|e| anyhow!("Failed creating directory: {e}"))
17}
18
19/// Delete a directory if it exists
20pub fn delete_directory(dir: &Path) -> MithrilResult<()> {
21    if dir.exists() {
22        fs::remove_dir_all(dir).map_err(|e| anyhow!("Failed deleting directory: {e}"))?;
23    }
24
25    Ok(())
26}
27
28/// Read files in a directory
29pub fn read_files_in_directory(dir: &Path) -> MithrilResult<Vec<PathBuf>> {
30    let mut files = vec![];
31    for entry in fs::read_dir(dir)? {
32        let entry = entry?;
33        let path = entry.path();
34        if path.is_file() {
35            files.push(path);
36        }
37    }
38
39    Ok(files)
40}