anyhow_std/fs/
direntry.rs

1use crate::fs::Metadata;
2use anyhow::Context;
3use std::fs::FileType;
4use std::ops::Deref;
5
6/// Wraps [std::fs::DirEntry] to provide the path as error context
7#[derive(Debug, derive_more::From, derive_more::Into)]
8pub struct DirEntry {
9    de: std::fs::DirEntry,
10}
11
12impl DirEntry {
13    /// Extend [std::fs::DirEntry::metadata] providing the path in the error context
14    pub fn metadata(&self) -> anyhow::Result<Metadata> {
15        self.de
16            .metadata()
17            .map(|md| Metadata::from((md, self.path())))
18            .with_context(|| format!("while processing path {:?}", self.path().display()))
19    }
20
21    /// Extend [std::fs::DirEntry::file_type] providing the path in the error context
22    pub fn file_type(&self) -> anyhow::Result<FileType> {
23        self.de
24            .file_type()
25            .with_context(|| format!("while processing path {:?}", self.path().display()))
26    }
27}
28
29impl Deref for DirEntry {
30    type Target = std::fs::DirEntry;
31
32    fn deref(&self) -> &Self::Target {
33        &self.de
34    }
35}