use wasm_bindgen::prelude::*;
pub struct WasmError;
impl WasmError {
pub fn shape_mismatch() -> JsValue {
JsValue::from_str("Shape mismatch between tensors")
}
pub fn dimension_error(required: usize, got: usize) -> JsValue {
JsValue::from_str(&format!("Requires {}D tensor, got {}D", required, got))
}
pub fn size_mismatch(expected: usize, got: usize) -> JsValue {
JsValue::from_str(&format!(
"Size mismatch: expected {}, got {}",
expected, got
))
}
pub fn empty_tensor() -> JsValue {
JsValue::from_str("Operation requires non-empty tensor")
}
pub fn invalid_range<T: std::fmt::Display>(param: &str, value: T, min: T, max: T) -> JsValue {
JsValue::from_str(&format!(
"{} must be between {} and {}, got {}",
param, min, max, value
))
}
pub fn invalid_param<T: std::fmt::Display>(param: &str, value: T, reason: &str) -> JsValue {
JsValue::from_str(&format!("Invalid {}: {} ({})", param, value, reason))
}
pub fn insufficient_data(operation: &str, required: usize, got: usize) -> JsValue {
JsValue::from_str(&format!(
"{} requires at least {} elements, got {}",
operation, required, got
))
}
pub fn index_out_of_bounds(index: usize, len: usize) -> JsValue {
JsValue::from_str(&format!("Index {} out of bounds for length {}", index, len))
}
pub fn math_domain_error(function: &str, input: f32) -> JsValue {
JsValue::from_str(&format!(
"{} domain error: input {} is invalid",
function, input
))
}
}
pub type WasmResult<T> = Result<T, JsValue>;
#[macro_export]
macro_rules! wasm_error {
(shape_mismatch) => {
WasmError::shape_mismatch()
};
(empty_tensor) => {
WasmError::empty_tensor()
};
(dimension $required:expr, $got:expr) => {
WasmError::dimension_error($required, $got)
};
(insufficient_data $op:expr, $required:expr, $got:expr) => {
WasmError::insufficient_data($op, $required, $got)
};
}