1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#[cfg(feature = "anchor")]
use anchor_lang::prelude::AnchorSerialize;
#[cfg(not(feature = "anchor"))]
use borsh::BorshSerialize;

use crate::{
    accounts::{BaseAssetV1, PluginHeaderV1},
    registry_records_to_external_plugin_adapter_list, registry_records_to_plugin_list, Asset,
    PluginRegistryV1Safe,
};

impl Asset {
    pub fn deserialize(data: &[u8]) -> Result<Self, std::io::Error> {
        let base = BaseAssetV1::from_bytes(data)?;
        let base_data = base.try_to_vec()?;
        let (plugin_header, plugin_list, external_plugin_adapter_list) = if base_data.len()
            != data.len()
        {
            let plugin_header = PluginHeaderV1::from_bytes(&data[base_data.len()..])?;
            let plugin_registry = PluginRegistryV1Safe::from_bytes(
                &data[plugin_header.plugin_registry_offset as usize..],
            )?;

            let plugin_list = registry_records_to_plugin_list(&plugin_registry.registry, data)?;
            let external_plugin_adapter_list = registry_records_to_external_plugin_adapter_list(
                &plugin_registry.external_registry,
                data,
            )?;

            (
                Some(plugin_header),
                Some(plugin_list),
                Some(external_plugin_adapter_list),
            )
        } else {
            (None, None, None)
        };

        Ok(Self {
            base,
            plugin_list: plugin_list.unwrap_or_default(),
            external_plugin_adapter_list: external_plugin_adapter_list.unwrap_or_default(),
            plugin_header,
        })
    }

    #[inline(always)]
    pub fn from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
        Self::deserialize(data)
    }
}

impl<'a> TryFrom<&solana_program::account_info::AccountInfo<'a>> for Asset {
    type Error = std::io::Error;

    fn try_from(
        account_info: &solana_program::account_info::AccountInfo<'a>,
    ) -> Result<Self, Self::Error> {
        let data: &[u8] = &(*account_info.data).borrow();
        Self::deserialize(data)
    }
}