use core::fmt;
use wasm_bindgen::prelude::*;
#[derive(Debug)]
pub struct Error(JsValue);
impl fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_name = String)]
pub fn to_string(value: &JsValue) -> String;
}
to_string(&self.0).fmt(f)
}
}
impl Error {
pub fn new<T: fmt::Display>(msg: T) -> Self {
Error(JsError::new(&msg.to_string()).into())
}
}
#[cfg(feature = "std")]
impl crate_serde::ser::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::new(msg)
}
}
#[cfg(feature = "std")]
impl serde::de::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::new(msg)
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
#[cfg(not(feature = "std"))]
impl crate_serde::ser::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::new(msg)
}
}
#[cfg(not(feature = "std"))]
impl serde::de::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::new(msg)
}
}
impl From<JsValue> for Error {
fn from(error: JsValue) -> Error {
Error(error)
}
}
impl From<Error> for JsValue {
fn from(error: Error) -> JsValue {
error.0
}
}