#[derive(Debug, Clone)]
pub struct KabelRuntimeError {
pub kind: RuntimeErrorKind,
pub message: String,
pub line: usize,
pub code: String,
}
impl KabelRuntimeError {
pub fn new(kind: RuntimeErrorKind, message: String, line: usize, code: String) -> Self {
Self {
kind,
message,
line,
code,
}
}
}
impl std::fmt::Display for KabelRuntimeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!(
"Error {:0>4}: {1} at line {2}\n\
{3}\n",
self.kind.clone() as usize,
self.message,
self.line + 1,
self.code,
))
}
}
impl std::error::Error for KabelRuntimeError {}
#[derive(Debug, Clone)]
pub enum RuntimeErrorKind {
MismatchedTypes,
WrongType,
}
impl std::fmt::Display for RuntimeErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use RuntimeErrorKind::*;
match self {
MismatchedTypes => f.write_str("Mismatched types"),
WrongType => f.write_str("WrongType"),
}
}
}
impl From<RuntimeErrorKind> for usize {
fn from(value: RuntimeErrorKind) -> Self {
use RuntimeErrorKind::*;
match value {
MismatchedTypes => 0x00,
WrongType => 0x01,
}
}
}