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