stop stream

This commit is contained in:
Borodinov Ilya 2024-05-13 22:43:12 +03:00
parent 5fad11ec7b
commit fad33758fe
Signed by: noth
GPG key ID: 75503B2EF596D1BD

View file

@ -1,7 +1,6 @@
use std::path::Path; use std::path::Path;
use crate::ResultExt; use crate::ResultExt;
use futures::TryStreamExt;
use tokio::fs; use tokio::fs;
use tokio_stream::{wrappers::ReadDirStream, StreamExt}; use tokio_stream::{wrappers::ReadDirStream, StreamExt};
@ -10,25 +9,20 @@ pub async fn remove_matching<F>(dir: &Path, predicate: F) -> std::io::Result<()>
where where
F: Fn(&Path) -> bool, F: Fn(&Path) -> bool,
{ {
ReadDirStream::new(fs::read_dir(dir).await?) let mut entries = fs::read_dir(dir).await?;
.try_filter_map(move |entry| {
let predicate = |e| predicate(e); while let Some(entry) = entries.next_entry().await? {
async move { let entry_path = entry.path();
let path = entry.path(); if predicate(entry_path.as_path()) {
if predicate(path.as_path()) { if let Ok(metadata) = fs::metadata(&entry_path).await {
if let Ok(metadata) = fs::metadata(&path).await {
if metadata.is_file() { if metadata.is_file() {
fs::remove_file(&path).await?; fs::remove_file(&entry_path).await?;
} else { } else {
fs::remove_dir_all(&path).await?; fs::remove_dir_all(&entry_path).await?;
} }
} }
Ok(Some(()))
} else {
Ok(None)
} }
} }
})
.try_collect::<()>() Ok(())
.await
} }