cover-files 0.1.24

A simple Rust tool for syncing directories with change detection
Documentation
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use super::sync::SyncData;
use std::{collections::BTreeSet, path::PathBuf};

/// Lists the source sub-directories by skipping the parent directory name.
///
/// Takes:
/// - List of all the source sub-directories
/// - Name of the source directory
///
/// Returns:
/// - List of source sub-directories after skipping the source directory name
///
/// # Example
///
/// ```rust,no_run
/// use cover_files::sync::sync::SyncData;
/// use cover_files::sync::log::filter_src_dir;
/// 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 list_dirs = sync.list_src_dirs();
/// let filtered = filter_src_dir(&list_dirs, &sync.source);
/// println!("{:?}", filtered);
/// ```
pub fn filter_src_dir(src_dirs: &Vec<PathBuf>, source: &PathBuf) -> Vec<PathBuf> {
    let mut list_src_dirs = Vec::new();

    for dirs in src_dirs {
        if dirs == source {
            continue;
        }

        let subdir = dirs
            .strip_prefix(source)
            .expect("[ERROR]: failed to get the directory");
        list_src_dirs.push(PathBuf::from(subdir));
    }
    list_src_dirs
}

/// Lists the destination sub-directories by skipping the parent directory name.
///
/// Takes:
/// - List of all the destination sub-directories
/// - Name of the destination directory
///
/// Returns:
/// - List of destination sub-directories after skipping the destination directory name
///
/// # Example
///
/// ```rust,no_run
/// use cover_files::sync::sync::SyncData;
/// use cover_files::sync::log::filter_dest_dir;
/// use std::path::PathBuf;
///
/// let sync = SyncData {
///    source: PathBuf::new(),
///    destination: PathBuf::from("source directory"),
///    changed_only: true,
///    delete: false,
///    verbose: false,
///    dry_run: false,
/// };

/// let list_dirs = sync.list_dest_dirs();
/// let filtered = filter_dest_dir(&list_dirs, &sync.destination);
/// println!("{:?}", filtered);
/// ```
pub fn filter_dest_dir(dest_dirs: &Vec<PathBuf>, destination: &PathBuf) -> Vec<PathBuf> {
    let mut list_dest_dirs = Vec::new();

    for dirs in dest_dirs {
        if dirs == destination {
            continue;
        }

        let subdir = dirs
            .strip_prefix(destination)
            .expect("[ERROR]: failed to get the directory");
        list_dest_dirs.push(PathBuf::from(subdir));
    }
    list_dest_dirs
}

/// Lists the source files by skipping the whole path.
///
/// Takes:
/// - List of all the source file names
///
/// Returns:
/// - List of source filenames after skipping the path
///
/// # Example
///
/// ```rust,no_run
/// use cover_files::sync::sync::SyncData;
/// use cover_files::sync::log::filter_src_file;
/// 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 list_files = sync.list_src_files();
/// let filtered = filter_src_file(&list_files);
/// println!("{:?}", filtered);
/// ```
pub fn filter_src_file(src_files: &Vec<PathBuf>) -> Vec<PathBuf> {
    let mut list_src_files = Vec::new();

    for file in src_files {
        let sub_file = file
            .file_name()
            .expect("[ERROR]: failed to get the filename");

        list_src_files.push(PathBuf::from(sub_file));
    }
    list_src_files
}

/// Lists the destination files by skipping the whole path.
///
/// Takes:
/// - List of all the destination file names
///
/// Returns:
/// - List of destination filenames after skipping the path
///
/// # Example
///
/// ```rust,no_run
/// use cover_files::sync::sync::SyncData;
/// use cover_files::sync::log::filter_dest_file;
/// 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 list_files = sync.list_dest_files();
/// let filtered = filter_dest_file(&list_files);
/// println!("{:?}", filtered);
/// ```
pub fn filter_dest_file(dest_files: &Vec<PathBuf>) -> Vec<PathBuf> {
    let mut list_dest_files = Vec::new();

    for file in dest_files {
        let sub_file = file
            .file_name()
            .expect("[ERROR]: failed to get the filename");

        list_dest_files.push(PathBuf::from(sub_file));
    }
    list_dest_files
}

