git.delta.rocks / unique-network / refs/commits / 7563962a61ba

difftreelog

source

pallets/proxy-rmrk-core/src/property.rs3.3 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use super::*;18use up_data_structs::PropertyScope;19use core::convert::AsRef;2021pub const RESOURCE_ID_PREFIX: &str = "rsid-";22pub const USER_PROPERTY_PREFIX: &str = "userprop-";2324pub enum RmrkProperty<'r> {25	Metadata,26	CollectionType,27	RmrkInternalCollectionId,28	TokenType,29	Transferable,30	RoyaltyInfo,31	Equipped,32	ResourcePriorities,33	NextResourceId,34	ResourceId(RmrkResourceId),35	PendingNftAccept,36	PendingChildren,37	AssociatedBases,38	Parts,39	Base,40	Src,41	EquippedNft,42	BaseType,43	ExternalPartId,44	EquippableList,45	ZIndex,46	ThemeName,47	ThemeInherit,48	UserProperty(&'r [u8]),49}5051impl<'r> RmrkProperty<'r> {52	pub fn to_key<T: Config>(self) -> Result<PropertyKey, Error<T>> {53		fn get_bytes<T: AsRef<[u8]>>(container: &T) -> &[u8] {54			container.as_ref()55		}5657		macro_rules! key {58            ($($component:expr),+) => {59                PropertyKey::try_from([$(key!(@ &$component)),+].concat())60                    .map_err(|_| <Error<T>>::RmrkPropertyKeyIsTooLong)61            };6263            (@ $key:expr) => {64                get_bytes($key)65            };66        }6768		match self {69			Self::Metadata => key!("metadata"),70			Self::CollectionType => key!("collection-type"),71			Self::RmrkInternalCollectionId => key!("internal-id"),72			Self::TokenType => key!("token-type"),73			Self::Transferable => key!("transferable"),74			Self::RoyaltyInfo => key!("royalty-info"),75			Self::Equipped => key!("equipped"),76			Self::ResourcePriorities => key!("resource-priorities"),77			Self::NextResourceId => key!("next-resource-id"),78			Self::ResourceId(id) => key!(RESOURCE_ID_PREFIX, id.to_le_bytes()),79			Self::PendingNftAccept => key!("pending-nft-accept"),80			Self::PendingChildren => key!("pending-children"),81			Self::AssociatedBases => key!("assoc-bases"),82			Self::Parts => key!("parts"),83			Self::Base => key!("base"),84			Self::Src => key!("src"),85			Self::EquippedNft => key!("equipped-nft"),86			Self::BaseType => key!("base-type"),87			Self::ExternalPartId => key!("ext-part-id"),88			Self::EquippableList => key!("equippable-list"),89			Self::ZIndex => key!("z-index"),90			Self::ThemeName => key!("theme-name"),91			Self::ThemeInherit => key!("theme-inherit"),92			Self::UserProperty(name) => key!(USER_PROPERTY_PREFIX, name),93		}94	}95}9697pub fn strip_key_prefix(key: &PropertyKey, prefix: &str) -> Option<PropertyKey> {98	let key_prefix = PropertyKey::try_from(prefix.as_bytes().to_vec()).ok()?;99	let key_prefix = PropertyScope::Rmrk.apply(key_prefix).ok()?;100101	key.as_slice()102		.strip_prefix(key_prefix.as_slice())?103		.to_vec()104		.try_into()105		.ok()106}107108pub fn is_valid_key_prefix(key: &PropertyKey, prefix: &str) -> bool {109	strip_key_prefix(key, prefix).is_some()110}