2023-02-08 16:10:22 +03:00
|
|
|
use std::{fs, path::Path};
|
2023-02-07 12:06:44 +03:00
|
|
|
|
2023-02-08 16:10:22 +03:00
|
|
|
fn list_files_abs(dir_path: &Path) -> Result<Vec<String>, std::io::Error> {
|
2023-02-07 12:06:44 +03:00
|
|
|
let mut files = Vec::new();
|
2023-02-08 16:10:22 +03:00
|
|
|
if dir_path.is_dir() {
|
|
|
|
for entry in fs::read_dir(dir_path)? {
|
2023-02-07 17:44:02 +03:00
|
|
|
let entry = entry?;
|
2023-02-07 12:06:44 +03:00
|
|
|
let path = entry.path();
|
|
|
|
if path.is_dir() {
|
2023-02-08 16:10:22 +03:00
|
|
|
let mut sub_files = list_files(&path)?;
|
|
|
|
files.append(&mut sub_files);
|
2023-02-07 12:06:44 +03:00
|
|
|
} else {
|
2023-02-08 16:10:22 +03:00
|
|
|
match path.to_str() {
|
|
|
|
Some(path) => files.push(path.to_string()),
|
|
|
|
None => {
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Invalid path",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 12:06:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
files.sort();
|
2023-02-07 17:44:02 +03:00
|
|
|
Ok(files)
|
2023-02-07 12:06:44 +03:00
|
|
|
}
|
2023-02-08 16:10:22 +03:00
|
|
|
|
|
|
|
// Returns an ordered list of relative paths for files inside a directory recursively.
|
|
|
|
pub fn list_files(dir_path: &Path) -> Result<Vec<String>, std::io::Error> {
|
|
|
|
list_files_abs(dir_path).map(|files| {
|
|
|
|
files
|
|
|
|
.iter()
|
|
|
|
.filter_map(|file| {
|
|
|
|
let file_path = Path::new(file);
|
|
|
|
let relative_path = file_path.strip_prefix(dir_path).ok()?;
|
|
|
|
relative_path.to_str().map(|s| s.to_string())
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
}
|