referrerpolicy=no-referrer-when-downgrade

pallet_nfts/
macros.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18/// Implements encoding and decoding traits for a wrapper type that represents
19/// bitflags. The wrapper type should contain a field of type `$size`, where
20/// `$size` is an integer type (e.g., u8, u16, u32) that can represent the bitflags.
21/// The `$bitflag_enum` type is the enumeration type that defines the individual bitflags.
22///
23/// This macro provides implementations for the following traits:
24/// - `MaxEncodedLen`: Calculates the maximum encoded length for the wrapper type.
25/// - `Encode`: Encodes the wrapper type using the provided encoding function.
26/// - `EncodeLike`: Trait indicating the type can be encoded as is.
27/// - `Decode`: Decodes the wrapper type from the input.
28/// - `TypeInfo`: Provides type information for the wrapper type.
29macro_rules! impl_codec_bitflags {
30	($wrapper:ty, $size:ty, $bitflag_enum:ty) => {
31		impl MaxEncodedLen for $wrapper {
32			fn max_encoded_len() -> usize {
33				<$size>::max_encoded_len()
34			}
35		}
36		impl Encode for $wrapper {
37			fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
38				self.0.bits().using_encoded(f)
39			}
40		}
41		impl EncodeLike for $wrapper {}
42		impl Decode for $wrapper {
43			fn decode<I: codec::Input>(
44				input: &mut I,
45			) -> ::core::result::Result<Self, codec::Error> {
46				let field = <$size>::decode(input)?;
47				Ok(Self(BitFlags::from_bits(field as $size).map_err(|_| "invalid value")?))
48			}
49		}
50
51		impl TypeInfo for $wrapper {
52			type Identity = Self;
53
54			fn type_info() -> Type {
55				Type::builder()
56					.path(Path::new("BitFlags", module_path!()))
57					.type_params(vec![TypeParameter::new("T", Some(meta_type::<$bitflag_enum>()))])
58					.composite(
59						Fields::unnamed()
60							.field(|f| f.ty::<$size>().type_name(stringify!($bitflag_enum))),
61					)
62			}
63		}
64	};
65}
66pub(crate) use impl_codec_bitflags;