use std::{fmt, result, str};
#[cfg(feature = "std")]
use std::error;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize))]
pub enum Error {
Null,
Bounds,
ZeroFill,
Unmapped,
Misaligned,
BadMagic,
PeMagic,
Insanity,
Invalid,
Overflow,
Encoding,
Aliasing,
}
impl From<str::Utf8Error> for Error {
fn from(_err: str::Utf8Error) -> Error {
Error::Encoding
}
}
impl Error {
pub fn is_null(self) -> bool {
self == Error::Null
}
pub fn to_str(self) -> &'static str {
match self {
Error::Null => "null address reference",
Error::Bounds => "bounds check failed",
Error::ZeroFill => "zero filled data reference",
Error::Unmapped => "overlay data reference",
Error::Misaligned => "address misaligned",
Error::BadMagic => "unknown magic number",
Error::PeMagic => "try again with correct parser",
Error::Insanity => "data insanity",
Error::Invalid => "invalid data",
Error::Overflow => "overflow error",
Error::Encoding => "encoding error",
Error::Aliasing => "aliasing error",
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.to_str())
}
}
#[cfg(feature = "std")]
impl error::Error for Error {
fn description(&self) -> &str {
self.to_str()
}
}