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