git.delta.rocks / unique-network / refs/commits / 7faf112aedc7

difftreelog

source

pallets/rmrk-proxy/src/misc.rs2.3 KiBsourcehistory
1use super::*;2use codec::{Encode, Decode};3use frame_support::dispatch::Vec;4use pallet_nonfungible::NonfungibleHandle;56#[macro_export]7macro_rules! rmrk_property {8    ($key:ident, $value:expr) => {9        Property {10            key: rmrk_property!(@raw $key),11            value: $value.into()12        }13    };1415    (@raw $key:ident) => {16        RmrkProperty::$key.to_key()17    };1819    ($key:ident) => {20        PropertyScope::Rmrk.apply(rmrk_property!(@raw $key)).unwrap()21    };22}2324macro_rules! impl_rmrk_value {25    ($enum_name:path, decode_error: $error:ident) => {26        impl Into<PropertyValue> for $enum_name {27            fn into(self) -> PropertyValue {28                self.encode().try_into().unwrap()29            }30        }3132        impl TryFrom<&PropertyValue> for $enum_name {33            type Error = MiscError;3435            fn try_from(value: &PropertyValue) -> Result<Self, Self::Error> {36                let mut value = value.as_slice();3738                <$enum_name>::decode(&mut value)39                    .map_err(|_| MiscError::$error)40            }41        }4243    };44}4546pub enum MiscError {47    CorruptedCollectionType,48}4950impl<T: Config> From<MiscError> for Error<T> {51    fn from(error: MiscError) -> Self {52        match error {53            MiscError::CorruptedCollectionType => Self::CorruptedCollectionType,54        }55    }56}5758pub trait IntoNftCollection<T: Config> {59    fn into_nft_collection(self) -> Result<NonfungibleHandle<T>, Error<T>>;60}6162impl<T: Config> IntoNftCollection<T> for CollectionHandle<T> {63    fn into_nft_collection(self) -> Result<NonfungibleHandle<T>, Error<T>> {64        match self.mode {65            CollectionMode::NFT => Ok(NonfungibleHandle::cast(self)),66            _ => Err(<Error<T>>::NotRmrkCollection)67        }68    }69}7071pub enum RmrkProperty {72    Metadata,73    CollectionType,74}7576impl RmrkProperty {77    pub fn to_key(self) -> PropertyKey {78        let key = |str_key: &str| {79            PropertyKey::try_from(80                str_key.bytes().collect::<Vec<_>>()81            ).unwrap()82        };8384        match self {85            Self::Metadata => key("metadata"),86            Self::CollectionType => key("collection-type"),87        }88    }89}9091#[derive(Encode, Decode, PartialEq, Eq)]92pub enum CollectionType {93    Regular,94    Resource,95    Base,96}9798impl_rmrk_value!(CollectionType, decode_error: CorruptedCollectionType);