1 use std::path::PathBuf; 2 3 pub mod cargo_handler; 4 5 pub struct FileUtils; 6 7 impl FileUtils { 8 /// 列出指定目录下的所有文件 9 /// 10 /// ## 参数 11 /// 12 /// - `path` - 指定的目录 13 /// - `ext_name` - 文件的扩展名,如果为None,则列出所有文件 14 /// - `recursive` - 是否递归列出所有文件 list_all_files(path: &PathBuf, ext_name: Option<&str>, recursive: bool) -> Vec<PathBuf>15 pub fn list_all_files(path: &PathBuf, ext_name: Option<&str>, recursive: bool) -> Vec<PathBuf> { 16 let mut queue: Vec<PathBuf> = Vec::new(); 17 let mut result = Vec::new(); 18 queue.push(path.clone()); 19 20 while !queue.is_empty() { 21 let path = queue.pop().unwrap(); 22 let d = std::fs::read_dir(path); 23 if d.is_err() { 24 continue; 25 } 26 let d = d.unwrap(); 27 28 d.for_each(|ent| { 29 if let Ok(ent) = ent { 30 if let Ok(file_type) = ent.file_type() { 31 if file_type.is_file() { 32 if let Some(e) = ext_name { 33 if let Some(ext) = ent.path().extension() { 34 if ext == e { 35 result.push(ent.path()); 36 } 37 } 38 } 39 } else if file_type.is_dir() && recursive { 40 queue.push(ent.path()); 41 } 42 } 43 } 44 }); 45 } 46 47 return result; 48 } 49 } 50