1use super::*;2use codec::{Encode, Decode};3use pallet_nonfungible::NonfungibleHandle;45#[macro_export]6macro_rules! rmrk_property {7 ($key:ident, $value:expr) => {8 Property {9 key: rmrk_property!($key),10 value: $value.into()11 }12 };1314 ($key:ident) => {15 RmrkProperty::$key.to_key()16 };17}1819macro_rules! impl_rmrk_value {20 ($enum_name:path, decode_error: $error:ident) => {21 impl Into<PropertyValue> for $enum_name {22 fn into(self) -> PropertyValue {23 self.encode().try_into().unwrap()24 }25 }2627 impl TryFrom<&PropertyValue> for $enum_name {28 type Error = MiscError;2930 fn try_from(value: &PropertyValue) -> Result<Self, Self::Error> {31 let mut value = value.as_slice();3233 <$enum_name>::decode(&mut value)34 .map_err(|_| MiscError::$error)35 }36 }3738 };39}4041pub enum MiscError {42 CorruptedCollectionType,43}4445impl<T: Config> From<MiscError> for Error<T> {46 fn from(error: MiscError) -> Self {47 match error {48 MiscError::CorruptedCollectionType => Self::CorruptedCollectionType,49 }50 }51}5253pub trait IntoNftCollection<T: Config> {54 fn into_nft_collection(self) -> Result<NonfungibleHandle<T>, Error<T>>;55}5657impl<T: Config> IntoNftCollection<T> for CollectionHandle<T> {58 fn into_nft_collection(self) -> Result<NonfungibleHandle<T>, Error<T>> {59 match self.mode {60 CollectionMode::NFT => Ok(NonfungibleHandle::cast(self)),61 _ => Err(<Error<T>>::NotRmrkCollection)62 }63 }64}6566pub enum RmrkProperty {67 Metadata,68 CollectionType,69}7071impl RmrkProperty {72 pub fn to_key(self) -> PropertyKey {73 let key = |str_key: &str| {74 PropertyKey::try_from(75 str_key.bytes().collect::<Vec<_>>()76 ).unwrap()77 };7879 match self {80 Self::Metadata => key("metadata"),81 Self::CollectionType => key("collection-type"),82 }83 }84}8586#[derive(Encode, Decode, PartialEq, Eq)]87pub enum CollectionType {88 Regular,89 Resource,90 Base,91}9293impl_rmrk_value!(CollectionType, decode_error: CorruptedCollectionType);