1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use crate::fs::Metadata;
use anyhow::Context;
use std::fs::FileType;
use std::ops::Deref;

/// Wraps [std::fs::DirEntry] to provide the path as error context
#[derive(Debug, derive_more::From, derive_more::Into)]
pub struct DirEntry {
    de: std::fs::DirEntry,
}

impl DirEntry {
    /// Extend [std::fs::DirEntry::metadata] providing the path in the error context
    pub fn metadata(&self) -> anyhow::Result<Metadata> {
        self.de
            .metadata()
            .map(|md| Metadata::from((md, self.path())))
            .with_context(|| format!("while processing path {:?}", self.path().display()))
    }

    /// Extend [std::fs::DirEntry::file_type] providing the path in the error context
    pub fn file_type(&self) -> anyhow::Result<FileType> {
        self.de
            .file_type()
            .with_context(|| format!("while processing path {:?}", self.path().display()))
    }
}

impl Deref for DirEntry {
    type Target = std::fs::DirEntry;

    fn deref(&self) -> &Self::Target {
        &self.de
    }
}