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
127
128
129
130
131
132
133
134
135
136
137
use std::fmt::Display;
//
use crate::fs::Result;
use super::{reader::Reader, Error, FileHandle, FileMarker, PathHandle, PathMarker, PathReal};
impl FileHandle {
/// Returns a [Reader] for the file.
///
/// ```
/// # fn try_main() -> kxio::fs::Result<()> {
/// let fs = kxio::fs::temp()?;
/// let path = fs.base().join("foo").join("bar");
/// let file = fs.file(&path);
/// let reader = file.reader()?;
/// # Ok(())
/// # }
/// ```
#[tracing::instrument]
pub fn reader(&self) -> Result<Reader> {
self.check_error()?;
Reader::new(&self.as_pathbuf())
}
/// Writes a slice as the entire contents of a file.
///
/// Wrapper for [std::fs::write]
///
/// ```
/// # fn try_main() -> kxio::fs::Result<()> {
/// let fs = kxio::fs::temp()?;
/// let path = fs.base().join("foo").join("bar");
/// let file = fs.file(&path);
/// file.write("new file contents")?;
/// # Ok(())
/// # }
/// ```
#[tracing::instrument(skip_all)]
pub fn write<C: AsRef<[u8]>>(&self, contents: C) -> Result<()> {
self.check_error()?;
let file = self.as_pathbuf();
tracing::debug!(?file, "std::fs::write");
std::fs::write(file, contents).map_err(Error::Io)
}
/// Copies the contents of a file to another file.
///
/// Wrapper for [std::fs::copy]
///
/// ```
/// # fn try_main() -> kxio::fs::Result<()> {
/// let fs = kxio::fs::temp()?;
/// let src_path = fs.base().join("foo");
/// let dest_path = fs.base().join("bar");
/// let src = fs.file(&src_path);
/// # fs.file(&dest_path).write("new file contents")?;
/// let dest = fs.file(&dest_path);
/// src.copy(&dest)?;
/// # Ok(())
/// # }
/// ```
#[tracing::instrument]
pub fn copy(&self, dest: &FileHandle) -> Result<u64> {
self.check_error()?;
let from = self.as_pathbuf();
let to = dest.as_pathbuf();
tracing::debug!(?from, ?to, "std::fs::copy");
std::fs::copy(from, to).map_err(Error::Io)
}
/// Removes a file.
///
/// Wrapper for [std::fs::remove_file]
///
/// ```
/// # fn try_main() -> kxio::fs::Result<()> {
/// let fs = kxio::fs::temp()?;
/// let path = fs.base().join("foo");
/// let file = fs.file(&path);
/// file.remove()?;
/// # Ok(())
/// # }
/// ```
#[tracing::instrument]
pub fn remove(&self) -> Result<()> {
self.check_error()?;
let file = self.as_pathbuf();
tracing::debug!(?file, "std::fs::remove_file");
std::fs::remove_file(file).map_err(Error::Io)
}
/// Creates a hard link on the filesystem.
///
/// Wrapper for [std::fs::hard_link]
///
/// ```
/// # use kxio::fs::Result;
/// # fn main() -> Result<()> {
/// let fs = kxio::fs::temp()?;
/// let src_path = fs.base().join("foo");
/// let src = fs.file(&src_path);
/// # src.write("bar")?;
/// let dst_path = fs.base().join("bar");
/// let dst = fs.file(&dst_path);
/// src.hard_link(&dst)?;
/// # Ok(())
/// # }
/// ```
#[tracing::instrument]
pub fn hard_link(&self, dest: &FileHandle) -> Result<()> {
self.check_error()?;
let original = self.as_pathbuf();
let link = dest.as_pathbuf();
tracing::debug!(?original, ?link, "std::fs::hard_link");
std::fs::hard_link(original, link).map_err(Error::Io)
}
}
impl TryFrom<PathHandle<PathMarker>> for FileHandle {
type Error = crate::fs::Error;
fn try_from(path: PathHandle<PathMarker>) -> std::result::Result<Self, Self::Error> {
match path.as_file() {
Ok(Some(dir)) => Ok(dir.clone()),
Ok(None) => Err(crate::fs::Error::NotAFile {
path: path.as_pathbuf(),
}),
Err(err) => Err(err),
}
}
}
impl Display for PathReal<FileMarker> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_pathbuf().display())
}
}