use js_sys::ArrayBuffer;
use js_sys::Uint8Array;
use wasm_bindgen::{JsCast, JsValue};
pub type Key = Vec<u8>;
pub type Val = Vec<u8>;
pub struct Kv {
pub key: Key,
pub val: Val,
}
pub trait Convert<T> {
fn convert(self) -> T;
}
impl Convert<Vec<u8>> for JsValue {
fn convert(self) -> Vec<u8> {
if self.has_type::<ArrayBuffer>() {
let v = Uint8Array::new(&self);
return v.to_vec();
}
if self.has_type::<Uint8Array>() {
let v: Uint8Array = self.unchecked_into();
return v.to_vec();
}
vec![]
}
}
impl Convert<JsValue> for Vec<u8> {
fn convert(self) -> JsValue {
JsValue::from(Uint8Array::from(&self[..]))
}
}