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