use std::{borrow::Cow, string::FromUtf8Error};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum JavaError {
#[error("Error read from stream")]
ReadError(#[from] StreamError),
#[error("Error converting read object into something useful: {0}")]
ConvertError(#[from] ConversionError),
}
#[derive(Debug, Error)]
pub enum StreamError {
#[error("Unexpected end of stream")]
EndOfStream(#[from] std::io::Error),
#[error("This isn't a JavaObject - magic numbers: {0:X}")]
NonJavaObject(u16),
#[error("Unknown serialization version: {0}")]
UnknownVersion(u16),
#[error("Unknown mark: {0}")]
UnknownMark(u8),
#[error("Unknown type marker: {0}")]
UnrecognisedType(char),
#[error("Unknown reference handle: {0}")]
UnknownReference(u32),
#[error("Invalid reference. Expected {0}")]
InvalidReference(&'static str),
#[error("Invalid Stream: {0}")]
InvalidStream(&'static str),
#[error("{0} isn't implemented yet")]
NotImplemented(&'static str),
}
impl From<FromUtf8Error> for StreamError {
fn from(_: FromUtf8Error) -> Self {
StreamError::InvalidStream("String is not valid UTF-8")
}
}
#[derive(Debug, Error)]
pub enum ConversionError {
#[error("Field '{0}' does not exist")]
FieldNotFound(String),
#[error("No object found")]
NullPointerException,
#[error("Expected '{0}'")]
InvalidType(&'static str),
#[error("Unexpected block data")]
UnexpectedBlockData(Vec<u8>),
#[error("Missing Annotation: {0}")]
MissingAnnotations(usize),
#[error("Expected class '{0}', found '{1}'")]
IncorrectClass(Cow<'static, str>, Cow<'static, str>),
#[error("Class '{0}' was not expected")]
UnexpectedClass(String),
}