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
use super::sync::SyncData;
use std::path::PathBuf;
use walkdir::WalkDir;
/// Implementation for listing all the files and directories.
impl SyncData {
/// Gets the list of source files by walking through the source.
///
/// Returns:
/// - List of source files in a vector
///
/// Checks whether the source is actually a directory and walks to find the files in it.
///
/// # Example
///
/// ```rust,no_run
/// use cover_files::sync::sync::SyncData;
/// use std::path::PathBuf;
///
/// let sync = SyncData {
/// source: PathBuf::from("source_directory"),
/// destination: PathBuf::new(),
/// changed_only: true,
/// delete: false,
/// verbose: false,
/// dry_run: false,
/// };
///
/// let mut searched_file: Vec<PathBuf> = Vec::new();
/// let searched = sync.list_src_files();
///
/// for file in searched {
/// let filename = file
/// .file_name()
/// .and_then(|f| f.to_str())
/// .expect("[ERROR]: failed to get the filename");
///
/// searched_file.push(PathBuf::from(filename));
/// }
///
/// assert!(searched_file.len() != 0);
/// ```
pub fn list_src_files(&self) -> Vec<PathBuf> {
let mut src_files_list = Vec::new();
if !self.source.is_dir() {
eprintln!(
"[ERROR]: given source '{}' is not a directory",
self.source.display()
);
return Vec::new();
}
for entry in WalkDir::new(&self.source) {
let entry_path = entry
.as_ref()
.expect("[ERROR]: failed to get the path")
.path()
.to_path_buf();
if entry_path.is_file() {
src_files_list.push(entry_path);
}
}
src_files_list
}
/// Gets the list of sub-directories by walking through the source.
///
/// Returns:
/// - List of source directories in a vector
///
/// Checks whether the source is actually a directory and walks to find the sub-directories in it.
///
/// # Example
///
/// ```rust,no_run
/// use cover_files::sync::sync::SyncData;
/// use std::path::PathBuf;
///
/// let sync = SyncData {
/// source: PathBuf::from("source_directory"),
/// destination: PathBuf::new(),
/// changed_only: true,
/// delete: false,
/// verbose: false,
/// dry_run: false,
/// };
///
/// let mut searched_dir: Vec<PathBuf> = Vec::new();
/// let searched = sync.list_src_dirs();
///
/// for dir in &searched {
/// let data = dir
/// .iter()
/// .last()
/// .expect("[ERROR]: failed to get the last name")
/// .to_string_lossy()
/// .to_string();
///
/// searched_dir.push(PathBuf::from(data));
/// }
///
/// assert!(searched_dir.len() != 0);
/// ```
pub fn list_src_dirs(&self) -> Vec<PathBuf> {
let mut src_dirs_list = Vec::new();
if !self.source.is_dir() {
eprintln!(
"[ERROR]: given source '{}' is not a directory",
self.source.display()
);
return Vec::new();
}
for entry in WalkDir::new(&self.source) {
let entry_path = entry
.as_ref()
.expect("[ERROR]: failed to get the path")
.path()
.to_path_buf();
if entry_path.is_dir() {
src_dirs_list.push(entry_path);
}
}
src_dirs_list
}
/// Gets the list of destination files by walking through the destination.
///
/// Returns:
/// - List of destination files in a vector
///
/// Checks whether the destination is actually a directory and walks to find the files in it.
///
/// # Example
///
/// ```rust,no_run
/// use cover_files::sync::sync::SyncData;
/// use std::path::PathBuf;
///
/// let sync = SyncData {
/// source: PathBuf::new(),
/// destination: PathBuf::from("destination_directory"),
/// changed_only: true,
/// delete: false,
/// verbose: false,
/// dry_run: false,
/// };
///
/// let mut searched_file: Vec<PathBuf> = Vec::new();
/// let searched = sync.list_dest_files();
///
/// for file in searched {
/// let filename = file
/// .file_name()
/// .and_then(|f| f.to_str())
/// .expect("[ERROR]: failed to get the filename");
///
/// searched_file.push(PathBuf::from(filename));
/// }
///
/// assert!(searched_file.len() != 0);
/// ```
pub fn list_dest_files(&self) -> Vec<PathBuf> {
let mut dest_files_list = Vec::new();
if !self.destination.is_dir() {
eprintln!(
"[ERROR]: given destination '{}' is not a directory",
self.destination.display()
);
return Vec::new();
}
for entry in WalkDir::new(&self.destination) {
let entry_path = entry
.as_ref()
.expect("[ERROR]: failed to get the path")
.path()
.to_path_buf();
if entry_path.is_file() {
dest_files_list.push(entry_path);
}
}
dest_files_list
}
/// Gets the list of sub-directories by walking through the destination.
///
/// Returns:
/// - List of destination directories in a vector
///
/// Checks whether the destination is actually a directory and walks to find the sub-directories in it.
///
/// # Example
///
/// ```rust,no_run
/// use cover_files::sync::sync::SyncData;
/// use std::path::PathBuf;
///
/// let sync = SyncData {
/// source: PathBuf::new(),
/// destination: PathBuf::from("destination_directory"),
/// changed_only: true,
/// delete: false,
/// verbose: false,
/// dry_run: false,
/// };
///
/// let mut searched_dir: Vec<PathBuf> = Vec::new();
/// let searched = sync.list_dest_dirs();
///
/// for dir in &searched {
/// let data = dir
/// .iter()
/// .last()
/// .expect("[ERROR]: failed to get the last name")
/// .to_string_lossy()
/// .to_string();
///
/// searched_dir.push(PathBuf::from(data));
/// }
///
/// assert!(searched_dir.len() != 0);
/// ```
pub fn list_dest_dirs(&self) -> Vec<PathBuf> {
let mut dest_dirs_list = Vec::new();
if !self.destination.is_dir() {
eprintln!(
"[ERROR]: given source '{}' is not a directory",
self.destination.display()
);
return Vec::new();
}
for entry in WalkDir::new(&self.destination) {
let entry_path = entry
.as_ref()
.expect("[ERROR]: failed to get the path")
.path()
.to_path_buf();
if entry_path.is_dir() {
dest_dirs_list.push(entry_path);
}
}
dest_dirs_list
}
}