use crate::{Checksum, CompressionType};
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Io(std::io::Error),
Decompress(CompressionType),
InvalidVersion(u8),
Unrecoverable,
ChecksumMismatch {
got: Checksum,
expected: Checksum,
},
InvalidTag((&'static str, u8)),
InvalidTrailer,
InvalidHeader(&'static str),
Utf8(std::str::Utf8Error),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "LsmTreeError: {self:?}")
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(e) => Some(e),
_ => None,
}
}
}
impl From<sfa::Error> for Error {
fn from(value: sfa::Error) -> Self {
match value {
sfa::Error::Io(e) => Self::from(e),
sfa::Error::ChecksumMismatch { got, expected } => {
log::error!("Archive ToC checksum mismatch");
Self::ChecksumMismatch {
got: got.into(),
expected: expected.into(),
}
}
sfa::Error::InvalidHeader => {
log::error!("Invalid archive header");
Self::Unrecoverable
}
sfa::Error::InvalidVersion => {
log::error!("Invalid archive version");
Self::Unrecoverable
}
sfa::Error::UnsupportedChecksumType => {
log::error!("Invalid archive checksum type");
Self::Unrecoverable
}
}
}
}
impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Self::Io(value)
}
}
pub type Result<T> = std::result::Result<T, Error>;