nmd_core/utility/
file_utility.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::fs::{self, File};
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};


/// Return entirely file content 
pub fn read_file_content(file_path_buf: &PathBuf) -> Result<String, io::Error> {

    let mut file = File::open(file_path_buf)?;

    let mut content = String::new();
    file.read_to_string(&mut content)?;

    Ok(content)
}


/// Return true if &str passed is a valid file path
pub fn is_file_path(s: &str) -> bool {
    
    Path::new(s).is_absolute() || Path::new(s).is_relative()
}

pub fn create_directory(path: &PathBuf) -> Result<(), io::Error> {
   fs::create_dir(path)
}

pub fn create_empty_file(file_path: &PathBuf) -> Result<(), io::Error> {

    create_file_with_content(file_path, "")
}

pub fn create_file_with_content(file_path: &PathBuf, content: &str) -> Result<(), io::Error> {

    let mut file = File::create(&file_path)?;

    file.write_all(content.as_bytes())
}

/// Generate a new file name String using passed base and extension arguments
pub fn build_output_file_name(base: &str, ext: Option<&str>) -> String {

    let base: Vec<char> = base.chars()
                                .filter(|c| c.is_alphanumeric() || c.eq(&'_') || c.eq(&'-') || c.eq(&' ') || c.eq(&'.'))
                                .map(|c| c.to_ascii_lowercase())
                                .collect();

    let base = String::from_iter(base);

    let base: Vec<char> = base.trim().chars().map(|c| {
                                    if c.eq(&' ') {
                                        return '-';
                                    }

                                    c
                                })
                                .collect();

    let base = String::from_iter(base);

    if let Some(ext) = ext {

        return format!("{}.{}", base, ext);

    } else {

        return base;
    }
}

pub fn all_files_in_dir(dir_path: &PathBuf, exts: &Vec<String>) -> Result<Vec<PathBuf>, io::Error> {
    if !dir_path.is_dir() {

        let e = format!("{:?} must be a directory", dir_path);

        return Err(io::Error::new(io::ErrorKind::NotFound, e));
    }

    let mut files: Vec<PathBuf> = Vec::new();

    for entry in fs::read_dir(dir_path)? {
        if let Ok(entry) = entry {
            let path = entry.path();
            
            if let Some(extension) = path.extension() {

                for ext in exts {
                    if extension.to_string_lossy().eq(ext) {
                        if let Some(_) = path.file_name() {
                            files.push(path);
                            break;
                        }
                    }
                }
            }
        }
    }   

    Ok(files)
}