use std::collections::HashMap;
use js_sys::Array;
use wasm_bindgen::{prelude::Closure, JsValue};
use crate::imports::ObjectExt;
pub trait ToJsValue {
fn to_js_value(&self) -> JsValue;
}
impl ToJsValue for String {
fn to_js_value(&self) -> JsValue {
JsValue::from_str(self)
}
}
impl ToJsValue for &'static str {
fn to_js_value(&self) -> JsValue {
JsValue::from_str(self)
}
}
impl ToJsValue for bool {
fn to_js_value(&self) -> JsValue {
JsValue::from_bool(*self)
}
}
impl ToJsValue for usize {
fn to_js_value(&self) -> JsValue {
JsValue::from_f64(*self as f64)
}
}
impl ToJsValue for isize {
fn to_js_value(&self) -> JsValue {
JsValue::from_f64(*self as f64)
}
}
impl ToJsValue for u64 {
fn to_js_value(&self) -> JsValue {
JsValue::from_f64(*self as f64)
}
}
impl ToJsValue for i64 {
fn to_js_value(&self) -> JsValue {
JsValue::from_f64(*self as f64)
}
}
impl ToJsValue for u32 {
fn to_js_value(&self) -> JsValue {
JsValue::from_f64(*self as f64)
}
}
impl ToJsValue for i32 {
fn to_js_value(&self) -> JsValue {
JsValue::from_f64(*self as f64)
}
}
impl ToJsValue for u16 {
fn to_js_value(&self) -> JsValue {
JsValue::from_f64(*self as f64)
}
}
impl ToJsValue for i16 {
fn to_js_value(&self) -> JsValue {
JsValue::from_f64(*self as f64)
}
}
impl ToJsValue for u8 {
fn to_js_value(&self) -> JsValue {
JsValue::from_f64(*self as f64)
}
}
impl ToJsValue for i8 {
fn to_js_value(&self) -> JsValue {
JsValue::from_f64(*self as f64)
}
}
impl ToJsValue for f32 {
fn to_js_value(&self) -> JsValue {
JsValue::from_f64(*self as f64)
}
}
impl ToJsValue for f64 {
fn to_js_value(&self) -> JsValue {
JsValue::from_f64(*self as f64)
}
}
impl<T> ToJsValue for Option<T>
where
T: ToJsValue,
{
fn to_js_value(&self) -> JsValue {
match self {
Some(val) => val.to_js_value(),
None => JsValue::null(),
}
}
}
impl<T> ToJsValue for Vec<T>
where
T: ToJsValue,
{
fn to_js_value(&self) -> JsValue {
self.iter()
.map(|v| v.to_js_value())
.collect::<Array>()
.into()
}
}
impl<V> ToJsValue for HashMap<String, V>
where
V: ToJsValue,
{
fn to_js_value(&self) -> JsValue {
let obj = ObjectExt::new();
for (k, v) in self {
obj.set(k, v.to_js_value())
}
obj.into()
}
}
impl<T> ToJsValue for Closure<T>
where
T: ?Sized,
{
fn to_js_value(&self) -> JsValue {
self.as_ref().to_owned()
}
}
impl ToJsValue for JsValue {
fn to_js_value(&self) -> JsValue {
self.to_owned()
}
}
impl ToJsValue for () {
fn to_js_value(&self) -> JsValue {
JsValue::undefined()
}
}