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
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use wasm_bindgen::{JsCast, JsValue};

pub trait ToJs {
    fn to_js(&self) -> JsValue;

    /// return `None` to leave property unset
    ///
    /// return `Some(value)` to set property
    fn to_js_property_value(&self) -> Option<JsValue> {
        Some(self.to_js())
    }
}

/// # Convert as js
///
/// `Some(value)` => `value`
///
/// `None`        => `undefined`
///
/// # Convert as js property value
///
/// `Some(value)` => property set to `value`
///
/// `None`        => property unset
impl<T> ToJs for Option<T>
where
    T: ToJs,
{
    fn to_js(&self) -> JsValue {
        if let Some(v) = self {
            v.to_js()
        } else {
            JsValue::UNDEFINED
        }
    }

    fn to_js_property_value(&self) -> Option<JsValue> {
        self.as_ref().map(|v| v.to_js())
    }
}

macro_rules! impl_to_js {
    (deref_copy: $($n:ty)*) => ($(
        impl ToJs for $n {
            #[inline]
            fn to_js(&self) -> JsValue {
                JsValue::from(*self)
            }
        }
    )*);
    (into: $($n:ty)*) => ($(
        impl ToJs for $n {
            #[inline]
            fn to_js(&self) -> JsValue {
                JsValue::from(self)
            }
        }
    )*);
}

impl_to_js! {
    into:
    String
    js_sys::Object
    js_sys::Array
    js_sys::JsString
    js_sys::Number
    js_sys::BigInt
    js_sys::Boolean
    js_sys::Function
}

impl_to_js! {
    deref_copy:
    &str
    // numbers https://docs.rs/wasm-bindgen/0.2.78/src/wasm_bindgen/lib.rs.html#849
    i8 u8 i16 u16 i32 u32 f32 f64
    // big_numbers https://docs.rs/wasm-bindgen/0.2.78/src/wasm_bindgen/lib.rs.html#869
    i64 u64 i128 u128 isize usize
    bool
}

impl<T: JsCast> ToJs for &T {
    fn to_js(&self) -> JsValue {
        JsValue::from(*self)
    }
}

macro_rules! impl_js_for_iter {
    ($($t:tt)+) => {
        $($t)+ {
            #[inline]
            fn to_js(&self) -> JsValue {
                js_sys::Array::from_iter(self.iter().map(|v| v.to_js())).into()
            }
        }
    };
}

impl_js_for_iter! { impl<T: ToJs> ToJs for Vec<T> }
impl_js_for_iter! { impl<T: ToJs> ToJs for &[T] }
impl_js_for_iter! { impl<N: ToJs, const S: usize> ToJs for [N; S] }

macro_rules! impl_js_for_tuple {
    (@impl $arr_method:ident ( $($t:ident),+ $(,)? )) => {
        impl<$($t: ToJs),+> ToJs for ($($t),+ ,) {
            #[inline]
            fn to_js(&self) -> JsValue {
                #![allow(non_snake_case)]
                let ($($t),+ ,) = self;
                js_sys::Array::$arr_method(
                    $(&$t.to_js()),+
                ).into()
            }
        }
    };
    (@impl ( $($t:ident),+ $(,)? )) => {
        impl<$($t: ToJs),+> ToJs for ($($t),+ ,) {
            #[inline]
            fn to_js(&self) -> JsValue {
                #![allow(non_snake_case)]
                let ($($t),+ ,) = self;
                js_sys::Array::from_iter([
                    $($t.to_js()),+
                ]).into()
            }
        }
    };
    ( $( $($arr_method:ident)? ( $($t:ident),+ $(,)? ) )* ) => {
        $(
            impl_js_for_tuple! { @impl $($arr_method)? ($($t),+ ,) }
        )*
    };
}

impl_js_for_tuple! {
    of1(T0,)
    of2(T0,T1)
    of3(T0,T1,T2)
    of4(T0,T1,T2,T3)
    of5(T0,T1,T2,T3,T4)
    (T0,T1,T2,T3,T4,T5)
    (T0,T1,T2,T3,T4,T5,T6)
    (T0,T1,T2,T3,T4,T5,T6,T7)
    (T0,T1,T2,T3,T4,T5,T6,T7,T8)
    (T0,T1,T2,T3,T4,T5,T6,T7,T8,T9)
}

impl<T: ToJs> ToJs for Box<T> {
    #[inline]
    fn to_js(&self) -> JsValue {
        self.as_ref().to_js()
    }
}

impl ToJs for () {
    fn to_js(&self) -> JsValue {
        JsValue::UNDEFINED
    }
}

impl ToJs for JsValue {
    fn to_js(&self) -> JsValue {
        self.clone()
    }
}