use std::{
error::Error,
fmt::{Debug, Display, Formatter, Result as FmtResult},
};
use mlua::prelude::*;
use lune_utils::fmt::ErrorComponents;
pub type RuntimeResult<T, E = RuntimeError> = Result<T, E>;
#[derive(Debug, Clone)]
pub struct RuntimeError {
error: LuaError,
disable_colors: bool,
}
impl RuntimeError {
#[must_use]
#[doc(hidden)]
pub fn enable_colors(mut self) -> Self {
self.disable_colors = false;
self
}
#[must_use]
#[doc(hidden)]
pub fn disable_colors(mut self) -> Self {
self.disable_colors = true;
self
}
#[must_use]
pub fn is_incomplete_input(&self) -> bool {
matches!(
self.error,
LuaError::SyntaxError {
incomplete_input: true,
..
}
)
}
}
impl From<LuaError> for RuntimeError {
fn from(value: LuaError) -> Self {
Self {
error: value,
disable_colors: false,
}
}
}
impl From<&LuaError> for RuntimeError {
fn from(value: &LuaError) -> Self {
Self {
error: value.clone(),
disable_colors: false,
}
}
}
impl Display for RuntimeError {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "{}", ErrorComponents::from(self.error.clone()))
}
}
impl Error for RuntimeError {
fn cause(&self) -> Option<&dyn Error> {
Some(&self.error)
}
}