use std::{
convert::From,
fmt::{Display, Formatter},
string::String
};
#[derive(Debug)]
pub enum Error
{
Checksum(u32, u32),
Io(std::io::Error),
TypeError(&'static str, &'static str),
PropCountExceeded(usize),
MissingProp(&'static str),
Truncation(&'static str),
Corruption(String),
Utf8(&'static str),
Unsupported(String),
Capacity(usize),
Deflate(&'static str),
Inflate(&'static str),
Other(String)
}
impl From<std::io::Error> for Error
{
fn from(e: std::io::Error) -> Self
{
return Error::Io(e);
}
}
impl From<&str> for Error
{
fn from(e: &str) -> Self
{
return Error::Other(String::from(e));
}
}
impl Display for Error
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result
{
return match self {
Error::Checksum(expected, actual) => f.write_str(&format!(
"checksum validation failed (expected {}, got {})",
expected, actual
)),
Error::Io(e) => f.write_str(&format!("io error ({})", e)),
Error::TypeError(expected, actual) => {
f.write_str(&format!("incompatible types (expected {}, got {})", expected, actual))
},
Error::PropCountExceeded(v) => f.write_str(&format!("BPXSD - too many props (count {}, max is 256)", v)),
Error::MissingProp(v) => f.write_str(&format!("BPXSD - missing property {}", v)),
Error::Truncation(e) => f.write_str(&format!(
"unexpected EOF while reading {}, are you sure the data is not truncated?",
e
)),
Error::Corruption(e) => f.write_str(&format!("illegal bytes found ({})", e)),
Error::Utf8(e) => f.write_str(&format!("utf8 decoding/encoding error in {}", e)),
Error::Unsupported(e) => f.write_str(&format!("unsupported operation {}", e)),
Error::Capacity(e) => f.write_str(&format!(
"section capacity exceeded (found {} bytes, max is 2 pow 32 bytes)",
e
)),
Error::Deflate(e) => f.write_str(&format!("deflate error ({})", e)),
Error::Inflate(e) => f.write_str(&format!("inflate error ({})", e)),
Error::Other(e) => f.write_str(&format!("{}", e))
};
}
}