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
use std::path::Path; use std::path::PathBuf; use std::collections::HashSet; use walkdir::WalkDir; pub fn list_file_names<P: AsRef<Path>>(path: P) -> Vec<String> { let mut files = Vec::new(); let walk_dir = WalkDir::new(path); for dir_entry in walk_dir.max_depth(1).into_iter() { if dir_entry.is_err() { panic!("{}", dir_entry.err().unwrap()); } let entry = dir_entry.unwrap(); if entry.metadata().unwrap().is_file() { files.push(entry.file_name().to_os_string().into_string().unwrap()); } } files } pub fn list_all<P: AsRef<Path>>(path: P) -> HashSet<String> { let mut dirs = HashSet::new(); let walk_dir = WalkDir::new(path); for dir_entry in walk_dir .min_depth(1) .sort_by(|a, b| a.file_name().cmp(b.file_name())) .into_iter() { if dir_entry.is_err() { panic!("{}", dir_entry.err().unwrap()); } dirs.insert(dir_entry.unwrap().path().display().to_string()); } dirs } pub fn list_sub_dirs<P: AsRef<Path>>(path: P) -> Vec<String> { let mut dirs = Vec::new(); let walk_dir = WalkDir::new(path); for dir_entry in walk_dir .min_depth(1) .max_depth(1) .sort_by(|a, b| a.file_name().cmp(b.file_name())) .into_iter() { if dir_entry.is_err() { panic!("{}", dir_entry.err().unwrap()); } let entry = dir_entry.unwrap(); if entry.metadata().unwrap().is_dir() { dirs.push(entry.path().display().to_string()) } } dirs } pub fn find_in_path(root_path: &str, file: Vec<&str>) -> Option<String> { let all_files = list_all(root_path); let mut parent_path = PathBuf::from(root_path).to_path_buf(); for each_part in file.into_iter() { parent_path.push(each_part); } match all_files.contains(&parent_path.display().to_string()) { true => Some(parent_path.display().to_string()), _ => None, } } pub fn join_path(root_path: &str, file: Vec<&str>) -> String { let mut parent_path = PathBuf::from(root_path).to_path_buf(); for each_part in file.into_iter() { parent_path.push(each_part); } parent_path.display().to_string() }