git.delta.rocks / unique-network / refs/commits / 51e15a07c4a1

difftreelog

source

pallets/proxy-rmrk-core/src/property.rs3.2 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	Parts,38	Base,39	Src,40	EquippedNft,41	BaseType,42	ExternalPartId,43	EquippableList,44	ZIndex,45	ThemeName,46	ThemeInherit,47	UserProperty(&'r [u8]),48}4950impl<'r> RmrkProperty<'r> {51	pub fn to_key<T: Config>(self) -> Result<PropertyKey, Error<T>> {52		fn get_bytes<T: AsRef<[u8]>>(container: &T) -> &[u8] {53			container.as_ref()54		}5556		macro_rules! key {57            ($($component:expr),+) => {58                PropertyKey::try_from([$(key!(@ &$component)),+].concat())59                    .map_err(|_| <Error<T>>::RmrkPropertyKeyIsTooLong)60            };6162            (@ $key:expr) => {63                get_bytes($key)64            };65        }6667		match self {68			Self::Metadata => key!("metadata"),69			Self::CollectionType => key!("collection-type"),70			Self::RmrkInternalCollectionId => key!("internal-id"),71			Self::TokenType => key!("token-type"),72			Self::Transferable => key!("transferable"),73			Self::RoyaltyInfo => key!("royalty-info"),74			Self::Equipped => key!("equipped"),75			Self::ResourcePriorities => key!("resource-priorities"),76			Self::NextResourceId => key!("next-resource-id"),77			Self::ResourceId(id) => key!(RESOURCE_ID_PREFIX, id.to_le_bytes()),78			Self::PendingNftAccept => key!("pending-nft-accept"),79			Self::PendingChildren => key!("pending-children"),80			Self::Parts => key!("parts"),81			Self::Base => key!("base"),82			Self::Src => key!("src"),83			Self::EquippedNft => key!("equipped-nft"),84			Self::BaseType => key!("base-type"),85			Self::ExternalPartId => key!("ext-part-id"),86			Self::EquippableList => key!("equippable-list"),87			Self::ZIndex => key!("z-index"),88			Self::ThemeName => key!("theme-name"),89			Self::ThemeInherit => key!("theme-inherit"),90			Self::UserProperty(name) => key!(USER_PROPERTY_PREFIX, name),91		}92	}93}9495pub fn strip_key_prefix(key: &PropertyKey, prefix: &str) -> Option<PropertyKey> {96	let key_prefix = PropertyKey::try_from(prefix.as_bytes().to_vec()).ok()?;97	let key_prefix = PropertyScope::Rmrk.apply(key_prefix).ok()?;9899	key.as_slice().strip_prefix(key_prefix.as_slice())?100		.to_vec().try_into().ok()101}102103pub fn is_valid_key_prefix(key: &PropertyKey, prefix: &str) -> bool {104	strip_key_prefix(key, prefix).is_some()105}