use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
InvalidArgument,
Unreadable,
Password,
Limit,
Timeout,
Failed,
Io,
Library,
Internal,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
kind: ErrorKind,
message: String,
}
impl Error {
pub(crate) fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
Self {
kind,
message: message.into(),
}
}
pub(crate) fn argument(message: impl Into<String>) -> Self {
Self::new(ErrorKind::InvalidArgument, message)
}
pub(crate) fn from_status(status: i32, message: String) -> Self {
let kind = match status {
1 => ErrorKind::InvalidArgument,
2 => ErrorKind::Unreadable,
3 => ErrorKind::Password,
4 => ErrorKind::Limit,
5 => ErrorKind::Timeout,
6 => ErrorKind::Failed,
7 => ErrorKind::Io,
_ => ErrorKind::Internal,
};
Self::new(kind, message)
}
pub fn kind(&self) -> ErrorKind {
self.kind
}
pub fn message(&self) -> &str {
&self.message
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message)
}
}
impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Self::new(ErrorKind::Io, e.to_string())
}
}