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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use crate::error;
use crate::path_stuff;
use fs_extra;
// use serde;
// use serde_json;
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
/// the File struct is a struct to help you work with files
#[derive(Debug, Clone)]
pub struct File {
/// the path of file
pub path: PathBuf,
}
impl AsRef<Path> for File {
fn as_ref(&self) -> &Path {
self.path.as_path()
}
}
// new and static methods and prop-like methods
#[allow(dead_code)]
impl File {
/// creates a new File
/// ```
/// use fs_pro::File;
///
/// let path = File::new("/path/to/path").unwrap();
/// let path = File::new(Path::new("/path/to/path")).unwrap();
/// let path = File::new(PathBuf::from("/path/to/path"));
/// ```
/// # Errors
/// - given path is directory
/// - invalid path
pub fn new<P: AsRef<Path>>(path: P) -> error::Result<File> {
let mut path_buf = PathBuf::new();
path_buf.push(path);
let real_path = path_buf.as_path();
if real_path.exists() && !real_path.is_file() {
Err(error::Error::new(
error::ErrorKind::InvalidFile,
"path suppose to be file found folder",
))
} else {
Ok(File { path: path_buf })
}
}
/// creates a file in the temp directory
/// ```
/// use fs_pro::File;
///
/// let temp_file = Fir::temp_file("name").unwrap();
/// ```
pub fn temp_file<P: AsRef<Path>>(name: P) -> error::Result<File> {
let file = File::temp_file_no_create(name)?;
file.create()?;
Ok(file)
}
/// like `temp_file` but doesn't create file
/// ```
/// use fs_pro::File;
///
/// let temp_file = Fir::temp_file_no_create("name").unwrap();
/// ```
pub fn temp_file_no_create<P: AsRef<Path>>(name: P) -> error::Result<File> {
let mut tmp_dir = std::env::temp_dir();
tmp_dir.push(name);
let file = File::new(tmp_dir)?;
Ok(file)
}
/// create a file in the temp directory with random name
/// ```
/// use fs_pro::File;
///
/// let temp_file = File::temp_file_rand();
/// ```
pub fn temp_file_rand() -> error::Result<File> {
Ok(File::temp_file(path_stuff::get_rand_chars(10))?)
}
/// like `temp_file_rand` but doesn't create file
/// ```
/// use fs_pro::File;
///
/// let temp_file = File::temp_file_rand_no_create();
/// ```
pub fn temp_file_rand_no_create() -> error::Result<File> {
Ok(File::temp_file_no_create(path_stuff::get_rand_chars(10))?)
}
/// gets the parent of the file in &str
/// ```
/// use fs_pro::File;
///
/// let file = File::temp_file_rand().unwrap();
/// assert_eq!(file.parent().unwrap(), "/tmp");
/// ```
pub fn parent(&self) -> error::Result<&str> {
path_stuff::parent(self.path.as_path())
}
/// gets the file name (including extension) in &str
/// ```
/// use fs_pro::File
///
/// let file = File::new("my_file.txt").unwrap();
/// assert_eq!(file.name().unwrap(), "my_file.txt");
/// ```
pub fn name(&self) -> error::Result<&str> {
path_stuff::name(self.path.as_path())
}
/// gets the file name (excluding extension) in &str
/// ```
/// use fs_pro::File
///
/// let file = File::new("my_file.txt").unwrap();
/// assert_eq!(file.name_without_extension().unwrap(), "my_file");
/// ```
pub fn name_without_extension(&self) -> error::Result<&str> {
path_stuff::name_without_extension(self.path.as_path())
}
/// gets the extension of file in &str
/// ```
/// use fs_pro::File
///
/// let file = File::new("my_file.txt").unwrap();
/// assert_eq!(file.extension().unwrap(), "txt");
/// ```
pub fn extension(&self) -> error::Result<&str> {
path_stuff::extension(self.path.as_path())
}
/// parses the file path and returns fs_pro::ParsedPathFile
pub fn parse_path(&self) -> error::Result<path_stuff::ParsedPathFile<'_>> {
path_stuff::parse_path_file(self.path.as_path())
}
/// returns the size of file in bytes
pub fn size(&self) -> error::Result<u64> {
Ok(self.metadata()?.len())
}
}
#[allow(dead_code)]
impl File {
/// return true if the file exists
pub fn exists(&self) -> bool {
self.path.exists()
}
/// create the file if it doesn't exists
pub fn create(&self) -> error::Result<()> {
if !self.exists() {
error::result_from_io(fs::File::create(self.path.as_path()))?;
Ok(())
} else {
Ok(())
}
}
/// creates the directory and it's parent if doesn't exists
pub fn create_all(&self) -> error::Result<()> {
let parent =
error::result_from_option2(self.path.parent(), error::ErrorKind::PathNoParentFound)?;
error::result_from_io(fs::create_dir_all(parent))?;
self.create()?;
Ok(())
}
/// writes to file
/// ```
/// file.write("hi");
/// file.write(vec![10, 100, 100]);
/// ```
pub fn write<C: AsRef<[u8]>>(&self, content: C) -> error::Result<()> {
error::result_from_io(fs::write(self.path.as_path(), content))
}
/// reads the file as Vec<u8>
/// ```
/// file.read() // => [10, 124, ...]
/// ```
pub fn read(&self) -> error::Result<Vec<u8>> {
error::result_from_io(fs::read(self.path.as_path()))
}
/// reads the file as String
/// ```
/// file.read_to_string() // => "hello"
/// ```
pub fn read_to_string(&self) -> error::Result<String> {
error::result_from_io(fs::read_to_string(self.path.as_path()))
}
/// append to file
/// NOTE: will create file if it doesn't exists
/// ```
/// file.append(b"hello world");
/// file.append("hello world");
/// file.append("hello world".to_string());
/// ```
pub fn append<C: AsRef<[u8]>>(&self, content: C) -> error::Result<()> {
let maybe_file = fs::OpenOptions::new()
.write(true)
.append(true)
.create(true)
.open(self.path.as_path());
let mut file = error::result_from_io(maybe_file)?;
error::result_from_io(file.write_all(content.as_ref()))
}
/// get the metadata of the file
///
/// see https://doc.rust-lang.org/std/fs/struct.Metadata.html
pub fn metadata(&self) -> error::Result<fs::Metadata> {
error::result_from_io(fs::metadata(self.path.as_path()))
}
/// deletes the file
pub fn delete(&self) -> error::Result<()> {
error::result_from_io(fs::remove_file(self.path.as_path()))
}
/// copies the file to dest
/// ```
/// let file_copy = file.copy("dest.txt");
/// ```
pub fn copy<P: AsRef<Path>>(&self, destination: P) -> error::Result<File> {
let mut dest = PathBuf::new();
dest.push(destination);
error::result_from_io(fs::copy(&self.path.as_path(), &dest))?;
Ok(File::new(dest)?)
}
/// move the file to dest
/// ```
/// let file_moved = file.move_to("dest");
/// // or if you prefer
/// file = file.move_to("dest");
/// ```
pub fn move_to<P: AsRef<Path>>(&self, destination: P) -> error::Result<File> {
let options = fs_extra::file::CopyOptions::new();
let mut dest = PathBuf::new();
dest.push(destination);
error::result_from_fse(fs_extra::file::move_file(
self.path.as_path(),
&dest,
&options,
))?;
Ok(File::new(dest)?)
}
/// renames the file
/// NOTE: DO NOT use absolute paths with this function (use moveTo instead)
/// ```
/// let file_renamed = file.rename("new_name.txt");
/// // or if you prefer
/// file = file.rename("new_name.txt");
/// ```
pub fn rename<P: AsRef<Path>>(&self, dest: P) -> error::Result<File> {
let mut real_dest = PathBuf::new();
let parent =
error::result_from_option2(self.path.parent(), error::ErrorKind::PathNoParentFound)?;
real_dest.push(parent);
real_dest.push(dest);
self.move_to(real_dest)
}
/// sets the permissions of file see https://doc.rust-lang.org/std/fs/struct.Permissions.html
/// ```
/// let mut perm = file.metadata()?.permissions();
/// perms.set_readonly(true);
/// file.set_permissions(perms)?;
/// ```
pub fn set_permissions(&self, perm: fs::Permissions) -> error::Result<()> {
let file = error::result_from_io(fs::File::open(self.path.as_path()))?;
error::result_from_io(file.set_permissions(perm))
}
/// copy the file with progress
/// ```
/// use fs_extra;
/// file.copy_with_progress("dest", &fs_extra::file::CopyOptions::new(), |prg| {
/// println!("{:?}", prg.total_bytes);
/// })
/// ```
pub fn copy_with_progress<F: FnMut(fs_extra::file::TransitProcess), P: AsRef<Path>>(
&self,
to: P,
options: &fs_extra::file::CopyOptions,
progress_handler: F,
) -> error::Result<File> {
error::result_from_fse(fs_extra::file::copy_with_progress(
self.path.as_path(),
&to,
&options,
progress_handler,
))?;
Ok(File::new(to)?)
}
/// move the file with progress
/// ```
/// use fs_extra;
/// file.move_file_with_progress("dest", &fs_extra::file::CopyOptions::new(), |prg| {
/// println!("{:?}", prg.total_bytes);
/// })
/// ```
pub fn move_with_progress<F: FnMut(fs_extra::file::TransitProcess), P: AsRef<Path>>(
&self,
to: P,
options: &fs_extra::file::CopyOptions,
progress_handler: F,
) -> error::Result<File>
where
F: FnMut(fs_extra::file::TransitProcess),
{
error::result_from_fse(fs_extra::file::move_file_with_progress(
self.path.as_path(),
&to,
&options,
progress_handler,
))?;
Ok(File::new(to)?)
}
/// parses file as json
/// ```
/// use serde_json::Value;
///
/// let json: Value = file.json();
/// ```
#[cfg(feature = "json")]
pub fn json<T: for<'de> serde::Deserialize<'de>>(&self) -> error::Result<T> {
let file = error::result_from_io(fs::File::open(&self.path))?;
let reader = std::io::BufReader::new(file);
let maybe_res: serde_json::error::Result<T> = serde_json::from_reader(reader);
match maybe_res {
Ok(res) => Ok(res),
Err(e) => Err(error::Error::new_from_kind(error::ErrorKind::JsonError(e))),
}
}
}