ruffle/web/src/utils.rs

35 lines
868 B
Rust

//! Utility functions for the web backend.
use wasm_bindgen::JsValue;
#[derive(Debug)]
pub struct JsError {
value: JsValue,
}
impl std::fmt::Display for JsError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "JS exception: {:?}", self)
}
}
impl std::error::Error for JsError {}
pub trait JsResult<T> {
/// Converts a `JsValue` into a standard `Error`.
fn warn_on_error(&self);
fn into_js_result(self) -> Result<T, JsError>;
}
impl<T> JsResult<T> for Result<T, JsValue> {
#[inline]
fn warn_on_error(&self) {
if let Err(value) = self {
log::warn!("Unexpected JavaScript error: {:?}", value);
}
}
#[inline]
fn into_js_result(self) -> Result<T, JsError> {
self.map_err(|value| JsError { value })
}
}