gitbutler/src-tauri/src/fs.rs

24 lines
690 B
Rust
Raw Normal View History

2023-03-14 10:51:22 +03:00
use std::path::{Path, PathBuf};
2023-02-07 12:06:44 +03:00
use walkdir::WalkDir;
// Returns an ordered list of relative paths for files inside a directory recursively.
2023-03-14 10:51:22 +03:00
pub fn list_files<P: AsRef<Path>>(dir_path: P) -> Result<Vec<PathBuf>, std::io::Error> {
let mut files = vec![];
2023-03-14 10:51:22 +03:00
let dir_path = dir_path.as_ref();
if !dir_path.exists() {
return Ok(files);
}
for entry in WalkDir::new(dir_path) {
let entry = entry?;
if entry.file_type().is_file() {
2023-02-07 12:06:44 +03:00
let path = entry.path();
let path = path.strip_prefix(dir_path).unwrap();
2023-03-14 10:51:22 +03:00
let path = path.to_path_buf();
files.push(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
}