use std::string::FromUtf8Error;
use std::{error, fmt, io};
#[derive(Debug)]
pub enum IoError {
Io(io::Error),
UnsupportedType(String),
NullMaskInconsistent(String),
InputDataError(String),
Metadata(String),
Compression(String),
Internal(String),
Format(String),
UnsupportedEncoding(String),
}
impl fmt::Display for IoError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
IoError::Io(e) => write!(f, "I/O error: {}", e),
IoError::UnsupportedType(s) => write!(f, "Unsupported type: {}", s),
IoError::NullMaskInconsistent(s) => write!(f, "Null mask inconsistency: {}", s),
IoError::InputDataError(s) => write!(f, "Data error: {}", s),
IoError::Metadata(s) => write!(f, "Metadata error: {}", s),
IoError::Compression(s) => write!(f, "Compression error: {}", s),
IoError::Internal(s) => write!(f, "Internal error: {}", s),
IoError::Format(s) => write!(f, "Formatting error: {}", s),
IoError::UnsupportedEncoding(s) => write!(f, "Unsupported encoding error: {}", s),
}
}
}
impl error::Error for IoError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
IoError::Io(e) => Some(e),
_ => None,
}
}
}
impl From<io::Error> for IoError {
fn from(e: io::Error) -> Self {
IoError::Io(e)
}
}
impl From<FromUtf8Error> for IoError {
fn from(e: FromUtf8Error) -> Self {
IoError::InputDataError(e.to_string())
}
}
#[cfg(feature = "snappy")]
impl From<snap::Error> for IoError {
fn from(e: snap::Error) -> Self {
IoError::Compression(format!("Snappy: {e}"))
}
}