referrerpolicy=no-referrer-when-downgrade

frame_support_procedural/
key_prefix.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
18use frame_support_procedural_tools::generate_access_from_frame_or_crate;
19use proc_macro2::{Span, TokenStream};
20use quote::{format_ident, quote, ToTokens};
21use syn::{Ident, Result};
22
23const MAX_IDENTS: usize = 18;
24
25pub fn impl_key_prefix_for_tuples(input: proc_macro::TokenStream) -> Result<TokenStream> {
26	if !input.is_empty() {
27		return Err(syn::Error::new(Span::call_site(), "No arguments expected"))
28	}
29
30	let mut all_trait_impls = TokenStream::new();
31	let frame_support = generate_access_from_frame_or_crate("frame-support")?;
32
33	for i in 2..=MAX_IDENTS {
34		let current_tuple = (0..i)
35			.map(|n| Ident::new(&format!("Tuple{}", n), Span::call_site()))
36			.collect::<Vec<_>>();
37
38		for prefix_count in 1..i {
39			let (prefixes, suffixes) = current_tuple.split_at(prefix_count);
40
41			let hashers = current_tuple
42				.iter()
43				.map(|ident| format_ident!("Hasher{}", ident))
44				.collect::<Vec<_>>();
45			let kargs =
46				prefixes.iter().map(|ident| format_ident!("KArg{}", ident)).collect::<Vec<_>>();
47			let partial_keygen = generate_keygen(prefixes);
48			let suffix_keygen = generate_keygen(suffixes);
49			let suffix_tuple = generate_tuple(suffixes);
50
51			let trait_impls = quote! {
52				impl<
53					#(#current_tuple: FullCodec + StaticTypeInfo,)*
54					#(#hashers: StorageHasher,)*
55					#(#kargs: EncodeLike<#prefixes>),*
56				> HasKeyPrefix<( #( #kargs, )* )> for ( #( Key<#hashers, #current_tuple>, )* ) {
57					type Suffix = #suffix_tuple;
58
59					fn partial_key(prefix: ( #( #kargs, )* )) -> Vec<u8> {
60						<#partial_keygen>::final_key(prefix)
61					}
62				}
63
64				impl<
65					#(#current_tuple: FullCodec + StaticTypeInfo,)*
66					#(#hashers: ReversibleStorageHasher,)*
67					#(#kargs: EncodeLike<#prefixes>),*
68				> HasReversibleKeyPrefix<( #( #kargs, )* )> for ( #( Key<#hashers, #current_tuple>, )* ) {
69					fn decode_partial_key(key_material: &[u8]) -> Result<
70						Self::Suffix,
71						#frame_support::__private::codec::Error,
72					> {
73						<#suffix_keygen>::decode_final_key(key_material).map(|k| k.0)
74					}
75				}
76			};
77
78			all_trait_impls.extend(trait_impls);
79		}
80	}
81
82	Ok(all_trait_impls)
83}
84
85fn generate_tuple(idents: &[Ident]) -> TokenStream {
86	if idents.len() == 1 {
87		idents[0].to_token_stream()
88	} else {
89		quote!((#(#idents),*))
90	}
91}
92
93fn generate_keygen(idents: &[Ident]) -> TokenStream {
94	if idents.len() == 1 {
95		let key = &idents[0];
96		let hasher = format_ident!("Hasher{}", key);
97
98		quote!(Key<#hasher, #key>)
99	} else {
100		let hashers = idents.iter().map(|ident| format_ident!("Hasher{}", ident));
101
102		quote!((#(Key<#hashers, #idents>),*))
103	}
104}