referrerpolicy=no-referrer-when-downgrade

frame_support_procedural/pallet/parse/
view_functions.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governsing permissions and
// limitations under the License.

use frame_support_procedural_tools::get_doc_literals;
use inflector::Inflector;
use syn::spanned::Spanned;

/// Parsed representation of an impl block annotated with `pallet::view_functions_experimental`.
pub struct ViewFunctionsImplDef {
	/// The where_clause used.
	pub where_clause: Option<syn::WhereClause>,
	/// The span of the pallet::view_functions_experimental attribute.
	pub attr_span: proc_macro2::Span,
	/// The view function definitions.
	pub view_functions: Vec<ViewFunctionDef>,
}

impl ViewFunctionsImplDef {
	pub fn try_from(attr_span: proc_macro2::Span, item: &mut syn::Item) -> syn::Result<Self> {
		let syn::Item::Impl(item_impl) = item else {
			return Err(syn::Error::new(
				item.span(),
				"Invalid pallet::view_functions_experimental, expected item impl",
			))
		};
		let mut view_functions = Vec::new();
		for item in &mut item_impl.items {
			if let syn::ImplItem::Fn(method) = item {
				if !matches!(method.vis, syn::Visibility::Public(_)) {
					let msg = "Invalid pallet::view_functions_experimental, view function must be public: \
						`pub fn`";

					let span = match method.vis {
						syn::Visibility::Inherited => method.sig.span(),
						_ => method.vis.span(),
					};

					return Err(syn::Error::new(span, msg))
				}

				let view_fn_def = ViewFunctionDef::try_from(method.clone())?;
				view_functions.push(view_fn_def)
			} else {
				return Err(syn::Error::new(
					item.span(),
					"Invalid pallet::view_functions_experimental, expected a function",
				))
			}
		}
		Ok(Self {
			view_functions,
			attr_span,
			where_clause: item_impl.generics.where_clause.clone(),
		})
	}
}

/// Parsed representation of a view function definition.
#[derive(Clone)]
pub struct ViewFunctionDef {
	pub name: syn::Ident,
	pub docs: Vec<syn::Expr>,
	pub args: Vec<syn::FnArg>,
	pub return_type: syn::Type,
}

impl TryFrom<syn::ImplItemFn> for ViewFunctionDef {
	type Error = syn::Error;
	fn try_from(method: syn::ImplItemFn) -> Result<Self, Self::Error> {
		let syn::ReturnType::Type(_, type_) = method.sig.output else {
			return Err(syn::Error::new(method.sig.span(), "view functions must return a value"))
		};

		Ok(Self {
			name: method.sig.ident.clone(),
			docs: get_doc_literals(&method.attrs),
			args: method.sig.inputs.iter().cloned().collect::<Vec<_>>(),
			return_type: *type_.clone(),
		})
	}
}

impl ViewFunctionDef {
	pub fn view_function_struct_ident(&self) -> syn::Ident {
		syn::Ident::new(
			&format!("{}ViewFunction", self.name.to_string().to_pascal_case()),
			self.name.span(),
		)
	}

	pub fn view_function_id_suffix_bytes(&self) -> Result<[u8; 16], syn::Error> {
		let mut output = [0u8; 16];

		// concatenate the signature string
		let arg_types = self
			.args_names_types()?
			.1
			.iter()
			.map(|ty| quote::quote!(#ty).to_string().replace(" ", ""))
			.collect::<Vec<_>>()
			.join(",");
		let return_type = &self.return_type;
		let return_type = quote::quote!(#return_type).to_string().replace(" ", "");
		let view_fn_signature = format!(
			"{view_function_name}({arg_types}) -> {return_type}",
			view_function_name = &self.name,
		);

		// hash the signature string
		let hash = sp_crypto_hashing::twox_128(view_fn_signature.as_bytes());
		output.copy_from_slice(&hash[..]);
		Ok(output)
	}

	pub fn args_names_types(&self) -> Result<(Vec<syn::Ident>, Vec<syn::Type>), syn::Error> {
		Ok(self
			.args
			.iter()
			.map(|arg| {
				let syn::FnArg::Typed(pat_type) = arg else {
					return Err(syn::Error::new(
						arg.span(),
						"Unsupported argument in view function",
					));
				};
				let syn::Pat::Ident(ident) = &*pat_type.pat else {
					return Err(syn::Error::new(
						pat_type.pat.span(),
						"Unsupported pattern in view function argument",
					));
				};
				Ok((ident.ident.clone(), *pat_type.ty.clone()))
			})
			.collect::<Result<Vec<(syn::Ident, syn::Type)>, syn::Error>>()?
			.into_iter()
			.unzip())
	}
}