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
use super::sync::SyncData;
use std::{collections::HashSet, path::PathBuf};
/// Implemenation of the helper methods used in other methods.
impl SyncData {
/// Checks the source and destination directories presence.
///
/// Returns:
/// - Boolean to see the directories are not empty.
///
/// Verifiies the existance of source and destination and also used as directories.
///
/// # Example
///
/// ```rust,no_run
/// use cover_files::sync::sync::SyncData;
/// use std::path::PathBuf;
///
/// let sync = SyncData {
/// source: PathBuf::from("source_directory"),
/// destination: PathBuf::from("destination_directory"),
/// changed_only: true,
/// delete: false,
/// dry_run: false,
/// verbose: false,
/// };
///
/// assert!(sync.src_dest_dir_present(), "[ERROR]: source or destination not detected");
/// ```
pub fn src_dest_dir_present(&self) -> bool {
let src_not_empty = !self.source.to_string_lossy().trim().is_empty();
let dest_not_empty = !self.destination.to_string_lossy().trim().is_empty();
if !self.source.exists() && !self.destination.exists() {
return false;
}
if !self.source.is_dir() || !self.destination.is_dir() {
return false;
}
if src_not_empty && dest_not_empty {
return true;
} else {
return false;
}
}
/// Allows only one flag after the source and destination.
///
/// Returns:
/// - Boolean to check the selection.
///
/// Checks the source and destination as directories and count the flag if they are given.
///
/// # Example
///
/// ```rust,no_run
/// use cover_files::sync::sync::SyncData;
/// use std::path::PathBuf;
///
/// let mut sync = SyncData {
/// source: PathBuf::from("source_directory"),
/// destination: PathBuf::from("destination_directory"),
/// changed_only: true,
/// delete: false,
/// dry_run: false,
/// verbose: false,
/// };
///
/// assert!(sync.single_command_selected(), "[ERROR]: expected one command, but multiple are reported");
/// ```
pub fn single_command_selected(&mut self) -> bool {
let source_contains = !self.source.to_string_lossy().trim().is_empty();
let destination_contains = !self.destination.to_string_lossy().trim().is_empty();
if !self.source.is_dir() && !self.destination.is_dir() {
return false;
}
if !(source_contains && destination_contains) {
return false;
}
let mut count = 0;
if self.changed_only {
count += 1;
}
if self.delete {
count += 1;
}
if self.dry_run {
count += 1;
}
if self.verbose {
count += 1;
}
count == 1
}
/// Gets the file and directory names for checking the duplication.
///
/// Returns:
/// - Directories list
/// - Files list
///
/// After listing the files and directories, splits the full path of them and takes only the last name to return.
///
/// # 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,
/// dry_run: false,
/// verbose: false,
/// };
///
/// let (dir_list, file_list) = sync.get_file_names();
/// assert!(dir_list.len() != 0 && file_list.len() != 0);
/// ```
pub fn get_file_names(&self) -> (Vec<PathBuf>, Vec<PathBuf>) {
let mut dir_list: Vec<PathBuf> = Vec::new();
let mut file_list: Vec<PathBuf> = Vec::new();
let src_dirs = self.list_src_dirs();
let src_files = self.list_src_files();
for entry in src_dirs {
if entry == self.source {
continue;
}
let mut comp = entry.components();
comp.next();
let path = comp.as_path();
let path_data = path.display().to_string();
if let Some(dir) = path_data.clone().split("/").last() {
dir_list.push(PathBuf::from(dir));
}
}
for entry in src_files {
if entry == self.source {
continue;
}
let mut comp = entry.components();
comp.next();
let path = comp.as_path();
let path_data = path.display().to_string();
if let Some(file) = path_data.clone().split("/").last() {
file_list.push(PathBuf::from(file));
}
}
(dir_list, file_list)
}
/// It prevents the duplication of files and directories to be inserted.
///
/// Returns:
/// - Boolean to check if the files or directories has duplicates.
///
/// # Example
///
/// ```rust,no_run
/// use cover_files::sync::sync::SyncData;
/// use std::path::PathBuf;
///
/// let sync = SyncData {
/// source: PathBuf::from("source_directory"),
/// destination: PathBuf::from("destination_directory"),
/// changed_only: true,
/// delete: false,
/// dry_run: false,
/// verbose: false,
/// };
///
/// let result = sync.has_duplicates();
/// assert!(result == true);
/// ```
pub fn has_duplicates(&self) -> bool {
let mut dir_hash: HashSet<PathBuf> = HashSet::new();
let mut file_hash: HashSet<PathBuf> = HashSet::new();
let (dir_list, file_list) = self.get_file_names();
for dir in dir_list {
if !dir_hash.insert(dir) {
return true;
}
}
for file in file_list {
if !file_hash.insert(file) {
return true;
}
}
false
}
}