use std::error::Error;
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ScriptHandle(u64);
impl ScriptHandle {
pub fn new(id: u64) -> Self {
Self(id)
}
pub fn id(&self) -> u64 {
self.0
}
}
#[derive(Debug)]
pub enum ScriptError {
NotFound(String),
SyntaxError(String),
RuntimeError(String),
FunctionNotFound(String),
TypeError(String),
EntityDespawned(String),
}
impl fmt::Display for ScriptError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ScriptError::NotFound(msg) => write!(f, "Script not found: {}", msg),
ScriptError::SyntaxError(msg) => write!(f, "Syntax error: {}", msg),
ScriptError::RuntimeError(msg) => write!(f, "Runtime error: {}", msg),
ScriptError::FunctionNotFound(msg) => write!(f, "Function not found: {}", msg),
ScriptError::TypeError(msg) => write!(f, "Type error: {}", msg),
ScriptError::EntityDespawned(msg) => write!(f, "Entity despawned: {}", msg),
}
}
}
impl Error for ScriptError {}
pub trait ScriptingBackend {
fn load_script(&mut self, path: &str) -> Result<ScriptHandle, ScriptError>;
fn execute_chunk(&mut self, code: &str) -> Result<(), ScriptError>;
fn call_function(&self, handle: ScriptHandle, func_name: &str) -> Result<(), ScriptError>;
fn has_function(&self, handle: ScriptHandle, func_name: &str) -> bool;
fn unload_script(&mut self, handle: ScriptHandle);
fn backend_name(&self) -> &str;
}