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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
use std::fmt::Display;
use crate::fs::{DirItem, DirItemIterator, Result};
use super::{DirHandle, Error, PathHandle, PathMarker};
impl DirHandle {
/// Creates a new, empty directory at the path
///
/// Wrapper for [std::fs::create_dir]
///
/// ```
/// # fn try_main() -> kxio::fs::Result<()> {
/// let fs = kxio::fs::temp()?;
/// let path = fs.base().join("foo");
/// let dir = fs.dir(&path);
/// dir.create()?;
/// # Ok(())
/// # }
/// ```
#[tracing::instrument]
pub fn create(&self) -> Result<()> {
self.check_error()?;
tracing::debug!("std::fs::create_dir");
std::fs::create_dir(self.as_pathbuf()).map_err(Error::Io)
}
/// Recursively create a directory and all of its parent components if they are missing.
///
/// Wrapper for [std::fs::create_dir_all]
///
/// ```
/// # fn try_main() -> kxio::fs::Result<()> {
/// let fs = kxio::fs::temp()?;
/// let path = fs.base().join("foo").join("bar");
/// let dir = fs.dir(&path);
/// dir.create_all()?;
/// # Ok(())
/// # }
/// ```
#[tracing::instrument]
pub fn create_all(&self) -> Result<()> {
self.check_error()?;
tracing::debug!("std::fs::create_dir_all");
std::fs::create_dir_all(self.as_pathbuf()).map_err(Error::Io)
}
/// Returns an iterator over the entries within a directory.
///
/// Wrapper for [std::fs::read_dir]
///
/// ```
/// # fn try_main() -> kxio::fs::Result<()> {
/// let fs = kxio::fs::temp()?;
/// let path = fs.base().join("foo").join("bar");
/// let dir = fs.dir(&path);
/// for entry in dir.read()? { /* ... */ }
/// # Ok(())
/// # }
/// ```
#[tracing::instrument]
pub fn read(&self) -> Result<Box<dyn Iterator<Item = Result<DirItem>>>> {
self.check_error()?;
tracing::debug!("std::fs::read_dir");
let read_dir = std::fs::read_dir(self.as_pathbuf()).map_err(Error::Io)?;
Ok(Box::new(DirItemIterator::new(read_dir)))
}
/// Removes an empty directory.
///
/// Wrapper for [std::fs::remove_dir]
///
/// ```
/// # fn try_main() -> kxio::fs::Result<()> {
/// let fs = kxio::fs::temp()?;
/// let path = fs.base().join("foo").join("bar");
/// let dir = fs.dir(&path);
/// dir.remove()?;
/// # Ok(())
/// }
#[tracing::instrument]
pub fn remove(&self) -> Result<()> {
self.check_error()?;
tracing::debug!("std::fs::remove");
std::fs::remove_dir(self.as_pathbuf()).map_err(Error::Io)
}
/// Recursively remove a directory and all of its contents.
///
/// Wrapper for [std::fs::remove_dir_all]
///
/// ```
/// # fn try_main() -> kxio::fs::Result<()> {
/// let fs = kxio::fs::temp()?;
/// let path = fs.base().join("foo").join("bar");
/// let dir = fs.dir(&path);
/// dir.remove_all()?;
/// # Ok(())
/// }
#[tracing::instrument]
pub fn remove_all(&self) -> Result<()> {
self.check_error()?;
tracing::debug!("std::fs::remove_all");
std::fs::remove_dir_all(self.as_pathbuf()).map_err(Error::Io)
}
}
impl TryFrom<PathHandle<PathMarker>> for DirHandle {
type Error = crate::fs::Error;
fn try_from(path: PathHandle<PathMarker>) -> std::result::Result<Self, Self::Error> {
match path.as_dir() {
Ok(Some(dir)) => Ok(dir.clone()),
Ok(None) => Err(crate::fs::Error::NotADirectory {
path: path.as_pathbuf(),
}),
Err(err) => Err(err),
}
}
}
impl Display for DirHandle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}/", self.as_pathbuf().display())
}
}