use std::fmt;
use std::path::PathBuf;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CliOptionsError {
DisplayHelp(String),
DisplayVersion(String),
NoInput(String),
Error(String),
InvalidInputFile(PathBuf),
}
impl CliOptionsError {
pub fn from_clap(error: clap::Error, allow_color_stdout: bool) -> Self {
match error.kind() {
clap::error::ErrorKind::DisplayVersion => {
CliOptionsError::DisplayVersion(error.to_string())
}
clap::error::ErrorKind::DisplayHelp => {
let help = if allow_color_stdout {
error.render().ansi().to_string()
} else {
error.to_string()
};
CliOptionsError::DisplayHelp(help)
}
_ => {
let message = error.to_string();
let message = message.strip_prefix("error: ").unwrap_or(&message);
CliOptionsError::Error(message.to_string())
}
}
}
}
impl fmt::Display for CliOptionsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CliOptionsError::DisplayHelp(message) => write!(f, "{message}"),
CliOptionsError::DisplayVersion(message) => write!(f, "{message}"),
CliOptionsError::NoInput(message) => write!(f, "{message}"),
CliOptionsError::Error(message) => write!(f, "error: {message}"),
CliOptionsError::InvalidInputFile(path) => write!(
f,
"error: Cannot access '{}': No such file or directory",
path.display()
),
}
}
}