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
use fs::{remove_dir_all, rename};
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
use diagnostic_quick::{QError, QResult};
use diagnostic_quick::error_3rd::WalkDir;
use crate::FlattenFlies;
impl FlattenFlies {
pub fn run<P>(&self, input: P) -> QResult where P: AsRef<Path> {
let path = input.as_ref();
match self.try_run(path) {
Ok(_) => {}
Err(e) => {
Err(e)?
}
}
Ok(())
}
fn try_run(&self, path: &Path) -> QResult {
let target = self.output.as_path();
for entry in WalkDir::new(path) {
let entry = entry?;
if entry.path().is_file() {
let source = entry.path();
let target = target.join(source.file_name().unwrap());
self.safe_rename(source, &target)?;
}
}
for entry in WalkDir::new(path) {
self.debug_remove(entry?.path())?;
}
Ok(())
}
fn safe_rename(&self, source: &Path, target: &Path) -> QResult {
if self.overwrite {
return self.debug_rename(source, target);
}
if !target.exists() {
self.debug_rename(source, target)?;
}
let file_name = match target.file_name().and_then(OsStr::to_str) {
None => {
Err(QError::runtime_error("target file name is not valid"))?
}
Some(s) => {
s
}
};
let mut new_id = 1;
while new_id < 65535 {
let new_name = format!("{} ({})", file_name, new_id);
let new_target = target.with_file_name(new_name);
if !new_target.exists() {
return self.debug_rename(source, &new_target);
}
new_id += 1;
}
Ok(())
}
fn debug_rename(&self, source: &Path, target: &Path) -> QResult {
println!("[mv] {} \n -> {}", source.display(), target.display());
if self.execute {
rename(source, target)?;
}
Ok(())
}
fn debug_remove(&self, path: &Path) -> QResult {
if self.delete_empty && is_empty_directory(path) {
println!("[rm] remove {}", path.display());
if self.execute {
remove_dir_all(path)?;
}
}
Ok(())
}
}
pub fn is_empty_directory(path: &Path) -> bool {
match path.read_dir() {
Ok(mut o) => {
o.next().is_none()
}
Err(e) => {
println!("error: {}", e);
false
}
}
}