frame_support_procedural/
dynamic_params.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//! Code for the `#[dynamic_params]`, `#[dynamic_pallet_params]` and
19//! `#[dynamic_aggregated_params_internal]` macros.
20
21use frame_support_procedural_tools::generate_access_from_frame_or_crate;
22use inflector::Inflector;
23use proc_macro2::{Span, TokenStream};
24use quote::{format_ident, quote, ToTokens};
25use syn::{parse2, spanned::Spanned, visit_mut, visit_mut::VisitMut, Result, Token};
26
27/// Parse and expand a `#[dynamic_params(..)]` module.
28pub fn dynamic_params(attr: TokenStream, item: TokenStream) -> Result<TokenStream> {
29	DynamicParamModAttr::parse(attr, item).map(ToTokens::into_token_stream)
30}
31
32/// Parse and expand `#[dynamic_pallet_params(..)]` attribute.
33pub fn dynamic_pallet_params(attr: TokenStream, item: TokenStream) -> Result<TokenStream> {
34	DynamicPalletParamAttr::parse(attr, item).map(ToTokens::into_token_stream)
35}
36
37/// Parse and expand `#[dynamic_aggregated_params_internal]` attribute.
38pub fn dynamic_aggregated_params_internal(
39	_attr: TokenStream,
40	item: TokenStream,
41) -> Result<TokenStream> {
42	parse2::<DynamicParamAggregatedEnum>(item).map(ToTokens::into_token_stream)
43}
44
45/// A top `#[dynamic_params(..)]` attribute together with a mod.
46#[derive(derive_syn_parse::Parse)]
47pub struct DynamicParamModAttr {
48	params_mod: syn::ItemMod,
49	meta: DynamicParamModAttrMeta,
50}
51
52/// The inner meta of a `#[dynamic_params(..)]` attribute.
53#[derive(derive_syn_parse::Parse)]
54pub struct DynamicParamModAttrMeta {
55	name: syn::Ident,
56	_comma: Option<Token![,]>,
57	#[parse_if(_comma.is_some())]
58	params_pallet: Option<syn::Type>,
59}
60
61impl DynamicParamModAttr {
62	pub fn parse(attr: TokenStream, item: TokenStream) -> Result<Self> {
63		let params_mod = parse2(item)?;
64		let meta = parse2(attr)?;
65		Ok(Self { params_mod, meta })
66	}
67
68	pub fn inner_mods(&self) -> Vec<syn::ItemMod> {
69		self.params_mod.content.as_ref().map_or(Vec::new(), |(_, items)| {
70			items
71				.iter()
72				.filter_map(|i| match i {
73					syn::Item::Mod(m) => Some(m),
74					_ => None,
75				})
76				.cloned()
77				.collect()
78		})
79	}
80}
81
82impl ToTokens for DynamicParamModAttr {
83	fn to_tokens(&self, tokens: &mut TokenStream) {
84		let scrate = match crate_access() {
85			Ok(path) => path,
86			Err(err) => return tokens.extend(err),
87		};
88		let (mut params_mod, name) = (self.params_mod.clone(), &self.meta.name);
89		let dynam_params_ident = &params_mod.ident;
90
91		let mut quoted_enum = quote! {};
92		for m in self.inner_mods() {
93			let aggregate_name =
94				syn::Ident::new(&m.ident.to_string().to_pascal_case(), m.ident.span());
95			let mod_name = &m.ident;
96
97			let mut attrs = m.attrs.clone();
98			attrs.retain(|attr| !attr.path().is_ident("dynamic_pallet_params"));
99			if let Err(err) = ensure_codec_index(&attrs, m.span()) {
100				tokens.extend(err.into_compile_error());
101				return
102			}
103
104			quoted_enum.extend(quote! {
105				#(#attrs)*
106				#aggregate_name(#dynam_params_ident::#mod_name::Parameters),
107			});
108		}
109
110		// Inject the outer args into the inner `#[dynamic_pallet_params(..)]` attribute.
111		if let Some(params_pallet) = &self.meta.params_pallet {
112			MacroInjectArgs { runtime_params: name.clone(), params_pallet: params_pallet.clone() }
113				.visit_item_mod_mut(&mut params_mod);
114		}
115
116		tokens.extend(quote! {
117			#params_mod
118
119			#[#scrate::dynamic_params::dynamic_aggregated_params_internal]
120			pub enum #name {
121				#quoted_enum
122			}
123		});
124	}
125}
126
127/// Ensure there is a `#[codec(index = ..)]` attribute.
128fn ensure_codec_index(attrs: &Vec<syn::Attribute>, span: Span) -> Result<()> {
129	let mut found = false;
130
131	for attr in attrs.iter() {
132		if attr.path().is_ident("codec") {
133			let meta: syn::ExprAssign = attr.parse_args()?;
134			if meta.left.to_token_stream().to_string() == "index" {
135				found = true;
136				break
137			}
138		}
139	}
140
141	if !found {
142		Err(syn::Error::new(span, "Missing explicit `#[codec(index = ..)]` attribute"))
143	} else {
144		Ok(())
145	}
146}
147
148/// Used to inject arguments into the inner `#[dynamic_pallet_params(..)]` attribute.
149///
150/// This allows the outer `#[dynamic_params(..)]` attribute to specify some arguments that don't
151/// need to be repeated every time.
152struct MacroInjectArgs {
153	runtime_params: syn::Ident,
154	params_pallet: syn::Type,
155}
156impl VisitMut for MacroInjectArgs {
157	fn visit_item_mod_mut(&mut self, item: &mut syn::ItemMod) {
158		// Check if the mod has a `#[dynamic_pallet_params(..)]` attribute.
159		let attr = item.attrs.iter_mut().find(|attr| attr.path().is_ident("dynamic_pallet_params"));
160
161		if let Some(attr) = attr {
162			match &attr.meta {
163				syn::Meta::Path(path) =>
164					assert_eq!(path.to_token_stream().to_string(), "dynamic_pallet_params"),
165				_ => (),
166			}
167
168			let runtime_params = &self.runtime_params;
169			let params_pallet = &self.params_pallet;
170
171			attr.meta = syn::parse2::<syn::Meta>(quote! {
172				dynamic_pallet_params(#runtime_params, #params_pallet)
173			})
174			.unwrap()
175			.into();
176		}
177
178		visit_mut::visit_item_mod_mut(self, item);
179	}
180}
181/// The helper attribute of a `#[dynamic_pallet_params(runtime_params, params_pallet)]`
182/// attribute.
183#[derive(derive_syn_parse::Parse)]
184pub struct DynamicPalletParamAttr {
185	inner_mod: syn::ItemMod,
186	meta: DynamicPalletParamAttrMeta,
187}
188
189/// The inner meta of a `#[dynamic_pallet_params(..)]` attribute.
190#[derive(derive_syn_parse::Parse)]
191pub struct DynamicPalletParamAttrMeta {
192	runtime_params: syn::Ident,
193	_comma: Token![,],
194	parameter_pallet: syn::Type,
195}
196
197impl DynamicPalletParamAttr {
198	pub fn parse(attr: TokenStream, item: TokenStream) -> Result<Self> {
199		Ok(Self { inner_mod: parse2(item)?, meta: parse2(attr)? })
200	}
201
202	pub fn statics(&self) -> Vec<syn::ItemStatic> {
203		self.inner_mod.content.as_ref().map_or(Vec::new(), |(_, items)| {
204			items
205				.iter()
206				.filter_map(|i| match i {
207					syn::Item::Static(s) => Some(s),
208					_ => None,
209				})
210				.cloned()
211				.collect()
212		})
213	}
214}
215
216impl ToTokens for DynamicPalletParamAttr {
217	fn to_tokens(&self, tokens: &mut TokenStream) {
218		let scrate = match crate_access() {
219			Ok(path) => path,
220			Err(err) => return tokens.extend(err),
221		};
222		let (params_mod, parameter_pallet, runtime_params) =
223			(&self.inner_mod, &self.meta.parameter_pallet, &self.meta.runtime_params);
224
225		let aggregate_name = syn::Ident::new(
226			&params_mod.ident.to_string().to_pascal_case(),
227			params_mod.ident.span(),
228		);
229		let (mod_name, vis) = (&params_mod.ident, &params_mod.vis);
230		let statics = self.statics();
231
232		let (mut key_names, mut key_values, mut defaults, mut attrs, mut value_types): (
233			Vec<_>,
234			Vec<_>,
235			Vec<_>,
236			Vec<_>,
237			Vec<_>,
238		) = Default::default();
239
240		for s in statics.iter() {
241			if let Err(err) = ensure_codec_index(&s.attrs, s.span()) {
242				tokens.extend(err.into_compile_error());
243				return
244			}
245
246			key_names.push(&s.ident);
247			key_values.push(format_ident!("{}Value", &s.ident));
248			defaults.push(&s.expr);
249			attrs.push(&s.attrs);
250			value_types.push(&s.ty);
251		}
252
253		let key_ident = syn::Ident::new("ParametersKey", params_mod.ident.span());
254		let value_ident = syn::Ident::new("ParametersValue", params_mod.ident.span());
255		let runtime_key_ident = format_ident!("{}Key", runtime_params);
256		let runtime_value_ident = format_ident!("{}Value", runtime_params);
257
258		tokens.extend(quote! {
259			pub mod #mod_name {
260				use super::*;
261
262				#[doc(hidden)]
263				#[derive(
264					Clone,
265					PartialEq,
266					Eq,
267					#scrate::__private::codec::Encode,
268					#scrate::__private::codec::Decode,
269					#scrate::__private::codec::MaxEncodedLen,
270					#scrate::__private::RuntimeDebug,
271					#scrate::__private::scale_info::TypeInfo
272				)]
273				#vis enum Parameters {
274					#(
275						#(#attrs)*
276						#key_names(#key_names, Option<#value_types>),
277					)*
278				}
279
280				#[doc(hidden)]
281				#[derive(
282					Clone,
283					PartialEq,
284					Eq,
285					#scrate::__private::codec::Encode,
286					#scrate::__private::codec::Decode,
287					#scrate::__private::codec::MaxEncodedLen,
288					#scrate::__private::RuntimeDebug,
289					#scrate::__private::scale_info::TypeInfo
290				)]
291				#vis enum #key_ident {
292					#(
293						#(#attrs)*
294						#key_names(#key_names),
295					)*
296				}
297
298				#[doc(hidden)]
299				#[derive(
300					Clone,
301					PartialEq,
302					Eq,
303					#scrate::__private::codec::Encode,
304					#scrate::__private::codec::Decode,
305					#scrate::__private::codec::MaxEncodedLen,
306					#scrate::__private::RuntimeDebug,
307					#scrate::__private::scale_info::TypeInfo
308				)]
309				#vis enum #value_ident {
310					#(
311						#(#attrs)*
312						#key_names(#value_types),
313					)*
314				}
315
316				impl #scrate::traits::dynamic_params::AggregatedKeyValue for Parameters {
317					type Key = #key_ident;
318					type Value = #value_ident;
319
320					fn into_parts(self) -> (Self::Key, Option<Self::Value>) {
321						match self {
322							#(
323								Parameters::#key_names(key, value) => {
324									(#key_ident::#key_names(key), value.map(#value_ident::#key_names))
325								},
326							)*
327						}
328					}
329				}
330
331				#(
332					#[doc(hidden)]
333					#[derive(
334						Clone,
335						PartialEq,
336						Eq,
337						#scrate::__private::codec::Encode,
338						#scrate::__private::codec::Decode,
339						#scrate::__private::codec::MaxEncodedLen,
340						#scrate::__private::RuntimeDebug,
341						#scrate::__private::scale_info::TypeInfo
342					)]
343					#vis struct #key_names;
344
345					impl #scrate::__private::Get<#value_types> for #key_names {
346						fn get() -> #value_types {
347							match
348								<#parameter_pallet as
349									#scrate::storage::StorageMap<#runtime_key_ident, #runtime_value_ident>
350								>::get(#runtime_key_ident::#aggregate_name(#key_ident::#key_names(#key_names)))
351							{
352								Some(#runtime_value_ident::#aggregate_name(
353									#value_ident::#key_names(inner))) => inner,
354								Some(_) => {
355									#scrate::defensive!("Unexpected value type at key - returning default");
356									#defaults
357								},
358								None => #defaults,
359							}
360						}
361					}
362
363					impl #scrate::traits::dynamic_params::Key for #key_names {
364						type Value = #value_types;
365						type WrappedValue = #key_values;
366					}
367
368					impl From<#key_names> for #key_ident {
369						fn from(key: #key_names) -> Self {
370							#key_ident::#key_names(key)
371						}
372					}
373
374					impl TryFrom<#key_ident> for #key_names {
375						type Error = ();
376
377						fn try_from(key: #key_ident) -> Result<Self, Self::Error> {
378							match key {
379								#key_ident::#key_names(key) => Ok(key),
380								_ => Err(()),
381							}
382						}
383					}
384
385					#[doc(hidden)]
386					#[derive(
387						Clone,
388						PartialEq,
389						Eq,
390						#scrate::sp_runtime::RuntimeDebug,
391					)]
392					#vis struct #key_values(pub #value_types);
393
394					impl From<#key_values> for #value_ident {
395						fn from(value: #key_values) -> Self {
396							#value_ident::#key_names(value.0)
397						}
398					}
399
400					impl From<(#key_names, #value_types)> for Parameters {
401						fn from((key, value): (#key_names, #value_types)) -> Self {
402							Parameters::#key_names(key, Some(value))
403						}
404					}
405
406					impl From<#key_names> for Parameters {
407						fn from(key: #key_names) -> Self {
408							Parameters::#key_names(key, None)
409						}
410					}
411
412					impl TryFrom<#value_ident> for #key_values {
413						type Error = ();
414
415						fn try_from(value: #value_ident) -> Result<Self, Self::Error> {
416							match value {
417								#value_ident::#key_names(value) => Ok(#key_values(value)),
418								_ => Err(()),
419							}
420						}
421					}
422
423					impl From<#key_values> for #value_types {
424						fn from(value: #key_values) -> Self {
425							value.0
426						}
427					}
428				)*
429			}
430		});
431	}
432}
433
434#[derive(derive_syn_parse::Parse)]
435pub struct DynamicParamAggregatedEnum {
436	aggregated_enum: syn::ItemEnum,
437}
438
439impl ToTokens for DynamicParamAggregatedEnum {
440	fn to_tokens(&self, tokens: &mut TokenStream) {
441		let scrate = match crate_access() {
442			Ok(path) => path,
443			Err(err) => return tokens.extend(err),
444		};
445		let params_enum = &self.aggregated_enum;
446		let (name, vis) = (&params_enum.ident, &params_enum.vis);
447
448		let (mut indices, mut param_names, mut param_types): (Vec<_>, Vec<_>, Vec<_>) =
449			Default::default();
450		let mut attributes = Vec::new();
451		for (i, variant) in params_enum.variants.iter().enumerate() {
452			indices.push(i);
453			param_names.push(&variant.ident);
454			attributes.push(&variant.attrs);
455
456			param_types.push(match &variant.fields {
457				syn::Fields::Unnamed(fields) if fields.unnamed.len() == 1 => &fields.unnamed[0].ty,
458				_ => {
459					*tokens = quote! { compile_error!("Only unnamed enum variants with one inner item are supported") };
460					return
461				},
462			});
463		}
464
465		let params_key_ident = format_ident!("{}Key", params_enum.ident);
466		let params_value_ident = format_ident!("{}Value", params_enum.ident);
467
468		tokens.extend(quote! {
469			#[doc(hidden)]
470			#[derive(
471				Clone,
472				PartialEq,
473				Eq,
474				#scrate::__private::codec::Encode,
475				#scrate::__private::codec::Decode,
476				#scrate::__private::codec::MaxEncodedLen,
477				#scrate::sp_runtime::RuntimeDebug,
478				#scrate::__private::scale_info::TypeInfo
479			)]
480			#vis enum #name {
481				#(
482					//#[codec(index = #indices)]
483					#(#attributes)*
484					#param_names(#param_types),
485				)*
486			}
487
488			#[doc(hidden)]
489			#[derive(
490				Clone,
491				PartialEq,
492				Eq,
493				#scrate::__private::codec::Encode,
494				#scrate::__private::codec::Decode,
495				#scrate::__private::codec::MaxEncodedLen,
496				#scrate::sp_runtime::RuntimeDebug,
497				#scrate::__private::scale_info::TypeInfo
498			)]
499			#vis enum #params_key_ident {
500				#(
501					#(#attributes)*
502					#param_names(<#param_types as #scrate::traits::dynamic_params::AggregatedKeyValue>::Key),
503				)*
504			}
505
506			#[doc(hidden)]
507			#[derive(
508				Clone,
509				PartialEq,
510				Eq,
511				#scrate::__private::codec::Encode,
512				#scrate::__private::codec::Decode,
513				#scrate::__private::codec::MaxEncodedLen,
514				#scrate::sp_runtime::RuntimeDebug,
515				#scrate::__private::scale_info::TypeInfo
516			)]
517			#vis enum #params_value_ident {
518				#(
519					#(#attributes)*
520					#param_names(<#param_types as #scrate::traits::dynamic_params::AggregatedKeyValue>::Value),
521				)*
522			}
523
524			impl #scrate::traits::dynamic_params::AggregatedKeyValue for #name {
525				type Key = #params_key_ident;
526				type Value = #params_value_ident;
527
528				fn into_parts(self) -> (Self::Key, Option<Self::Value>) {
529					match self {
530						#(
531							#name::#param_names(parameter) => {
532								let (key, value) = parameter.into_parts();
533								(#params_key_ident::#param_names(key), value.map(#params_value_ident::#param_names))
534							},
535						)*
536					}
537				}
538			}
539
540			#(
541				impl ::core::convert::From<<#param_types as #scrate::traits::dynamic_params::AggregatedKeyValue>::Key> for #params_key_ident {
542					fn from(key: <#param_types as #scrate::traits::dynamic_params::AggregatedKeyValue>::Key) -> Self {
543						#params_key_ident::#param_names(key)
544					}
545				}
546
547				impl ::core::convert::TryFrom<#params_value_ident> for <#param_types as #scrate::traits::dynamic_params::AggregatedKeyValue>::Value {
548					type Error = ();
549
550					fn try_from(value: #params_value_ident) -> Result<Self, Self::Error> {
551						match value {
552							#params_value_ident::#param_names(value) => Ok(value),
553							_ => Err(()),
554						}
555					}
556				}
557			)*
558		});
559	}
560}
561
562/// Get access to the current crate and convert the error to a compile error.
563fn crate_access() -> core::result::Result<syn::Path, TokenStream> {
564	generate_access_from_frame_or_crate("frame-support").map_err(|e| e.to_compile_error())
565}