git.delta.rocks / unique-network / refs/commits / cf884d838f57

difftreelog

source

pallets/proxy-rmrk-core/src/misc.rs2.9 KiBsourcehistory
1use super::*;2use codec::{Encode, Decode};3use pallet_nonfungible::{NonfungibleHandle, ItemData};45macro_rules! impl_rmrk_value {6    ($enum_name:path, decode_error: $error:ident) => {7        impl TryFrom<&PropertyValue> for $enum_name {8            type Error = MiscError;910            fn try_from(value: &PropertyValue) -> Result<Self, Self::Error> {11                let mut value = value.as_slice();1213                <$enum_name>::decode(&mut value)14                    .map_err(|_| MiscError::$error)15            }16        }1718    };19}2021#[macro_export]22macro_rules! map_common_err_to_proxy {23    (match $err:ident { $($common_err:ident => $proxy_err:ident),+ }) => {24        $(25            if $err == <CommonError<T>>::$common_err.into() {26                return <Error<T>>::$proxy_err.into()27            } else28        )+ {29            $err30        }31    };32}3334pub enum MiscError {35    RmrkPropertyValueIsTooLong,36    CorruptedCollectionType,37}3839impl<T: Config> From<MiscError> for Error<T> {40    fn from(error: MiscError) -> Self {41        match error {42            MiscError::RmrkPropertyValueIsTooLong => Self::RmrkPropertyValueIsTooLong,43            MiscError::CorruptedCollectionType => Self::CorruptedCollectionType,44        }45    }46}4748pub trait IntoNftCollection<T: Config> {49    fn into_nft_collection(self) -> Result<NonfungibleHandle<T>, Error<T>>;50}5152impl<T: Config> IntoNftCollection<T> for CollectionHandle<T> {53    fn into_nft_collection(self) -> Result<NonfungibleHandle<T>, Error<T>> {54        match self.mode {55            CollectionMode::NFT => Ok(NonfungibleHandle::cast(self)),56            _ => Err(<Error<T>>::CollectionUnknown)57        }58    }59}6061pub trait IntoPropertyValue {62    fn into_property_value(self) -> Result<PropertyValue, MiscError>;63}6465impl<T: Encode> IntoPropertyValue for T {66    fn into_property_value(self) -> Result<PropertyValue, MiscError> {67        self.encode()68            .try_into()69            .map_err(|_| MiscError::RmrkPropertyValueIsTooLong)70    }71}7273pub trait RmrkNft {74    fn rmrk_nft_type(&self) -> Option<NftType>;75}7677impl<CrossAccountId> RmrkNft for ItemData<CrossAccountId> {78    fn rmrk_nft_type(&self) -> Option<NftType> {79        let mut value = self.const_data.as_slice();8081        NftType::decode(&mut value).ok()82    }83}8485pub trait RmrkDecode<T: Decode> {86    fn decode_property(&self) -> Option<T>;87}8889impl<T: Decode> RmrkDecode<T> for RmrkString {90    fn decode_property(&self) -> Option<T> { // todo access runtime errors? // but then rmrk_nft_type must have it too91        let mut value = self.as_slice();9293        T::decode(&mut value).ok()94    }95}9697#[derive(Encode, Decode, PartialEq, Eq)]98pub enum CollectionType {99    Regular,100    Resource,101    Base,102}103104#[derive(Encode, Decode, PartialEq, Eq)]105pub enum NftType {106    Regular,107    Resource,108    FixedPart,109    SlotPart,110    Theme111}112113impl_rmrk_value!(CollectionType, decode_error: CorruptedCollectionType);