use std::{fmt::Display, io::Error as IoError};
#[derive(Debug)]
pub enum Error {
Io(IoError),
EmptyPassword,
Yaml(String),
ChildExitStatusFailed(String, i32),
}
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::EmptyPassword, Self::EmptyPassword) => true,
(Self::Io(err), Self::Io(other_err)) => err.kind() == other_err.kind(),
(Self::Yaml(err), Self::Yaml(other_err)) => err == other_err,
(
Self::ChildExitStatusFailed(exe, code),
Self::ChildExitStatusFailed(other_exe, other_code),
) => exe == other_exe && code == other_code,
(_, _) => false,
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let message = match self {
Self::EmptyPassword => "password file is empty".into(),
Self::Io(why) => format!("I/O error: {why}"),
Self::Yaml(why) => format!("YAML parsing error: {why}"),
Self::ChildExitStatusFailed(exe, code) => {
format!("External program `{exe}` ended with status code {code}",)
}
};
write!(f, "{message}")
}
}