use core::fmt;
#[cfg(feature = "std")]
use std::error::Error as StdError;
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, Clone, PartialEq)]
pub enum Error {
InvalidFormat(String),
UnexpectedEof,
BufferOverflow,
NotEnoughData,
UnsupportedVersion(u8),
Compression(String),
Io(String),
Serde(String),
Custom(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::InvalidFormat(msg) => write!(f, "Invalid format: {msg}"),
Error::UnexpectedEof => write!(f, "Unexpected end of input"),
Error::BufferOverflow => write!(f, "Buffer overflow"),
Error::NotEnoughData => write!(f, "Not enough data to read"),
Error::UnsupportedVersion(v) => write!(f, "Unsupported version: {v}"),
Error::Compression(msg) => write!(f, "Compression error: {msg}"),
Error::Io(msg) => write!(f, "I/O error: {msg}"),
Error::Serde(msg) => write!(f, "Serialization error: {msg}"),
Error::Custom(msg) => write!(f, "Error: {msg}"),
}
}
}
#[cfg(feature = "std")]
impl StdError for Error {}
impl serde::ser::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::Serde(msg.to_string())
}
}
impl serde::de::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::Serde(msg.to_string())
}
}
#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::Io(err.to_string())
}
}
#[cfg(feature = "compression")]
impl From<lz4_flex::block::DecompressError> for Error {
fn from(err: lz4_flex::block::DecompressError) -> Self {
Error::Compression(format!("LZ4 decompression failed: {err:?}"))
}
}
#[cfg(feature = "compression")]
impl From<lz4_flex::block::CompressError> for Error {
fn from(err: lz4_flex::block::CompressError) -> Self {
Error::Compression(format!("LZ4 compression failed: {err:?}"))
}
}