difftreelog
fix rmrk_property macro
in: master
2 files changed
pallets/rmrk-proxy/src/misc.rsdiffbeforeafterboth1use 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);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);primitives/data-structs/src/lib.rsdiffbeforeafterboth--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -678,6 +678,7 @@
}
}
+#[derive(Debug)]
pub enum PropertiesError {
NoSpaceForProperty,
PropertyLimitReached,
@@ -693,7 +694,7 @@
}
impl PropertyScope {
- fn apply(self, key: PropertyKey) -> Result<PropertyKey, PropertiesError> {
+ pub fn apply(self, key: PropertyKey) -> Result<PropertyKey, PropertiesError> {
let scope_str: &[u8] = match self {
Self::None => return Ok(key),
Self::Rmrk => b"rmrk",