use crate::java_error::JavaError;
pub type Result<T, E = Error> = core::result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
ClassFileError(#[from] ristretto_classfile::Error),
#[error(transparent)]
ClassLoaderError(#[from] ristretto_classloader::Error),
#[error(transparent)]
JitError(#[from] ristretto_jit::Error),
#[error("Configuration error: {0}")]
ConfigurationError(String),
#[error("Internal error: {0}")]
InternalError(String),
#[error("Invalid constant; expected {expected}, found {actual}")]
InvalidConstant { expected: String, actual: String },
#[error("Invalid instant index: {0}")]
InvalidConstantIndex(u16),
#[error("Invalid local variable; expected {expected}, found {actual}")]
InvalidLocalVariable { expected: String, actual: String },
#[error("Invalid local variable index: {0}")]
InvalidLocalVariableIndex(usize),
#[error("Invalid operand; expected {expected}, found {actual}")]
InvalidOperand { expected: String, actual: String },
#[error("Invalid program counter: {0}")]
InvalidProgramCounter(usize),
#[error("Invalid stack value; expected {expected}, found {actual}")]
InvalidStackValue { expected: String, actual: String },
#[error(transparent)]
JavaError(#[from] JavaError),
#[error("Operand stack overflow")]
OperandStackOverflow,
#[error("Operand stack underflow")]
OperandStackUnderflow,
#[error("Parameters stack underflow")]
ParametersUnderflow,
#[error(transparent)]
ParseIntError(#[from] std::num::ParseIntError),
#[error("Poisoned lock: {0}")]
PoisonedLock(String),
#[error("java.lang.Throwable: {0}")]
Throwable(ristretto_classloader::Object),
#[error(transparent)]
TryFromIntError(#[from] std::num::TryFromIntError),
#[error("Unsupported class file version: {0}")]
UnsupportedClassFileVersion(u16),
}
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Error::InternalError(error.to_string())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_io_error() {
let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let error = Error::from(io_error);
assert_eq!(error.to_string(), "Internal error: file not found");
}
}