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

difftreelog

source

pallets/proxy-rmrk-core/src/misc.rs2.1 KiBsourcehistory
1use super::*;2use codec::{Encode, Decode};3use pallet_nonfungible::NonfungibleHandle;45macro_rules! impl_rmrk_value {6    ($enum_name:path, decode_error: $error:ident) => {7        impl IntoPropertyValue for $enum_name {8            fn into_property_value(self) -> Result<PropertyValue, MiscError> {9                self.encode()10                    .try_into()11                    .map_err(|_| MiscError::RmrkPropertyValueIsTooLong)12            }13        }1415        impl TryFrom<&PropertyValue> for $enum_name {16            type Error = MiscError;1718            fn try_from(value: &PropertyValue) -> Result<Self, Self::Error> {19                let mut value = value.as_slice();2021                <$enum_name>::decode(&mut value)22                    .map_err(|_| MiscError::$error)23            }24        }2526    };27}2829pub enum MiscError {30    RmrkPropertyValueIsTooLong,31    CorruptedCollectionType,32}3334impl<T: Config> From<MiscError> for Error<T> {35    fn from(error: MiscError) -> Self {36        match error {37            MiscError::RmrkPropertyValueIsTooLong => Self::RmrkPropertyValueIsTooLong,38            MiscError::CorruptedCollectionType => Self::CorruptedCollectionType,39        }40    }41}4243pub trait IntoNftCollection<T: Config> {44    fn into_nft_collection(self) -> Result<NonfungibleHandle<T>, Error<T>>;45}4647impl<T: Config> IntoNftCollection<T> for CollectionHandle<T> {48    fn into_nft_collection(self) -> Result<NonfungibleHandle<T>, Error<T>> {49        match self.mode {50            CollectionMode::NFT => Ok(NonfungibleHandle::cast(self)),51            _ => Err(<Error<T>>::CollectionUnknown)52        }53    }54}5556pub trait IntoPropertyValue {57    fn into_property_value(self) -> Result<PropertyValue, MiscError>;58}5960impl<L: Get<u32>> IntoPropertyValue for BoundedVec<u8, L> {61    fn into_property_value(self) -> Result<PropertyValue, MiscError> {62        self.into_inner()63            .try_into()64            .map_err(|_| MiscError::RmrkPropertyValueIsTooLong)65    }66}6768#[derive(Encode, Decode, PartialEq, Eq)]69pub enum CollectionType {70    Regular,71    Resource,72    Base,73}7475impl_rmrk_value!(CollectionType, decode_error: CorruptedCollectionType);