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
use crate::utils::*;
use crate::{error::Error, jsvalue::JsValueTrait};
use js_sys::{Array, Object, Reflect};
use wasm_bindgen::prelude::*;
pub trait ObjectTrait {
fn get(&self, prop: &str) -> Result<JsValue, Error>;
fn get_string(&self, prop: &str) -> Result<String, Error>;
fn get_u8(&self, prop: &str) -> Result<u8, Error>;
fn get_u16(&self, prop: &str) -> Result<u16, Error>;
fn get_u32(&self, prop: &str) -> Result<u32, Error>;
fn get_u64(&self, prop: &str) -> Result<u64, Error>;
fn get_f64(&self, prop: &str) -> Result<f64, Error>;
fn get_bool(&self, prop: &str) -> Result<bool, Error>;
fn get_vec(&self, prop: &str) -> Result<Vec<JsValue>, Error>;
fn get_vec_u8(&self, prop: &str) -> Result<Vec<u8>, Error>;
fn get_vec_u8_from_number_array(&self, prop: &str) -> Result<Vec<u8>, Error>;
fn get_vec_u8_from_uint8_array(&self, prop: &str) -> Result<Vec<u8>, Error>;
fn set(&self, prop: &str, value: &JsValue) -> Result<bool, Error>;
fn set_vec(&self, prop: &str, values: &[JsValue]) -> Result<bool, Error>;
fn set_properties(&self, props: &[(&str, &JsValue)]) -> Result<(), Error>;
fn delete(&self, prop: &str) -> Result<bool, Error>;
}
impl ObjectTrait for Object {
fn get(&self, prop: &str) -> Result<JsValue, Error> {
Ok(Reflect::get(self, &JsValue::from(prop))?)
}
fn get_string(&self, prop: &str) -> Result<String, Error> {
try_get_string_from_prop(self, prop)
}
fn get_u8(&self, prop: &str) -> Result<u8, Error> {
try_get_u8_from_prop(self, prop)
}
fn get_u16(&self, prop: &str) -> Result<u16, Error> {
try_get_u16_from_prop(self, prop)
}
fn get_u32(&self, prop: &str) -> Result<u32, Error> {
try_get_u32_from_prop(self, prop)
}
fn get_u64(&self, prop: &str) -> Result<u64, Error> {
try_get_u64_from_prop(self, prop)
}
fn get_bool(&self, prop: &str) -> Result<bool, Error> {
try_get_bool_from_prop(self, prop)
}
fn get_vec(&self, prop: &str) -> Result<Vec<JsValue>, Error> {
let v = Reflect::get(self, &JsValue::from(prop))?;
let array = v.dyn_into::<Array>()?;
Ok(array.to_vec())
}
fn get_vec_u8(&self, prop: &str) -> Result<Vec<u8>, Error> {
let v = Reflect::get(self, &JsValue::from(prop))?;
v.try_as_vec_u8()
}
fn get_vec_u8_from_number_array(&self, prop: &str) -> Result<Vec<u8>, Error> {
try_get_vec_u8_from_number_array_prop(self, prop)
}
fn get_vec_u8_from_uint8_array(&self, prop: &str) -> Result<Vec<u8>, Error> {
try_get_vec_u8_from_uint8_array_prop(self, prop)
}
fn get_f64(&self, prop: &str) -> Result<f64, Error> {
try_get_f64_from_prop(self, prop)
}
fn set(&self, prop: &str, value: &JsValue) -> Result<bool, Error> {
Ok(Reflect::set(self, &JsValue::from(prop), value)?)
}
fn set_vec(&self, prop: &str, values: &[JsValue]) -> Result<bool, Error> {
let array = js_sys::Array::new();
for v in values {
array.push(v);
}
Ok(Reflect::set(self, &JsValue::from(prop), &array)?)
}
fn set_properties(&self, props: &[(&str, &JsValue)]) -> Result<(), Error> {
for (k, v) in props.iter() {
Reflect::set(self, &JsValue::from(*k), v)?;
}
Ok(())
}
fn delete(&self, prop: &str) -> Result<bool, Error> {
Ok(Reflect::delete_property(self, &JsValue::from(prop))?)
}
}