use among::Among;
use core::fmt::{Debug, Display, Formatter, Result};
#[derive(thiserror::Error)]
pub enum Error {
#[error("missing append-only log header, log may be corrupted")]
CorruptedHeader,
#[error("append-only has bad magic text")]
BadMagicText,
#[error("cannot open append-only because the external magic doesn't match. expected {expected}, found {found}")]
BadExternalMagic {
expected: u16,
found: u16,
},
#[error(
"cannot open append-only because the magic doesn't match. expected {expected}, found {found}"
)]
BadMagic {
expected: u16,
found: u16,
},
#[error("entry checksum mismatch")]
ChecksumMismatch,
#[error("entry data len {len} is greater than the remaining file size {remaining}, append-only file might be corrupted")]
EntryTooLarge {
len: u32,
remaining: u32,
},
#[error(transparent)]
IO(#[from] std::io::Error),
}
impl Debug for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
Display::fmt(self, f)
}
}
impl Error {
#[inline]
pub(super) const fn io<A, B>(err: std::io::Error) -> Among<A, B, Self> {
Among::Right(Self::IO(err))
}
#[inline]
pub(super) const fn checksum_mismatch<A, B>() -> Among<A, B, Self> {
Among::Right(Self::ChecksumMismatch)
}
#[inline]
pub(super) const fn entry_corrupted<A, B>(len: u32, remaining: u32) -> Among<A, B, Self> {
Among::Right(Self::EntryTooLarge { len, remaining })
}
#[inline]
pub(super) const fn bad_external_magic<A, B>(expected: u16, found: u16) -> Among<A, B, Self> {
Among::Right(Self::BadExternalMagic { expected, found })
}
#[inline]
pub(super) const fn bad_magic<A, B>(expected: u16, found: u16) -> Among<A, B, Self> {
Among::Right(Self::BadMagic { expected, found })
}
#[inline]
pub(super) const fn corrupted_header<A, B>() -> Among<A, B, Self> {
Among::Right(Self::CorruptedHeader)
}
}
#[cfg(feature = "std")]
#[inline]
pub(super) fn read_only_error() -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::PermissionDenied, "append log read-only")
}