use thiserror::Error;
#[derive(Debug, Clone, Error)]
pub enum StdlibError {
#[error("{function}: expected {expected} argument(s), got {got}")]
WrongArgCount {
function: String,
expected: usize,
got: usize,
},
#[error("{function}: argument {position} expected {expected}, got {got}")]
TypeMismatch {
function: String,
position: usize,
expected: String,
got: String,
},
#[error("Assertion failed: {message}")]
AssertionFailed { message: String },
#[error("Unknown function: {module}.{function}")]
UnknownFunction { module: String, function: String },
#[error("{0}")]
RuntimeError(String),
#[error("{module}.{function}: capability call requires host (cap_id={cap_id}, fn_id={fn_id})")]
CapabilityCall {
module: String,
function: String,
cap_id: u32,
fn_id: u32,
args: Vec<crate::value::Value>,
},
}
impl StdlibError {
pub fn wrong_args(function: &str, expected: usize, got: usize) -> Self {
Self::WrongArgCount {
function: function.to_string(),
expected,
got,
}
}
pub fn type_mismatch(function: &str, position: usize, expected: &str, got: &str) -> Self {
Self::TypeMismatch {
function: function.to_string(),
position,
expected: expected.to_string(),
got: got.to_string(),
}
}
pub fn unknown_function(module: &str, function: &str) -> Self {
Self::UnknownFunction {
module: module.to_string(),
function: function.to_string(),
}
}
pub fn capability_call(
module: &str,
function: &str,
cap_id: u32,
fn_id: u32,
args: Vec<crate::value::Value>,
) -> Self {
Self::CapabilityCall {
module: module.to_string(),
function: function.to_string(),
cap_id,
fn_id,
args,
}
}
}