|
1 | 1 | #![allow(dead_code)] // not used on all platforms |
2 | 2 |
|
3 | | -use crate::fs; |
| 3 | +use crate::fmt; |
| 4 | +use crate::fs::{self, File, OpenOptions, create_dir, remove_dir, remove_file, rename}; |
4 | 5 | use crate::io::{self, Error, ErrorKind}; |
5 | | -use crate::path::Path; |
| 6 | +use crate::path::{Path, PathBuf}; |
6 | 7 | use crate::sys_common::ignore_notfound; |
7 | 8 |
|
8 | 9 | pub(crate) const NOT_FILE_ERROR: Error = io::const_error!( |
@@ -58,3 +59,52 @@ pub fn exists(path: &Path) -> io::Result<bool> { |
58 | 59 | Err(error) => Err(error), |
59 | 60 | } |
60 | 61 | } |
| 62 | + |
| 63 | +pub struct Dir { |
| 64 | + path: PathBuf, |
| 65 | +} |
| 66 | + |
| 67 | +impl Dir { |
| 68 | + pub fn new<P: AsRef<Path>>(path: P) -> io::Result<Self> { |
| 69 | + Ok(Self { path: path.as_ref().to_path_buf() }) |
| 70 | + } |
| 71 | + |
| 72 | + pub fn new_with<P: AsRef<Path>>(path: P, _opts: &OpenOptions) -> io::Result<Self> { |
| 73 | + Ok(Self { path: path.as_ref().to_path_buf() }) |
| 74 | + } |
| 75 | + |
| 76 | + pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> { |
| 77 | + File::open(self.path.join(path)) |
| 78 | + } |
| 79 | + |
| 80 | + pub fn open_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<File> { |
| 81 | + opts.open(self.path.join(path)) |
| 82 | + } |
| 83 | + |
| 84 | + pub fn create_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> { |
| 85 | + create_dir(self.path.join(path)) |
| 86 | + } |
| 87 | + |
| 88 | + pub fn remove_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> { |
| 89 | + remove_file(self.path.join(path)) |
| 90 | + } |
| 91 | + |
| 92 | + pub fn remove_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> { |
| 93 | + remove_dir(self.path.join(path)) |
| 94 | + } |
| 95 | + |
| 96 | + pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>( |
| 97 | + &self, |
| 98 | + from: P, |
| 99 | + to_dir: &Self, |
| 100 | + to: Q, |
| 101 | + ) -> io::Result<()> { |
| 102 | + rename(self.path.join(from), to_dir.path.join(to)) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +impl fmt::Debug for Dir { |
| 107 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 108 | + f.debug_struct("Dir").field("path", &self.path).finish() |
| 109 | + } |
| 110 | +} |
0 commit comments