From c22a6144da2806871d0df521762e2d0e334dcea8 Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Thu, 19 May 2022 14:58:11 +0000 Subject: [PATCH] feat: add all rmrk properties --- --- a/pallets/rmrk-proxy/src/lib.rs +++ b/pallets/rmrk-proxy/src/lib.rs @@ -27,8 +27,10 @@ pub use pallet::*; pub mod misc; +pub mod property; use misc::*; +pub use property::*; #[frame_support::pallet] pub mod pallet { @@ -69,6 +71,7 @@ /* Unique-specific events */ CorruptedCollectionType, NotRmrkCollection, + RmrkPropertyIsTooLong, /* RMRK compatible events */ CollectionNotEmpty, @@ -113,8 +116,8 @@ &collection, PropertyScope::Rmrk, [ - rmrk_property!(Metadata, metadata), - rmrk_property!(CollectionType, CollectionType::Regular), + rmrk_property!(Config=T, Metadata: metadata)?, + rmrk_property!(Config=T, CollectionType: CollectionType::Regular)?, ].into_iter() )?; @@ -215,7 +218,7 @@ fn get_collection_type(collection_id: CollectionId) -> Result { let collection_type: CollectionType = >::collection_properties(collection_id) - .get(&rmrk_property!(CollectionType)) + .get(&rmrk_property!(Config=T, CollectionType)?) .ok_or(>::NotRmrkCollection)? .try_into() .map_err(>::from)?; --- a/pallets/rmrk-proxy/src/misc.rs +++ b/pallets/rmrk-proxy/src/misc.rs @@ -1,31 +1,12 @@ use super::*; use codec::{Encode, Decode}; -use frame_support::dispatch::Vec; use pallet_nonfungible::NonfungibleHandle; - -#[macro_export] -macro_rules! rmrk_property { - ($key:ident, $value:expr) => { - Property { - key: rmrk_property!(@raw $key), - value: $value.into() - } - }; - (@raw $key:ident) => { - RmrkProperty::$key.to_key() - }; - - ($key:ident) => { - PropertyScope::Rmrk.apply(rmrk_property!(@raw $key)).unwrap() - }; -} - macro_rules! impl_rmrk_value { ($enum_name:path, decode_error: $error:ident) => { - impl Into for $enum_name { - fn into(self) -> PropertyValue { - self.encode().try_into().unwrap() + impl From<$enum_name> for PropertyValue { + fn from(e: $enum_name) -> Self { + e.encode().try_into().unwrap() } } @@ -64,26 +45,6 @@ match self.mode { CollectionMode::NFT => Ok(NonfungibleHandle::cast(self)), _ => Err(>::NotRmrkCollection) - } - } -} - -pub enum RmrkProperty { - Metadata, - CollectionType, -} - -impl RmrkProperty { - pub fn to_key(self) -> PropertyKey { - let key = |str_key: &str| { - PropertyKey::try_from( - str_key.bytes().collect::>() - ).unwrap() - }; - - match self { - Self::Metadata => key("metadata"), - Self::CollectionType => key("collection-type"), } } } --- /dev/null +++ b/pallets/rmrk-proxy/src/property.rs @@ -0,0 +1,92 @@ +use super::*; +use core::convert::AsRef; + +pub enum RmrkProperty { + Metadata, + CollectionType, + Recipient, + Royalty, + Equipped, + Pending, + ResourceCollection, + ResourcePriorities, + PendingRemoval, + Parts, + Base, + Src, + Slot, + License, + Thumb, + EquippedNft, + BaseType, + // // RmrkPartId(/* Id type? */) + EquippableList, + ZIndex, + ThemeName, + ThemeProperty(RmrkString), + ThemeInherit, +} + +impl RmrkProperty { + pub fn to_key(self) -> Result> { + fn get_bytes>(container: &T) -> &[u8] { + container.as_ref() + } + + macro_rules! key { + ($($component:expr),+) => { + PropertyKey::try_from([$(key!(@ &$component)),+].concat()) + .map_err(|_| >::RmrkPropertyIsTooLong) + }; + + (@ $key:expr) => { + get_bytes($key) + }; + } + + match self { + Self::Metadata => key!("metadata"), + Self::CollectionType => key!("collection-type"), + Self::Recipient => key!("recipient"), + Self::Royalty => key!("royalty"), + Self::Equipped => key!("equipped"), + Self::Pending => key!("pending"), + Self::ResourceCollection => key!("resource-collection"), + Self::ResourcePriorities => key!("resource-priorities"), + Self::PendingRemoval => key!("pending-removal"), + Self::Parts => key!("parts"), + Self::Base => key!("base"), + Self::Src => key!("src"), + Self::Slot => key!("slot"), + Self::License => key!("license"), + Self::Thumb => key!("thumb"), + Self::EquippedNft => key!("equipped-nft"), + Self::BaseType => key!("base-type"), + // RmrkPartId(/* Id type? */) + Self::EquippableList => key!("equippable-list"), + Self::ZIndex => key!("z-index"), + Self::ThemeName => key!("theme-name"), + Self::ThemeProperty(name) => key!("theme-property-", name), + Self::ThemeInherit => key!("theme-inherit"), + } + } +} + +#[macro_export] +macro_rules! rmrk_property { + (Config=$cfg:ty, $key:ident: $value:expr) => { + rmrk_property!(@$cfg, $key).map(|key| Property { + key, + value: $value.into() + }) + }; + + (@$cfg:ty, $key:ident) => { + $crate::RmrkProperty::$key.to_key::<$cfg>() + }; + + (Config=$cfg:ty, $key:ident) => { + PropertyScope::Rmrk.apply(rmrk_property!(@$cfg, $key)?) + .map_err(|_| >::RmrkPropertyIsTooLong) + }; +} --- a/primitives/data-structs/src/lib.rs +++ b/primitives/data-structs/src/lib.rs @@ -924,4 +924,4 @@ pub type RmrkThemeName = RmrkRpcString; pub type RmrkPropertyKey = RmrkRpcString; -type RmrkString = BoundedVec; +pub type RmrkString = BoundedVec; -- gitstuff