2023-07-05 10:01:40 -04:00
|
|
|
use std::{
|
|
|
|
array::TryFromSliceError, ffi::NulError, fmt, io, num::TryFromIntError, string::FromUtf8Error,
|
|
|
|
};
|
2023-07-04 00:51:47 -04:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
Int(TryFromIntError),
|
|
|
|
Io(io::Error),
|
|
|
|
Utf8(FromUtf8Error),
|
|
|
|
Slice(TryFromSliceError),
|
2023-07-11 00:57:22 -04:00
|
|
|
BadPath,
|
2023-07-04 00:51:47 -04:00
|
|
|
InvalidChecksum,
|
2023-07-06 18:31:49 -04:00
|
|
|
InvalidAlgorithm,
|
2023-07-07 19:20:21 -04:00
|
|
|
InvalidMagic,
|
2023-07-04 00:51:47 -04:00
|
|
|
MissingData,
|
2023-07-05 23:21:30 -04:00
|
|
|
MutexError,
|
2023-07-05 10:01:40 -04:00
|
|
|
NulError,
|
2023-07-05 23:21:30 -04:00
|
|
|
SenderError,
|
2023-07-04 00:51:47 -04:00
|
|
|
UnexpectedData,
|
|
|
|
UnknownFileType,
|
|
|
|
Other(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Int(e) => write!(f, "{e}"),
|
|
|
|
Self::Utf8(e) => write!(f, "{e}"),
|
|
|
|
Self::Slice(e) => write!(f, "{e}"),
|
|
|
|
Self::Io(e) => write!(f, "{e}"),
|
2023-07-11 00:57:22 -04:00
|
|
|
Self::BadPath => write!(f, "bad path"),
|
2023-07-06 18:31:49 -04:00
|
|
|
Self::InvalidAlgorithm => write!(f, "invalid algorithm"),
|
2023-07-04 00:51:47 -04:00
|
|
|
Self::InvalidChecksum => write!(f, "invalid checksum"),
|
2023-07-07 19:20:21 -04:00
|
|
|
Self::InvalidMagic => write!(f, "invalid magic"),
|
2023-07-04 00:51:47 -04:00
|
|
|
Self::MissingData => write!(f, "missing data"),
|
2023-07-05 23:21:30 -04:00
|
|
|
Self::MutexError => write!(f, "mutex error"),
|
2023-07-05 10:01:40 -04:00
|
|
|
Self::NulError => write!(f, "nul error"),
|
2023-07-05 23:21:30 -04:00
|
|
|
Self::SenderError => write!(f, "sender error"),
|
2023-07-04 00:51:47 -04:00
|
|
|
Self::UnexpectedData => write!(f, "unexpected data"),
|
|
|
|
Self::UnknownFileType => write!(f, "unknown file type"),
|
|
|
|
Self::Other(s) => write!(f, "{s}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for Error {
|
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
|
|
match self {
|
|
|
|
Self::Int(e) => Some(e),
|
|
|
|
Self::Io(e) => Some(e),
|
|
|
|
Self::Utf8(e) => Some(e),
|
|
|
|
Self::Slice(e) => Some(e),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<TryFromIntError> for Error {
|
|
|
|
fn from(value: TryFromIntError) -> Self {
|
|
|
|
Self::Int(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(value: io::Error) -> Self {
|
|
|
|
Self::Io(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<FromUtf8Error> for Error {
|
|
|
|
fn from(value: FromUtf8Error) -> Self {
|
|
|
|
Self::Utf8(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<TryFromSliceError> for Error {
|
|
|
|
fn from(value: TryFromSliceError) -> Self {
|
|
|
|
Self::Slice(value)
|
|
|
|
}
|
|
|
|
}
|
2023-07-05 10:01:40 -04:00
|
|
|
|
|
|
|
impl From<NulError> for Error {
|
|
|
|
fn from(_value: NulError) -> Self {
|
|
|
|
Self::NulError
|
|
|
|
}
|
|
|
|
}
|