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
//! Intermediate representation of the runtime metadata.
19

            
20
#![cfg_attr(not(feature = "std"), no_std)]
21
#![warn(missing_docs)]
22

            
23
extern crate alloc;
24

            
25
// Re-export.
26
#[doc(hidden)]
27
pub use frame_metadata;
28

            
29
mod types;
30
use frame_metadata::RuntimeMetadataPrefixed;
31
pub use types::*;
32

            
33
mod v14;
34
mod v15;
35

            
36
/// Metadata V14.
37
const V14: u32 = 14;
38

            
39
/// Metadata V15.
40
const V15: u32 = 15;
41

            
42
/// Transform the IR to the specified version.
43
///
44
/// Use [`supported_versions`] to find supported versions.
45
pub fn into_version(metadata: MetadataIR, version: u32) -> Option<RuntimeMetadataPrefixed> {
46
	// Note: Unstable metadata version is `u32::MAX` until stabilized.
47
	match version {
48
		// Latest stable version.
49
		V14 => Some(into_v14(metadata)),
50
		// Unstable metadata.
51
		V15 => Some(into_latest(metadata)),
52
		_ => None,
53
	}
54
}
55

            
56
/// Returns the supported metadata versions.
57
pub fn supported_versions() -> alloc::vec::Vec<u32> {
58
	alloc::vec![V14, V15]
59
}
60

            
61
/// Transform the IR to the latest stable metadata version.
62
pub fn into_latest(metadata: MetadataIR) -> RuntimeMetadataPrefixed {
63
	let latest: frame_metadata::v15::RuntimeMetadataV15 = metadata.into();
64
	latest.into()
65
}
66

            
67
/// Transform the IR to metadata version 14.
68
pub fn into_v14(metadata: MetadataIR) -> RuntimeMetadataPrefixed {
69
	let latest: frame_metadata::v14::RuntimeMetadataV14 = metadata.into();
70
	latest.into()
71
}
72

            
73
#[cfg(test)]
74
mod test {
75
	use super::*;
76
	use frame_metadata::{v14::META_RESERVED, RuntimeMetadata};
77
	use scale_info::meta_type;
78

            
79
	fn ir_metadata() -> MetadataIR {
80
		MetadataIR {
81
			pallets: vec![],
82
			extrinsic: ExtrinsicMetadataIR {
83
				ty: meta_type::<()>(),
84
				version: 0,
85
				address_ty: meta_type::<()>(),
86
				call_ty: meta_type::<()>(),
87
				signature_ty: meta_type::<()>(),
88
				extra_ty: meta_type::<()>(),
89
				signed_extensions: vec![],
90
			},
91
			ty: meta_type::<()>(),
92
			apis: vec![],
93
			outer_enums: OuterEnumsIR {
94
				call_enum_ty: meta_type::<()>(),
95
				event_enum_ty: meta_type::<()>(),
96
				error_enum_ty: meta_type::<()>(),
97
			},
98
		}
99
	}
100

            
101
	#[test]
102
	fn into_version_14() {
103
		let ir = ir_metadata();
104
		let metadata = into_version(ir, V14).expect("Should return prefixed metadata");
105

            
106
		assert_eq!(metadata.0, META_RESERVED);
107

            
108
		assert!(matches!(metadata.1, RuntimeMetadata::V14(_)));
109
	}
110

            
111
	#[test]
112
	fn into_version_15() {
113
		let ir = ir_metadata();
114
		let metadata = into_version(ir, V15).expect("Should return prefixed metadata");
115

            
116
		assert_eq!(metadata.0, META_RESERVED);
117

            
118
		assert!(matches!(metadata.1, RuntimeMetadata::V15(_)));
119
	}
120
}