zed/crates/util/src/fs.rs
Piotr Osiewicz 34f09bae4f
chore: Clean up util dependencies. (#9247)
This allows this crate to start building sooner + it reduces our total
build graph size by 13 units (1104 -> 1091).

Release Notes:

- N.A
2024-03-13 00:10:49 +01:00

28 lines
932 B
Rust

use std::path::Path;
use crate::ResultExt;
use async_fs as fs;
use futures_lite::StreamExt;
/// Removes all files and directories matching the given predicate
pub async fn remove_matching<F>(dir: &Path, predicate: F)
where
F: Fn(&Path) -> bool,
{
if let Some(mut entries) = fs::read_dir(dir).await.log_err() {
while let Some(entry) = entries.next().await {
if let Some(entry) = entry.log_err() {
let entry_path = entry.path();
if predicate(entry_path.as_path()) {
if let Ok(metadata) = fs::metadata(&entry_path).await {
if metadata.is_file() {
fs::remove_file(&entry_path).await.log_err();
} else {
fs::remove_dir_all(&entry_path).await.log_err();
}
}
}
}
}
}
}