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
use std::fs;
pub struct FS {}
#[derive(Debug)]
pub struct File {
pub path: String,
pub metadata: fs::Metadata,
pub file_type: fs::FileType,
}
impl File {
pub fn from_dir_entry(path: String, dir_entry: &fs::DirEntry) -> File {
File {
path,
metadata: dir_entry.metadata().unwrap(),
file_type: dir_entry.file_type().unwrap().clone(),
}
}
}
impl FS {
/// Return a Vec for All files in directory
///
/// # Arguments
///
/// * `path` - Path where the file is located
///
/// # Examples
///
/// ```rust
/// use FS;
///
/// fn (){
/// let resources = FS::get_all_in_directories("resources");
/// }
/// ```
pub fn get_all_in_directories(path: &str) -> Vec<File> {
let directory_root = fs::read_dir(path).unwrap();
let mut directories_dir_entry = Vec::new();
let mut directories: Vec<File> = Vec::new();
for directory in directory_root {
directories_dir_entry.push(directory.unwrap())
}
for directory in &directories_dir_entry {
let path = String::from(directory.path().clone().to_str().unwrap());
directories.push(File::from_dir_entry(path, directory));
}
directories
}
/// Read a file and convert to String
///
/// # Arguments
///
/// * `path` - Path to read file
///
/// # Examples
///
/// ```rust
/// use FS;
///
/// let file = FS::read_to_string("main.js");
/// ```
pub fn read_to_string(path: &str) -> String {
match fs::read_to_string(path) {
Ok(content) => content,
Err(error) => panic!("{}", error),
}
}
}