workflow_wasm/
printable.rs

1//! Printable representation for a JsValue. Will print the string
2//! representation if it is a string, otherwise
3//! will output the debug representation.
4
5use wasm_bindgen::prelude::*;
6
7#[derive(Clone, Debug)]
8pub struct Printable(JsValue);
9
10unsafe impl Send for Printable {}
11unsafe impl Sync for Printable {}
12
13impl Printable {
14    pub fn new(value: JsValue) -> Self {
15        Self(value)
16    }
17}
18
19impl std::fmt::Display for Printable {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        if let Some(string) = self.0.as_string() {
22            write!(f, "{}", string)
23        } else {
24            write!(f, "{:?}", self.0)
25        }
26    }
27}
28
29impl AsRef<JsValue> for Printable {
30    fn as_ref(&self) -> &JsValue {
31        &self.0
32    }
33}
34
35impl From<JsValue> for Printable {
36    fn from(value: JsValue) -> Self {
37        Self(value)
38    }
39}
40
41impl From<JsError> for Printable {
42    fn from(value: JsError) -> Self {
43        Self(value.into())
44    }
45}
46
47impl From<&Printable> for JsValue {
48    fn from(value: &Printable) -> Self {
49        value.0.clone()
50    }
51}