Skip to main content

gmsol_sdk/js/
virtual_inventory.rs

1use std::sync::Arc;
2
3use gmsol_programs::{gmsol_store::accounts::VirtualInventory, model::VirtualInventoryModel};
4use wasm_bindgen::prelude::*;
5
6use crate::utils::zero_copy::{
7    try_deserialize_zero_copy, try_deserialize_zero_copy_from_base64_with_options,
8};
9
10/// Wrapper of [`VirtualInventory`].
11#[wasm_bindgen(js_name = VirtualInventory)]
12#[derive(Clone)]
13pub struct JsVirtualInventory {
14    virtual_inventory: Arc<VirtualInventory>,
15}
16
17#[wasm_bindgen(js_class = VirtualInventory)]
18impl JsVirtualInventory {
19    /// Create from base64 encoded account data with options.
20    pub fn decode_from_base64_with_options(
21        data: &str,
22        no_discriminator: Option<bool>,
23    ) -> crate::Result<Self> {
24        let virtual_inventory = try_deserialize_zero_copy_from_base64_with_options(
25            data,
26            no_discriminator.unwrap_or(false),
27        )?;
28
29        Ok(Self {
30            virtual_inventory: Arc::new(virtual_inventory.0),
31        })
32    }
33
34    /// Create from base64 encoded account data.
35    pub fn decode_from_base64(data: &str) -> crate::Result<Self> {
36        Self::decode_from_base64_with_options(data, None)
37    }
38
39    /// Create from account data.
40    pub fn decode(data: &[u8]) -> crate::Result<Self> {
41        let virtual_inventory = try_deserialize_zero_copy(data)?;
42
43        Ok(Self {
44            virtual_inventory: Arc::new(virtual_inventory.0),
45        })
46    }
47
48    /// Convert into [`JsVirtualInventoryModel`].
49    pub fn to_model(&self) -> JsVirtualInventoryModel {
50        JsVirtualInventoryModel {
51            model: VirtualInventoryModel::from_parts(self.virtual_inventory.clone()),
52        }
53    }
54
55    /// Create a clone of this virtual inventory.
56    #[wasm_bindgen(js_name = clone)]
57    pub fn js_clone(&self) -> Self {
58        self.clone()
59    }
60}
61
62/// Wrapper of [`VirtualInventoryModel`].
63#[wasm_bindgen(js_name = VirtualInventoryModel)]
64#[derive(Clone)]
65pub struct JsVirtualInventoryModel {
66    pub(super) model: VirtualInventoryModel,
67}
68
69#[wasm_bindgen(js_class = VirtualInventoryModel)]
70impl JsVirtualInventoryModel {
71    /// Create a clone of this virtual inventory model.
72    #[wasm_bindgen(js_name = clone)]
73    pub fn js_clone(&self) -> Self {
74        self.clone()
75    }
76}
77
78impl From<VirtualInventoryModel> for JsVirtualInventoryModel {
79    fn from(model: VirtualInventoryModel) -> Self {
80        Self { model }
81    }
82}