use js_export_macro::js_export;
use miden_client::asset::AccountStorageDelta as NativeAccountStorageDelta;
use crate::models::word::Word;
use crate::platform::{JsBytes, JsErr};
use crate::utils::{deserialize_from_bytes, serialize_to_bytes};
#[derive(Clone)]
#[js_export]
pub struct AccountStorageDelta(NativeAccountStorageDelta);
#[js_export]
impl AccountStorageDelta {
pub fn serialize(&self) -> JsBytes {
serialize_to_bytes(&self.0)
}
pub fn deserialize(bytes: JsBytes) -> Result<AccountStorageDelta, JsErr> {
deserialize_from_bytes::<NativeAccountStorageDelta>(&bytes).map(AccountStorageDelta)
}
#[js_export(js_name = "isEmpty")]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn values(&self) -> Vec<Word> {
self.0
.values()
.map(|(_slot_name, value)| value)
.copied()
.map(Into::into)
.collect()
}
}
impl From<NativeAccountStorageDelta> for AccountStorageDelta {
fn from(native_account_storage_delta: NativeAccountStorageDelta) -> Self {
Self(native_account_storage_delta)
}
}
impl From<&NativeAccountStorageDelta> for AccountStorageDelta {
fn from(native_account_storage_delta: &NativeAccountStorageDelta) -> Self {
Self(native_account_storage_delta.clone())
}
}
impl From<AccountStorageDelta> for NativeAccountStorageDelta {
fn from(account_storage_delta: AccountStorageDelta) -> Self {
account_storage_delta.0
}
}
impl From<&AccountStorageDelta> for NativeAccountStorageDelta {
fn from(account_storage_delta: &AccountStorageDelta) -> Self {
account_storage_delta.0.clone()
}
}