1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! Printable representation for a JsValue. Will print the string
//! representation if it is a string, otherwise
//! will output the debug representation.

use wasm_bindgen::prelude::*;

#[derive(Clone, Debug)]
pub struct Printable(JsValue);

unsafe impl Send for Printable {}
unsafe impl Sync for Printable {}

impl Printable {
    pub fn new(value: JsValue) -> Self {
        Self(value)
    }
}

impl std::fmt::Display for Printable {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(string) = self.0.as_string() {
            write!(f, "{}", string)
        } else {
            write!(f, "{:?}", self.0)
        }
    }
}

impl AsRef<JsValue> for Printable {
    fn as_ref(&self) -> &JsValue {
        &self.0
    }
}

impl From<JsValue> for Printable {
    fn from(value: JsValue) -> Self {
        Self(value)
    }
}

impl From<JsError> for Printable {
    fn from(value: JsError) -> Self {
        Self(value.into())
    }
}

impl From<&Printable> for JsValue {
    fn from(value: &Printable) -> Self {
        value.0.clone()
    }
}