use std::fmt::{Debug, Display};
#[derive(Debug)]
pub enum Error {
IoError {
source: std::io::Error,
},
FromUtf8Error {
source: std::string::FromUtf8Error,
},
}
pub type Result<T> = std::result::Result<Error, T>;
#[inline]
pub fn io_error(source: std::io::Error) -> Error {
Error::IoError { source }
}
#[inline]
pub fn from_utf8_error(source: std::string::FromUtf8Error) -> Error {
Error::FromUtf8Error { source }
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Error::IoError { source } => format!("An I/O error occurred; source: {}", source),
Error::FromUtf8Error { source } => format!(
"An error occurred making a string from UTF-8 bytes; source: {}",
source
),
}
)
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
#[allow(unreachable_patterns)]
match self {
Error::IoError { source } => Some(source),
Error::FromUtf8Error { source } => Some(source),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(source: std::io::Error) -> Self {
io_error(source)
}
}
impl From<std::string::FromUtf8Error> for Error {
fn from(source: std::string::FromUtf8Error) -> Self {
from_utf8_error(source)
}
}