Skip to content

Commit

Permalink
Box Collection deserialization, use helper, add ext plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
danenbm committed Jul 2, 2024
1 parent b8775f2 commit 3d9f25e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
1 change: 1 addition & 0 deletions clients/rust/src/hooked/advanced_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ pub struct Asset {
pub struct Collection {
pub base: BaseCollectionV1,
pub plugin_list: PluginsList,
pub external_plugin_adapter_list: ExternalPluginAdaptersList,
pub plugin_header: Option<PluginHeaderV1>,
}

Expand Down
56 changes: 37 additions & 19 deletions clients/rust/src/hooked/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,64 @@ use borsh::BorshSerialize;

use crate::{
accounts::{BaseCollectionV1, PluginHeaderV1},
registry_records_to_plugin_list, Collection, PluginRegistryV1Safe,
registry_records_to_external_plugin_adapter_list, registry_records_to_plugin_list, Collection,
ExternalPluginAdaptersList, PluginRegistryV1Safe, PluginsList,
};

impl Collection {
pub fn deserialize(data: &[u8]) -> Result<Self, std::io::Error> {
pub fn deserialize(data: &[u8]) -> Result<Box<Self>, std::io::Error> {
let base = BaseCollectionV1::from_bytes(data)?;
let base_data = base.try_to_vec()?;
let (plugin_header, plugin_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)?;
if base_data.len() != data.len() {
return Self::deserialize_with_plugins(data, base, base_data);
}

(Some(plugin_header), Some(plugin_list))
} else {
(None, None)
};
Ok(Box::new(Self {
base,
plugin_list: PluginsList::default(),
external_plugin_adapter_list: ExternalPluginAdaptersList::default(),
plugin_header: None,
}))
}

fn deserialize_with_plugins(
data: &[u8],
base: BaseCollectionV1,
base_data: Vec<u8>,
) -> Result<Box<Self>, std::io::Error> {
let plugin_header = PluginHeaderV1::from_bytes(&data[base_data.len()..])?;
let plugin_registry = PluginRegistryV1Safe::from_bytes(
&data[plugin_header.plugin_registry_offset as usize..],
)?;

Ok(Self {
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,
)?;

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

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

impl<'a> TryFrom<&solana_program::account_info::AccountInfo<'a>> for Collection {
impl<'a> TryFrom<&solana_program::account_info::AccountInfo<'a>> for Box<Collection> {
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)
Collection::deserialize(data)
}
}

0 comments on commit 3d9f25e

Please sign in to comment.