fs_err/path.rs
1#[allow(unused_imports)]
2use crate::errors::{Error, ErrorKind};
3use std::fs;
4use std::io;
5use std::path::{Path, PathBuf};
6
7/// Defines aliases on [`Path`](https://doc.rust-lang.org/std/path/struct.Path.html) for `fs_err` functions.
8///
9/// This trait is sealed and can not be implemented by other crates.
10//
11// Because no one else can implement it, we can add methods backwards-compatibly.
12pub trait PathExt: crate::Sealed {
13 /// Returns Ok(true) if the path points at an existing entity.
14 ///
15 /// Wrapper for [`Path::try_exists`](https://doc.rust-lang.org/std/path/struct.Path.html#method.try_exists).
16 #[cfg(rustc_1_63)]
17 fn fs_err_try_exists(&self) -> io::Result<bool>;
18 /// Given a path, query the file system to get information about a file, directory, etc.
19 ///
20 /// Wrapper for [`crate::metadata`].
21 fn fs_err_metadata(&self) -> io::Result<fs::Metadata>;
22 /// Query the metadata about a file without following symlinks.
23 ///
24 /// Wrapper for [`crate::symlink_metadata`].
25 fn fs_err_symlink_metadata(&self) -> io::Result<fs::Metadata>;
26 /// Returns the canonical, absolute form of a path with all intermediate components
27 /// normalized and symbolic links resolved.
28 ///
29 /// Wrapper for [`crate::canonicalize`].
30 fn fs_err_canonicalize(&self) -> io::Result<PathBuf>;
31 /// Reads a symbolic link, returning the file that the link points to.
32 ///
33 /// Wrapper for [`crate::read_link`].
34 fn fs_err_read_link(&self) -> io::Result<PathBuf>;
35 /// Returns an iterator over the entries within a directory.
36 ///
37 /// Wrapper for [`crate::read_dir`].
38 fn fs_err_read_dir(&self) -> io::Result<crate::ReadDir>;
39}
40
41impl PathExt for Path {
42 #[cfg(rustc_1_63)]
43 fn fs_err_try_exists(&self) -> io::Result<bool> {
44 self.try_exists()
45 .map_err(|source| Error::build(source, ErrorKind::FileExists, self))
46 }
47
48 fn fs_err_metadata(&self) -> io::Result<fs::Metadata> {
49 crate::metadata(self)
50 }
51
52 fn fs_err_symlink_metadata(&self) -> io::Result<fs::Metadata> {
53 crate::symlink_metadata(self)
54 }
55
56 fn fs_err_canonicalize(&self) -> io::Result<PathBuf> {
57 crate::canonicalize(self)
58 }
59
60 fn fs_err_read_link(&self) -> io::Result<PathBuf> {
61 crate::read_link(self)
62 }
63
64 fn fs_err_read_dir(&self) -> io::Result<crate::ReadDir> {
65 crate::read_dir(self)
66 }
67}