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
#[macro_use]
pub mod macros {
pub type IoError<T> = Result<T, Box<dyn std::error::Error>>;
/// ## create and write to file
/// ```ignore
///if let Ok(_r) = create_write_file!("./demo.txt", "alen".as_bytes()) {
/// println!("{:?}", _r);
///}
/// ```
#[macro_export]
macro_rules! create_write_file {
( $path:expr,$data:expr ) => {{
use std::io::Write;
let mut file = std::fs::File::create($path).expect("create failed");
file.write_all(&$data)
}};
}
/// ## read file return Result<Vec\<u8\>, Box<dyn std::error::Error\>\>
/// ```ignore
///if let Ok(_r) = read_file!("./demo.txt") {
/// println!("{:?}", _r);
///}
/// ```
#[macro_export]
macro_rules! read_file {
( $path:expr) => {{
std::fs::read($path)
}};
}
/// ## rename file
/// ```ignore
/// match rename!("./demo.txt","./test.txt"){
/// Ok(_r)=>{
/// println!("done");
/// },
/// Err(_e)=>{}
/// }
/// ```
#[macro_export]
macro_rules! rename {
($path_1:expr,$path_2:expr) => {{
use easy_file::macros::IoError;
use std::fs;
use std::path::Path;
fn rename(path_1: &Path, path_2: &Path) -> IoError<()> {
fs::rename(path_1, path_2)?;
Ok(())
}
let path_1 = Path::new($path_1);
let path_2 = Path::new($path_2);
rename(path_1, path_2)
}};
}
/// can be used to remove file or folder
/// ```ignore
/// match remove!("./test.txt"){
/// Ok(_r)=>{
/// println!("done");
/// },
/// Err(_e)=>{}
/// }
/// ```
#[macro_export]
macro_rules! remove {
($path:expr) => {{
use easy_file::macros::IoError;
use std::fs;
use std::path::Path;
fn rm(path: &Path) -> IoError<()> {
if path.is_dir() {
fs::remove_dir_all(path)?;
} else if path.is_file() {
fs::remove_file(path)?;
}
Ok(())
}
let path = Path::new($path);
rm(path)
}};
}
/// list directory return Vec<PathBuf>
///```ignore
/// match list_dir!("./"){
/// Ok(_r)=>{
/// println!("{:?}",_r);
/// },
/// Err(_e)=>{}
///}
/// ```
#[macro_export]
macro_rules! list_dir {
($path:expr) => {{
fn list_dir(dir: &str) -> Vec<std::path::PathBuf> {
let files: Vec<_> = std::fs::read_dir(dir)
.unwrap()
.map(|entry| entry.unwrap().path())
.collect();
files
}
list_dir($path)
}};
}
/// append data to file
/// ```ignore
/// match append_to_file!("./foo.txt", "demon".as_bytes()) {
/// Ok(_r) => {
/// println!("done");
/// }
/// Err(_e) => {}
/// }
/// ```
#[macro_export]
macro_rules! append_to_file {
($path:expr,$data:expr) => {{
use std::io::Write;
fn append(file_path: &str, data: &[u8]) -> Result<usize, std::io::Error> {
use std::fs::OpenOptions;
use std::io::Read;
let mut file_0 = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(file_path);
let mut bytes = Vec::new();
let file_1 = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(file_path);
file_0.unwrap().read_to_end(&mut bytes).unwrap();
bytes.extend(data);
file_1.unwrap().write(&bytes.to_vec())
}
append($path, $data)
}};
}
/// list directory return Vec<PathBuf>
///```ignore
/// if let Ok(_r) = read_to_string!("./foo.txt") {
/// println!("{}", _r);
/// }
/// ```
#[macro_export]
macro_rules! read_to_string {
($path:expr) => {{
use easy_file::macros::IoError;
use std::{
fs::File,
io::{self, Read},
path::Path,
};
fn read_to_string<P: AsRef<Path>>(path: P) -> IoError<String> {
fn inner(path: &Path) -> IoError<String> {
let mut file = File::open(path)?;
let mut string = String::new();
file.read_to_string(&mut string)?;
Ok(string)
}
inner(path.as_ref())
}
read_to_string($path)
}};
}
/// copy file from a path to another path
///```ignore
/// if let Ok(_r) = copy_to_path!("./foo.txt", "./src/foo.txt") {
/// println!("{}", _r);
/// }
/// ```
#[macro_export]
macro_rules! copy_to_path {
($from:expr,$to:expr) => {{
use std::path::Path;
let path_0 = Path::new($from);
let path_1 = Path::new($to);
std::fs::copy(path_0, path_1)
}};
}
/// move file from a path to another path
///```ignore
/// if let Ok(_r) = move_to_path!("./foo.txt", "./src/foo.txt") {
/// }
/// ```
#[macro_export]
macro_rules! move_to_path {
($from:expr,$to:expr) => {{
use std::path::Path;
let path_0 = Path::new($from);
let path_1 = Path::new($to);
copy_to_path!(path_0, path_1).unwrap();
remove!(path_0).unwrap()
}};
}
/// Creates a new, empty directory at the provided path
///```ignore
/// if let Ok(_r) = create_dir!("./demo") {}
/// ```
#[macro_export]
macro_rules! create_dir {
($folder_name:expr) => {{
use easy_file::macros::IoError;
use std::path::Path;
let path_0 = Path::new($folder_name);
fn create_dir(name: &Path) -> IoError<()> {
std::fs::create_dir(name)?;
Ok(())
}
create_dir(path_0)
}};
}
/// Recursively create a directory and all of its parent components if they are missing.
///```ignore
/// if let Ok(_r) = create_dir_all!("./rust/js") {}
/// ```
#[macro_export]
macro_rules! create_dir_all {
($folder_name:expr) => {{
use easy_file::macros::IoError;
use std::path::Path;
let path_0 = Path::new($folder_name);
fn create_dir_all(name: &Path) -> IoError<()> {
std::fs::create_dir_all(name)?;
Ok(())
}
create_dir_all(path_0)
}};
}
}