Skip to main content

fs_err/
dir.rs

1use std::ffi::OsString;
2use std::fs;
3use std::io;
4use std::path::PathBuf;
5
6use crate::errors::{Error, ErrorKind};
7
8#[allow(unused_imports)]
9use crate as fs_err; // for docs
10
11/// Returns an iterator over the entries within a directory.
12///
13/// Wrapper for [`std::fs::read_dir`].
14pub fn read_dir<P: Into<PathBuf>>(path: P) -> io::Result<ReadDir> {
15    let path = path.into();
16
17    match fs::read_dir(&path) {
18        Ok(inner) => Ok(ReadDir { inner, path }),
19        Err(source) => Err(Error::build(source, ErrorKind::ReadDir, path)),
20    }
21}
22
23/// Wrapper around [`std::fs::ReadDir`] which adds more helpful information to
24/// all errors.
25///
26/// This struct is created via [`fs_err::read_dir`].
27#[derive(Debug)]
28pub struct ReadDir {
29    inner: fs::ReadDir,
30    path: PathBuf,
31}
32
33impl Iterator for ReadDir {
34    type Item = io::Result<DirEntry>;
35
36    fn next(&mut self) -> Option<Self::Item> {
37        Some(
38            self.inner
39                .next()?
40                .map_err(|source| Error::build(source, ErrorKind::ReadDir, &self.path))
41                .map(|inner| DirEntry { inner }),
42        )
43    }
44}
45
46/// Wrapper around [`std::fs::DirEntry`] which adds more
47/// helpful information to all errors.
48#[derive(Debug)]
49pub struct DirEntry {
50    inner: fs::DirEntry,
51}
52
53impl DirEntry {
54    /// Returns the full path to the file that this entry represents.
55    ///
56    /// Wrapper for [`std::fs::DirEntry::path`].
57    pub fn path(&self) -> PathBuf {
58        self.inner.path()
59    }
60
61    /// Returns the metadata for the file that this entry points at.
62    ///
63    /// Wrapper for [`std::fs::DirEntry::metadata`].
64    pub fn metadata(&self) -> io::Result<fs::Metadata> {
65        self.inner
66            .metadata()
67            .map_err(|source| Error::build(source, ErrorKind::Metadata, self.path()))
68    }
69
70    /// Returns the file type for the file that this entry points at.
71    ///
72    /// Wrapper for [`std::fs::DirEntry::file_type`].
73    pub fn file_type(&self) -> io::Result<fs::FileType> {
74        self.inner
75            .file_type()
76            .map_err(|source| Error::build(source, ErrorKind::Metadata, self.path()))
77    }
78
79    /// Returns the file name of this directory entry without any leading path component(s).
80    ///
81    /// Wrapper for [`std::fs::DirEntry::file_name`].
82    pub fn file_name(&self) -> OsString {
83        self.inner.file_name()
84    }
85}
86
87#[cfg(unix)]
88mod unix {
89    use std::os::unix::fs::DirEntryExt;
90
91    use super::*;
92
93    impl DirEntryExt for DirEntry {
94        fn ino(&self) -> u64 {
95            self.inner.ino()
96        }
97    }
98}