core_preview/hooked/
asset.rs

1//! This code was AUTOGENERATED using the kinobi library.
2//! Please DO NOT EDIT THIS FILE, instead use visitors
3//! to add features, then rerun kinobi to update it.
4//!
5//! [https://github.com/metaplex-foundation/kinobi]
6//!
7
8use borsh::BorshSerialize;
9
10use crate::{
11    accounts::{BaseAssetV1, PluginHeaderV1, PluginRegistryV1},
12    registry_records_to_plugin_list, Asset,
13};
14
15impl Asset {
16    pub fn deserialize_asset(data: &[u8]) -> Result<Asset, std::io::Error> {
17        let base = BaseAssetV1::from_bytes(data)?;
18        let base_data = base.try_to_vec()?;
19        let (plugin_header, plugin_list) = if base_data.len() != data.len() {
20            let plugin_header = PluginHeaderV1::from_bytes(&data[base_data.len()..])?;
21            let plugin_registry = PluginRegistryV1::from_bytes(
22                &data[plugin_header.plugin_registry_offset as usize..],
23            )?;
24            let plugin_list = registry_records_to_plugin_list(&plugin_registry.registry, data)?;
25
26            (Some(plugin_header), Some(plugin_list))
27        } else {
28            (None, None)
29        };
30
31        Ok(Self {
32            base,
33            plugin_list: plugin_list.unwrap_or_default(),
34            plugin_header,
35        })
36    }
37
38    #[inline(always)]
39    pub fn from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
40        Self::deserialize_asset(data)
41    }
42}
43
44impl<'a> TryFrom<&solana_program::account_info::AccountInfo<'a>> for Asset {
45    type Error = std::io::Error;
46
47    fn try_from(
48        account_info: &solana_program::account_info::AccountInfo<'a>,
49    ) -> Result<Self, Self::Error> {
50        let data: &[u8] = &(*account_info.data).borrow();
51        Self::deserialize_asset(data)
52    }
53}