referrerpolicy=no-referrer-when-downgrade

frame_support_procedural/construct_runtime/expand/
view_function.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 crate::construct_runtime::Pallet;
19use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
20
21/// Expands implementation of runtime level `DispatchViewFunction`.
22pub fn expand_outer_query(
23	runtime_name: &Ident,
24	pallet_decls: &[Pallet],
25	scrate: &TokenStream2,
26) -> TokenStream2 {
27	let runtime_view_function = syn::Ident::new("RuntimeViewFunction", Span::call_site());
28
29	let prefix_conditionals = pallet_decls.iter().map(|pallet| {
30		let pallet_name = &pallet.name;
31		let attr = pallet.get_attributes();
32		quote::quote! {
33			#attr
34			if id.prefix == <#pallet_name as #scrate::view_functions::ViewFunctionIdPrefix>::prefix() {
35				return <#pallet_name as #scrate::view_functions::DispatchViewFunction>::dispatch_view_function(id, input, output)
36			}
37		}
38	});
39
40	quote::quote! {
41		/// Runtime query type.
42		#[derive(
43			Clone, PartialEq, Eq,
44			#scrate::__private::codec::Encode,
45			#scrate::__private::codec::Decode,
46			#scrate::__private::codec::DecodeWithMemTracking,
47			#scrate::__private::scale_info::TypeInfo,
48			#scrate::__private::RuntimeDebug,
49		)]
50		pub enum #runtime_view_function {}
51
52		const _: () = {
53			impl #scrate::view_functions::DispatchViewFunction for #runtime_view_function {
54				fn dispatch_view_function<O: #scrate::__private::codec::Output>(
55					id: & #scrate::view_functions::ViewFunctionId,
56					input: &mut &[u8],
57					output: &mut O
58				) -> Result<(), #scrate::view_functions::ViewFunctionDispatchError>
59				{
60					#( #prefix_conditionals )*
61					Err(#scrate::view_functions::ViewFunctionDispatchError::NotFound(id.clone()))
62				}
63			}
64
65			impl #runtime_name {
66				/// Convenience function for view functions dispatching and execution from the runtime API.
67				pub fn execute_view_function(
68					id: #scrate::view_functions::ViewFunctionId,
69					input: #scrate::__private::Vec<::core::primitive::u8>,
70				) -> Result<#scrate::__private::Vec<::core::primitive::u8>, #scrate::view_functions::ViewFunctionDispatchError>
71				{
72					let mut output = #scrate::__private::vec![];
73					<#runtime_view_function as #scrate::view_functions::DispatchViewFunction>::dispatch_view_function(&id, &mut &input[..], &mut output)?;
74					Ok(output)
75				}
76			}
77		};
78	}
79}