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
use std::collections::HashSet;
use std::error::Error;
use std::fmt;
use std::fmt::{Display, Formatter};
#[derive(Debug)]
pub enum NFLZError {
FilenameMustIncludeExactlyOneNumberedGroup(String),
ValueInNumberedGroupInvalid(String),
CantReadDirectory(String, std::io::Error),
ConflictingFiles(Vec<String>),
RenameFailed(String, String, std::io::Error),
AmbiguousSuffixes(HashSet<String>),
AmbiguousPrefixes(HashSet<String>),
}
impl NFLZError {
pub fn filename(&self) -> Option<&str> {
match self {
NFLZError::FilenameMustIncludeExactlyOneNumberedGroup(fln) => {
Option::from(fln.as_str())
}
NFLZError::ValueInNumberedGroupInvalid(fln) => Option::from(fln.as_str()),
NFLZError::RenameFailed(fln, _, _) => Option::from(fln.as_str()),
_ => None,
}
}
}
impl Display for NFLZError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
NFLZError::FilenameMustIncludeExactlyOneNumberedGroup(fln) => f.write_str(&format!(
"The filename '{}' must include exactly one numbered group.",
fln
)),
NFLZError::ValueInNumberedGroupInvalid(value) => f.write_str(&format!(
"The value '{}' in the numbered group is not a number.",
value
)),
NFLZError::CantReadDirectory(value, os_err) => f.write_str(&format!(
"The directory ('{}') or the files in it can't be read because: {}",
value, os_err
)),
NFLZError::ConflictingFiles(files) => f.write_str(&format!(
"Can't rename files because {} new file names are in conflict with existing ones.",
files.len()
)),
NFLZError::RenameFailed(old_filename, new_filename, os_err) => f.write_str(&format!(
"Can't rename file '{}' to '{}' because: {}",
old_filename, new_filename, os_err,
)),
NFLZError::AmbiguousSuffixes(suffixes) => f.write_str(&format!(
"There are multiple (and therefore ambiguous) suffixes in this directory: {:?}",
suffixes,
)),
NFLZError::AmbiguousPrefixes(prefixes) => f.write_str(&format!(
"There are multiple (and therefore ambiguous) prefixes in this directory: {:?}",
prefixes,
)),
}
}
}
impl Error for NFLZError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
NFLZError::CantReadDirectory(_, os_err) => Some(os_err),
_ => None,
}
}
}