introduce rust

This commit is contained in:
Mikaël Cluseau
2024-04-29 12:54:25 +02:00
parent 6e1cb57e03
commit 73cafb7385
78 changed files with 3408 additions and 2457 deletions

43
src/fs.rs Normal file
View File

@ -0,0 +1,43 @@
use log::warn;
use std::io::Result;
use tokio::fs;
pub async fn walk_dir(dir: &str) -> Vec<String> {
let mut todo = Vec::new();
if let Ok(rd) = read_dir(dir).await {
todo.push(rd);
}
let mut results = Vec::new();
while let Some(rd) = todo.pop() {
for (path, is_dir) in rd {
if is_dir {
let Ok(child_rd) = (read_dir(&path).await)
.inspect_err(|e| warn!("reading dir {path} failed: {e}"))
else {
continue;
};
todo.push(child_rd);
} else {
results.push(path);
}
}
}
results
}
async fn read_dir(dir: &str) -> Result<Vec<(String, bool)>> {
let mut rd = fs::read_dir(dir).await?;
let mut entries = Vec::new();
while let Some(entry) = rd.next_entry().await? {
if let Some(path) = entry.path().to_str() {
entries.push((path.to_string(), entry.file_type().await?.is_dir()));
}
}
entries.sort(); // we want a deterministic & intuitive order
Ok(entries)
}