1use crate::fs::DirEntry;
2use anyhow::Context;
3use std::path::{Path, PathBuf};
4
5#[derive(Debug, derive_more::From, derive_more::Into)]
7pub struct ReadDir {
8 rd: std::fs::ReadDir,
9 path: PathBuf,
10}
11
12impl ReadDir {
13 pub(crate) fn from_path(p: &Path) -> std::io::Result<Self> {
14 p.read_dir().map(|rd| ReadDir {
15 rd,
16 path: p.to_path_buf(),
17 })
18 }
19}
20
21impl Iterator for ReadDir {
22 type Item = anyhow::Result<DirEntry>;
23
24 fn next(&mut self) -> Option<Self::Item> {
25 wrap_read_dir_item(&self.path, self.rd.next())
26 }
27}
28
29fn wrap_read_dir_item(
30 path: &Path,
31 item: Option<std::io::Result<std::fs::DirEntry>>,
32) -> Option<anyhow::Result<DirEntry>> {
33 item.map(|stditem| {
34 stditem
35 .map(DirEntry::from)
36 .with_context(|| format!("while reading directory {:?}", path.display()))
37 })
38}
39
40#[cfg(test)]
41mod tests;