fn print_format(files: &Vec<PathBuf>) {
    if files.len() == 0 {
        println!("Empty");
    } else if files.len() == 1 {
        println!("{}", files[0].display());
    } else {
        println!("{:?}", files);
    }
}

fn format_path(path: Vec<&PathBuf>) -> String {
    if path.is_empty() {
        "0".to_string()
            .strip_prefix("")
            .expect("[ERROR]: failed to remove the comma")
            .to_string()
    } else {
        format!(
            "[{}]",
            path.iter()
                .map(|f| f.display().to_string())
                .collect::<Vec<_>>()
                .join(", ")
        )
    }
}

fn list_data(data: [Vec<PathBuf>; 4], from: [PathBuf; 2]) -> [Vec<PathBuf>; 4] {
    let list_src_dirs = filter_src_dir(&data[0], &from[0]);
    let list_dest_dirs = filter_dest_dir(&data[1], &from[1]);
    let list_src_files = filter_src_file(&data[2]);
    let list_dest_files = filter_dest_file(&data[3]);

    println!("\n[---------LOGS OF ACTION---------]");

    print!("[SOURCE DIRECTORIES]: ");
    print_format(&list_src_dirs);

    print!("[DESTINATION DIRECTORIES]: ");
    print_format(&list_dest_dirs);

    print!("[SOURCE FILES]: ");
    print_format(&list_src_files);

    print!("[DESTINATION FILES]: ");
    print_format(&list_dest_files);

    let result: [Vec<PathBuf>; 4] = [
        list_src_dirs,
        list_dest_dirs,
        list_src_files,
        list_dest_files,
    ];
    result
}

/// Implementation of getting the logs of all the actions
impl SyncData {
    /// Gives the logs of the files and directories that will be copied from the source
    ///
    /// Lists all the source and destination files and directories, gives them the order,
    /// and pushes their union files and directories in a vector.
    ///
    /// Prints the result at the end to show.
    pub fn src_creation_log(&self) {
        let src_dirs = self.list_src_dirs();
        let dest_dirs = self.list_dest_dirs();
        let src_files = self.list_src_files();
        let dest_files = self.list_dest_files();

        let data: [Vec<PathBuf>; 4] = [src_dirs, dest_dirs, src_files, dest_files];
        let from: [PathBuf; 2] = [self.source.clone(), self.destination.clone()];
        let list_result = list_data(data, from);

        let src_dir_set: BTreeSet<_> = list_result[0].iter().collect();
        let dest_dir_set: BTreeSet<_> = list_result[1].iter().collect();
        let src_file_set: BTreeSet<_> = list_result[2].iter().collect();
        let dest_file_set: BTreeSet<_> = list_result[3].iter().collect();
        let mut directory: Vec<&PathBuf> = Vec::new();
        let mut file: Vec<&PathBuf> = Vec::new();
        let mut done = false;

        for name in src_dir_set.union(&dest_dir_set) {
            match (src_dir_set.get(name), dest_dir_set.get(name)) {
                (Some(src), None) => {
                    directory.push(src);
                    done = true;
                }
                _ => {}
            }
        }

        for name in src_file_set.union(&dest_file_set) {
            match (src_file_set.get(name), dest_file_set.get(name)) {
                (Some(src), None) => {
                    file.push(src);
                    done = true;
                }
                _ => {}
            }
        }

        if done {
            println!(
                "[DIFFERENCE]: {} directories & {} files",
                format_path(directory.clone()),
                format_path(file.clone())
            );
            println!("[STATUS]: Not matched");
            self.copy_src_to_dest();
            println!(
                "[COPIED]: {} & {} -> {}",
                format_path(directory),
                format_path(file),
                &self.destination.display()
            );
        }
    }

