use std::{fmt, io};
use crate::errors::DBIOError;
use super::footer::SIZE_OF_FOOTER_BYTES;
pub type TableReadResult<T> = Result<T, ReadError>;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ReadError {
FailedToParse(String),
Footer(FooterError),
BlockDecompression(DBIOError),
IO(DBIOError),
FilterBlock(String),
KeyNotFound,
}
impl std::error::Error for ReadError {}
impl fmt::Display for ReadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ReadError::FailedToParse(msg) => {
write!(f, "{}", msg)
}
ReadError::Footer(base_err) => {
write!(f, "{}", base_err)
}
ReadError::BlockDecompression(base_err) => {
write!(
f,
"Failed to decompress a block from the file. The original error was {}",
base_err
)
}
ReadError::IO(base_err) => write!(f, "{}", base_err),
ReadError::FilterBlock(base_err) => write!(f, "Failed attempting to read the filter block. Continuing without filters. Original error: {}", base_err),
ReadError::KeyNotFound => write!(f, "The specified key was not found."),
}
}
}
impl From<io::Error> for ReadError {
fn from(err: io::Error) -> Self {
ReadError::IO(err.into())
}
}
impl From<FooterError> for ReadError {
fn from(err: FooterError) -> Self {
ReadError::Footer(err)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FooterError {
FooterSerialization(usize),
}
impl std::error::Error for FooterError {}
impl fmt::Display for FooterError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FooterError::FooterSerialization(actual_buffer_size) => {
write!(f, "Failed to serialize the footer. The length of the serialized buffer was expected to be {} but was {}", SIZE_OF_FOOTER_BYTES, actual_buffer_size)
}
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BuilderError {
IO(DBIOError),
AlreadyClosed,
OutOfOrder,
Footer(FooterError),
}
impl std::error::Error for BuilderError {}
impl fmt::Display for BuilderError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BuilderError::IO(base_err) => write!(f, "{}", base_err),
BuilderError::AlreadyClosed => write!(
f,
"Attempted to perform an operation when the file had already been closed."
),
BuilderError::OutOfOrder => {
write!(f, "Attempted to add a key but it was out of order.")
}
BuilderError::Footer(base_err) => write!(f, "{}", base_err),
}
}
}
impl From<io::Error> for BuilderError {
fn from(err: io::Error) -> Self {
BuilderError::IO(err.into())
}
}
impl From<FooterError> for BuilderError {
fn from(err: FooterError) -> Self {
BuilderError::Footer(err)
}
}