use std::error::Error;
use std::fmt;
pub type Result<T> = std::result::Result<T, PageError>;
#[derive(Debug)]
pub enum PageError {
InvalidVersion,
BadSegmentCount,
MissingMagic,
TooMuchData,
NotEnoughData,
Io(std::io::Error),
}
impl fmt::Display for PageError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PageError::InvalidVersion => {
write!(f, "Invalid stream structure version (Should always be 0)")
},
PageError::BadSegmentCount => write!(f, "Page has a segment count < 1"),
PageError::MissingMagic => write!(f, "Page is missing a magic signature"),
PageError::TooMuchData => write!(f, "Too much data was provided"),
PageError::NotEnoughData => {
write!(f, "Too little data is available for the expected read")
},
PageError::Io(err) => write!(f, "{}", err),
}
}
}
impl Error for PageError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match *self {
PageError::Io(ref e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for PageError {
fn from(err: std::io::Error) -> PageError {
PageError::Io(err)
}
}