    /// Gives the logs of the source files that are modified.
    ///
    /// Lists all the source and destination files and directories,
    /// and prints the modified files.
    ///
    /// After giving the status, updates the destination file according to it.
    pub fn src_modification_log(&self, filenames: Vec<PathBuf>) {
        let src_dirs = self.list_src_dirs();
        let dest_dirs = self.list_dest_dirs();
        let src_files = self.list_src_files();
        let dest_files = self.list_dest_files();

        let data: [Vec<PathBuf>; 4] = [src_dirs, dest_dirs, src_files, dest_files];
        let from: [PathBuf; 2] = [self.source.clone(), self.destination.clone()];
        let _ = list_data(data, from);

        print!("[MODIFIED FILES]: ");
        print_format(&filenames);

        println!("[STATUS]: Not matched");

        self.update_dest_file(filenames.clone());

        print!("[UPDATED FILES]: ");
        print_format(&filenames);
    }

    /// Gives the logs of the files and directories that will be removed from the destination
    ///
    /// Lists all the source and destination files and directories, gives them the order,
    /// and pushes their union files and directories in a vector.
    ///
    /// Prints the result at the end to show.
    pub fn dest_creation_log(&self) {
        let src_dirs = self.list_src_dirs();
        let dest_dirs = self.list_dest_dirs();
        let src_files = self.list_src_files();
        let dest_files = self.list_dest_files();

        let data: [Vec<PathBuf>; 4] = [src_dirs, dest_dirs, src_files, dest_files];
        let from: [PathBuf; 2] = [self.source.clone(), self.destination.clone()];
        let list_result = list_data(data, from);

        let src_dir_set: BTreeSet<_> = list_result[0].iter().collect();
        let dest_dir_set: BTreeSet<_> = list_result[1].iter().collect();
        let src_file_set: BTreeSet<_> = list_result[2].iter().collect();
        let dest_file_set: BTreeSet<_> = list_result[3].iter().collect();
        let mut directory: Vec<&PathBuf> = Vec::new();
        let mut file: Vec<&PathBuf> = Vec::new();
        let mut done = false;

        for name in src_dir_set.union(&dest_dir_set) {
            match (src_dir_set.get(name), dest_dir_set.get(name)) {
                (None, Some(dest)) => {
                    directory.push(dest);
                    done = true;
                }
                _ => {}
            }
        }

        for name in src_file_set.union(&dest_file_set) {
            match (src_file_set.get(name), dest_file_set.get(name)) {
                (None, Some(dest)) => {
                    file.push(dest);
                    done = true;
                }
                _ => {}
            }
        }

        if done {
            println!(
                "[DIFFERENCE]: {} directories & {} files",
                format_path(directory.clone()),
                format_path(file.clone())
            );
            println!("[STATUS]: Not matched");
            self.remove_dest_file();
            println!(
                "[REMOVED]: {} directories & {} files from {}",
                format_path(directory),
                format_path(file),
                &self.destination.display()
            );
        }
    }

    /// Gives the logs of the destination files that are modified.
    ///
    /// Lists all the source and destination files and directories,
    /// and prints the modified files.
    ///
    /// After giving the status, removed the destination file content.
    /// The destination file content is not allowed to be modified.
    pub fn dest_modification_log(&self, filenames: Vec<PathBuf>) {
        let src_dirs = self.list_src_dirs();
        let dest_dirs = self.list_dest_dirs();
        let src_files = self.list_src_files();
        let dest_files = self.list_dest_files();

        let data: [Vec<PathBuf>; 4] = [src_dirs, dest_dirs, src_files, dest_files];
        let from: [PathBuf; 2] = [self.source.clone(), self.destination.clone()];
        let _ = list_data(data, from);

        print!("[MODIFIED FILES]: ");
        print_format(&filenames);

        println!("[STATUS]: Not matched");

        self.update_dest_file(filenames.clone());

        print!("[REMOVED FILE CONTENT]: ");
        print_format(&filenames);
    }
}