use crate::exception::Exception;
use core::{
error,
fmt::{self, Debug, Display, Formatter},
};
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Error {
ArgumentCount,
ConsExpected,
BytecodeEnd,
Format(fmt::Error),
InvalidMemoryAccess,
IllegalInstruction,
IllegalPrimitive,
NumberExpected,
OutOfMemory,
ProcedureExpected,
}
impl Exception for Error {
fn is_critical(&self) -> bool {
match self {
Self::ArgumentCount
| Self::InvalidMemoryAccess
| Self::IllegalPrimitive
| Self::NumberExpected
| Self::ConsExpected
| Self::ProcedureExpected => false,
Self::BytecodeEnd | Self::Format(_) | Self::IllegalInstruction | Self::OutOfMemory => {
true
}
}
}
}
impl error::Error for Error {}
impl Display for Error {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match self {
Self::ArgumentCount => write!(formatter, "invalid argument count"),
Self::BytecodeEnd => write!(formatter, "unexpected end of bytecode"),
Self::ConsExpected => write!(formatter, "cons expected"),
Self::Format(error) => write!(formatter, "{error}"),
Self::InvalidMemoryAccess => write!(formatter, "invalid memory access"),
Self::IllegalInstruction => write!(formatter, "illegal instruction"),
Self::IllegalPrimitive => write!(formatter, "illegal primitive"),
Self::NumberExpected => write!(formatter, "number expected"),
Self::OutOfMemory => write!(formatter, "out of memory"),
Self::ProcedureExpected => write!(formatter, "procedure expected"),
}
}
}
impl From<fmt::Error> for Error {
fn from(error: fmt::Error) -> Self {
Self::Format(error)
}
}