use alloc::string::{String, ToString};
use core::fmt;
#[derive(Debug)]
pub enum Trap {
DowncastError(&'static str),
ScriptOutOfBounds,
MissingGlobal(String),
Runtime(String),
}
impl Trap {
pub fn runtime(err: impl ToString) -> Trap {
Trap::Runtime(err.to_string())
}
}
impl fmt::Display for Trap {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::DowncastError(ty_name) => {
f.write_fmt(format_args!("could not convert to type: {}", ty_name))
}
Self::ScriptOutOfBounds => f.write_str("script out of bounds"),
Self::MissingGlobal(global_name) => f.write_fmt(format_args!(
"could not find global variable: {}",
global_name
)),
Self::Runtime(err) => f.write_str(err),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Trap {}