use std::sync::Arc;
use gmsol_programs::{gmsol_store::accounts::VirtualInventory, model::VirtualInventoryModel};
use wasm_bindgen::prelude::*;
use crate::utils::zero_copy::{
try_deserialize_zero_copy, try_deserialize_zero_copy_from_base64_with_options,
};
#[wasm_bindgen(js_name = VirtualInventory)]
#[derive(Clone)]
pub struct JsVirtualInventory {
virtual_inventory: Arc<VirtualInventory>,
}
#[wasm_bindgen(js_class = VirtualInventory)]
impl JsVirtualInventory {
pub fn decode_from_base64_with_options(
data: &str,
no_discriminator: Option<bool>,
) -> crate::Result<Self> {
let virtual_inventory = try_deserialize_zero_copy_from_base64_with_options(
data,
no_discriminator.unwrap_or(false),
)?;
Ok(Self {
virtual_inventory: Arc::new(virtual_inventory.0),
})
}
pub fn decode_from_base64(data: &str) -> crate::Result<Self> {
Self::decode_from_base64_with_options(data, None)
}
pub fn decode(data: &[u8]) -> crate::Result<Self> {
let virtual_inventory = try_deserialize_zero_copy(data)?;
Ok(Self {
virtual_inventory: Arc::new(virtual_inventory.0),
})
}
pub fn to_model(&self) -> JsVirtualInventoryModel {
JsVirtualInventoryModel {
model: VirtualInventoryModel::from_parts(self.virtual_inventory.clone()),
}
}
#[wasm_bindgen(js_name = clone)]
pub fn js_clone(&self) -> Self {
self.clone()
}
}
#[wasm_bindgen(js_name = VirtualInventoryModel)]
#[derive(Clone)]
pub struct JsVirtualInventoryModel {
pub(super) model: VirtualInventoryModel,
}
#[wasm_bindgen(js_class = VirtualInventoryModel)]
impl JsVirtualInventoryModel {
#[wasm_bindgen(js_name = clone)]
pub fn js_clone(&self) -> Self {
self.clone()
}
}
impl From<VirtualInventoryModel> for JsVirtualInventoryModel {
fn from(model: VirtualInventoryModel) -> Self {
Self { model }
}
}