chainql_frame_metadata/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) 2018-2020 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//! Decodable variant of the RuntimeMetadata.
19
20#![cfg_attr(not(feature = "std"), no_std)]
21#![warn(missing_docs)]
22#[cfg(all(
23	any(feature = "decode", feature = "serde_full"),
24	any(
25		feature = "v13",
26		feature = "v12",
27		feature = "v11",
28		feature = "v10",
29		feature = "v9",
30		feature = "v8",
31		feature = "legacy"
32	),
33	not(feature = "std")
34))]
35compile_error!("decode and serde_full features prior to v14 require std");
36
37#[cfg(feature = "serde_full")]
38use serde::{Deserialize, Serialize};
39
40#[cfg(feature = "decode")]
41use codec::{Decode, Error, Input};
42
43cfg_if::cfg_if! {
44	if #[cfg(not(feature = "std"))] {
45		extern crate alloc;
46		use alloc::vec::Vec;
47	}
48}
49
50use codec::{Encode, Output};
51
52/// A type that decodes to a different type than it encodes.
53#[cfg(any(
54	feature = "v13",
55	feature = "v12",
56	feature = "v11",
57	feature = "v10",
58	feature = "v9",
59	feature = "v8",
60	feature = "legacy"
61))]
62pub mod decode_different;
63
64/// Metadata v8
65#[cfg(feature = "v8")]
66pub mod v8;
67
68/// Metadata v9
69#[cfg(feature = "v9")]
70pub mod v9;
71
72/// Metadata v10
73#[cfg(feature = "v10")]
74pub mod v10;
75
76/// Metadata v11
77#[cfg(feature = "v11")]
78pub mod v11;
79
80/// Metadata v12
81#[cfg(feature = "v12")]
82pub mod v12;
83
84/// Metadata v13
85#[cfg(feature = "v13")]
86pub mod v13;
87
88/// Metadata v14
89#[cfg(feature = "v14")]
90pub mod v14;
91
92// Reexport all the types from the latest version.
93//
94// When a new version becomes available, update this.
95#[cfg(feature = "v14")]
96pub use self::v14::*;
97
98/// Metadata prefixed by a u32 for reserved usage
99#[derive(Eq, Encode, PartialEq, Debug)]
100#[cfg_attr(feature = "decode", derive(Decode))]
101#[cfg_attr(feature = "serde_full", derive(Serialize))]
102pub struct RuntimeMetadataPrefixed(pub u32, pub RuntimeMetadata);
103
104/// The metadata of a runtime.
105/// The version ID encoded/decoded through
106/// the enum nature of `RuntimeMetadata`.
107#[derive(Eq, Encode, PartialEq, Debug)]
108#[cfg_attr(feature = "decode", derive(Decode))]
109#[cfg_attr(feature = "serde_full", derive(Serialize))]
110pub enum RuntimeMetadata {
111	/// Unused; enum filler.
112	V0(RuntimeMetadataDeprecated),
113	/// Version 1 for runtime metadata. No longer used.
114	V1(RuntimeMetadataDeprecated),
115	/// Version 2 for runtime metadata. No longer used.
116	V2(RuntimeMetadataDeprecated),
117	/// Version 3 for runtime metadata. No longer used.
118	V3(RuntimeMetadataDeprecated),
119	/// Version 4 for runtime metadata. No longer used.
120	V4(RuntimeMetadataDeprecated),
121	/// Version 5 for runtime metadata. No longer used.
122	V5(RuntimeMetadataDeprecated),
123	/// Version 6 for runtime metadata. No longer used.
124	V6(RuntimeMetadataDeprecated),
125	/// Version 7 for runtime metadata. No longer used.
126	V7(RuntimeMetadataDeprecated),
127	/// Version 8 for runtime metadata.
128	#[cfg(any(feature = "v8", feature = "legacy"))]
129	V8(v8::RuntimeMetadataV8),
130	/// Version 8 for runtime metadata, as raw encoded bytes.
131	#[cfg(not(feature = "v8"))]
132	V8(OpaqueMetadata),
133	/// Version 9 for runtime metadata.
134	#[cfg(any(feature = "v9", feature = "legacy"))]
135	V9(v9::RuntimeMetadataV9),
136	/// Version 9 for runtime metadata, as raw encoded bytes.
137	#[cfg(not(feature = "v9"))]
138	V9(OpaqueMetadata),
139	/// Version 10 for runtime metadata.
140	#[cfg(any(feature = "v10", feature = "legacy"))]
141	V10(v10::RuntimeMetadataV10),
142	/// Version 10 for runtime metadata, as raw encoded bytes.
143	#[cfg(not(feature = "v10"))]
144	V10(OpaqueMetadata),
145	/// Version 11 for runtime metadata.
146	#[cfg(any(feature = "v11", feature = "legacy"))]
147	V11(v11::RuntimeMetadataV11),
148	/// Version 11 for runtime metadata, as raw encoded bytes.
149	#[cfg(not(feature = "v11"))]
150	V11(OpaqueMetadata),
151	/// Version 12 for runtime metadata
152	#[cfg(any(feature = "v12", feature = "legacy"))]
153	V12(v12::RuntimeMetadataV12),
154	/// Version 12 for runtime metadata, as raw encoded bytes.
155	#[cfg(not(feature = "v12"))]
156	V12(OpaqueMetadata),
157	/// Version 13 for runtime metadata.
158	#[cfg(any(feature = "v13", feature = "legacy"))]
159	V13(v13::RuntimeMetadataV13),
160	/// Version 13 for runtime metadata, as raw encoded bytes.
161	#[cfg(not(feature = "v13"))]
162	V13(OpaqueMetadata),
163	/// Version 14 for runtime metadata.
164	#[cfg(feature = "v14")]
165	V14(v14::RuntimeMetadataV14),
166	/// Version 14 for runtime metadata, as raw encoded bytes.
167	#[cfg(not(feature = "v14"))]
168	V14(OpaqueMetadata),
169}
170
171impl RuntimeMetadata {
172	/// Get the version number of the metadata.
173	pub fn version(&self) -> u32 {
174		match self {
175			RuntimeMetadata::V0(_) => 0,
176			RuntimeMetadata::V1(_) => 1,
177			RuntimeMetadata::V2(_) => 2,
178			RuntimeMetadata::V3(_) => 3,
179			RuntimeMetadata::V4(_) => 4,
180			RuntimeMetadata::V5(_) => 5,
181			RuntimeMetadata::V6(_) => 6,
182			RuntimeMetadata::V7(_) => 7,
183			RuntimeMetadata::V8(_) => 8,
184			RuntimeMetadata::V9(_) => 9,
185			RuntimeMetadata::V10(_) => 10,
186			RuntimeMetadata::V11(_) => 11,
187			RuntimeMetadata::V12(_) => 12,
188			RuntimeMetadata::V13(_) => 13,
189			RuntimeMetadata::V14(_) => 14,
190		}
191	}
192}
193
194/// Stores the encoded `RuntimeMetadata` as raw bytes.
195#[derive(Encode, Eq, PartialEq, Debug)]
196#[cfg_attr(feature = "decode", derive(Decode))]
197#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
198pub struct OpaqueMetadata(pub Vec<u8>);
199
200/// Enum that should fail.
201#[derive(Eq, PartialEq, Debug)]
202#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
203pub enum RuntimeMetadataDeprecated {}
204
205impl Encode for RuntimeMetadataDeprecated {
206	fn encode_to<W: Output + ?Sized>(&self, _dest: &mut W) {}
207}
208
209impl codec::EncodeLike for RuntimeMetadataDeprecated {}
210
211#[cfg(feature = "decode")]
212impl Decode for RuntimeMetadataDeprecated {
213	fn decode<I: Input>(_input: &mut I) -> Result<Self, Error> {
214		Err("Decoding is not supported".into())
215	}
216}