use std::ops::Deref;
use bincode::ErrorKind;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
#[error("Error converting IR to string")]
Format(#[from] std::fmt::Error),
#[error("Unexpected end of bytes. Deserialiser expected more bytes in the decompress slice")]
UnexpectedEndOfBytes,
#[error("Could not deserialize invalid unicode scalar value")]
InvalidUnicodeChar,
#[error("Unexpected bincode error")]
OtherBincode(bincode::Error),
}
impl From<bincode::Error> for Error {
fn from(value: bincode::Error) -> Self {
match value.deref() {
ErrorKind::InvalidCharEncoding => {
Error::InvalidUnicodeChar
}
ErrorKind::SizeLimit => {
Error::UnexpectedEndOfBytes
}
ErrorKind::Io(e) => {
match e.kind() {
std::io::ErrorKind::UnexpectedEof => {
Error::UnexpectedEndOfBytes
}
_ => {
Error::OtherBincode(value)
}
}
}
_ => {
Error::OtherBincode(value)
}
}
}
}