1#![cfg_attr(not(feature = "std"), no_std)]
19#![warn(missing_docs)]
20#[cfg(all(
21 any(feature = "decode", feature = "serde_full"),
22 feature = "legacy",
23 not(feature = "std")
24))]
25compile_error!("decode and serde_full features prior to v14 require std");
26
27#[cfg(feature = "serde_full")]
28use serde::{Deserialize, Serialize};
29
30#[cfg(feature = "decode")]
31use codec::{Decode, Error, Input};
32
33cfg_if::cfg_if! {
34 if #[cfg(not(feature = "std"))] {
35 extern crate alloc;
36 use alloc::vec::Vec;
37 }
38}
39
40use codec::{Encode, Output};
41
42#[cfg(feature = "legacy")]
44pub mod decode_different;
45
46#[cfg(feature = "legacy")]
48pub mod v8;
49
50#[cfg(feature = "legacy")]
52pub mod v9;
53
54#[cfg(feature = "legacy")]
56pub mod v10;
57
58#[cfg(feature = "legacy")]
60pub mod v11;
61
62#[cfg(feature = "legacy")]
64pub mod v12;
65
66#[cfg(feature = "legacy")]
68pub mod v13;
69
70#[cfg(feature = "current")]
72pub mod v14;
73
74#[cfg(feature = "current")]
76pub mod v15;
77
78#[cfg(feature = "current")]
80pub mod v16;
81
82pub const META_RESERVED: u32 = 0x6174656d; #[derive(Eq, Encode, PartialEq, Debug)]
87#[cfg_attr(feature = "decode", derive(Decode))]
88#[cfg_attr(feature = "serde_full", derive(Serialize))]
89pub struct RuntimeMetadataPrefixed(pub u32, pub RuntimeMetadata);
90
91impl From<RuntimeMetadataPrefixed> for Vec<u8> {
92 fn from(value: RuntimeMetadataPrefixed) -> Self {
93 value.encode()
94 }
95}
96
97#[derive(Eq, Encode, PartialEq, Debug)]
101#[cfg_attr(feature = "decode", derive(Decode))]
102#[cfg_attr(feature = "serde_full", derive(Serialize))]
103pub enum RuntimeMetadata {
104 V0(RuntimeMetadataDeprecated),
106 V1(RuntimeMetadataDeprecated),
108 V2(RuntimeMetadataDeprecated),
110 V3(RuntimeMetadataDeprecated),
112 V4(RuntimeMetadataDeprecated),
114 V5(RuntimeMetadataDeprecated),
116 V6(RuntimeMetadataDeprecated),
118 V7(RuntimeMetadataDeprecated),
120 #[cfg(feature = "legacy")]
122 V8(v8::RuntimeMetadataV8),
123 #[cfg(not(feature = "legacy"))]
125 V8(OpaqueMetadata),
126 #[cfg(feature = "legacy")]
128 V9(v9::RuntimeMetadataV9),
129 #[cfg(not(feature = "legacy"))]
131 V9(OpaqueMetadata),
132 #[cfg(feature = "legacy")]
134 V10(v10::RuntimeMetadataV10),
135 #[cfg(not(feature = "legacy"))]
137 V10(OpaqueMetadata),
138 #[cfg(feature = "legacy")]
140 V11(v11::RuntimeMetadataV11),
141 #[cfg(not(feature = "legacy"))]
143 V11(OpaqueMetadata),
144 #[cfg(feature = "legacy")]
146 V12(v12::RuntimeMetadataV12),
147 #[cfg(not(feature = "legacy"))]
149 V12(OpaqueMetadata),
150 #[cfg(feature = "legacy")]
152 V13(v13::RuntimeMetadataV13),
153 #[cfg(not(feature = "legacy"))]
155 V13(OpaqueMetadata),
156 #[cfg(feature = "current")]
158 V14(v14::RuntimeMetadataV14),
159 #[cfg(not(feature = "current"))]
161 V14(OpaqueMetadata),
162 #[cfg(feature = "current")]
164 V15(v15::RuntimeMetadataV15),
165 #[cfg(not(feature = "current"))]
167 V15(OpaqueMetadata),
168 #[cfg(feature = "current")]
170 V16(v16::RuntimeMetadataV16),
171 #[cfg(not(feature = "current"))]
173 V16(OpaqueMetadata),
174}
175
176impl RuntimeMetadata {
177 pub fn version(&self) -> u32 {
179 match self {
180 RuntimeMetadata::V0(_) => 0,
181 RuntimeMetadata::V1(_) => 1,
182 RuntimeMetadata::V2(_) => 2,
183 RuntimeMetadata::V3(_) => 3,
184 RuntimeMetadata::V4(_) => 4,
185 RuntimeMetadata::V5(_) => 5,
186 RuntimeMetadata::V6(_) => 6,
187 RuntimeMetadata::V7(_) => 7,
188 RuntimeMetadata::V8(_) => 8,
189 RuntimeMetadata::V9(_) => 9,
190 RuntimeMetadata::V10(_) => 10,
191 RuntimeMetadata::V11(_) => 11,
192 RuntimeMetadata::V12(_) => 12,
193 RuntimeMetadata::V13(_) => 13,
194 RuntimeMetadata::V14(_) => 14,
195 RuntimeMetadata::V15(_) => 15,
196 RuntimeMetadata::V16(_) => 16,
197 }
198 }
199}
200
201#[derive(Encode, Eq, PartialEq, Debug)]
203#[cfg_attr(feature = "decode", derive(Decode))]
204#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
205pub struct OpaqueMetadata(pub Vec<u8>);
206
207#[derive(Eq, PartialEq, Debug)]
209#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
210pub enum RuntimeMetadataDeprecated {}
211
212impl Encode for RuntimeMetadataDeprecated {
213 fn encode_to<W: Output + ?Sized>(&self, _dest: &mut W) {}
214}
215
216impl codec::EncodeLike for RuntimeMetadataDeprecated {}
217
218#[cfg(feature = "decode")]
219impl Decode for RuntimeMetadataDeprecated {
220 fn decode<I: Input>(_input: &mut I) -> Result<Self, Error> {
221 Err("Decoding is not supported".into())
222 }
223}
224
225#[cfg(test)]
226mod test {
227 use super::*;
228 use std::fs;
229
230 fn load_metadata(version: u32) -> Vec<u8> {
231 fs::read(format!("./test_data/ksm_metadata_v{}.bin", version)).unwrap()
232 }
233
234 #[test]
235 fn should_decode_metadatav9() {
236 let meta: RuntimeMetadataPrefixed =
237 Decode::decode(&mut load_metadata(9).as_slice()).unwrap();
238 assert!(matches!(meta.1, RuntimeMetadata::V9(_)));
239 }
240
241 #[test]
242 fn should_decode_metadatav10() {
243 let meta: RuntimeMetadataPrefixed =
244 Decode::decode(&mut load_metadata(10).as_slice()).unwrap();
245 assert!(matches!(meta.1, RuntimeMetadata::V10(_)));
246 }
247
248 #[test]
249 fn should_decode_metadatav11() {
250 let meta: RuntimeMetadataPrefixed =
251 Decode::decode(&mut load_metadata(11).as_slice()).unwrap();
252 assert!(matches!(meta.1, RuntimeMetadata::V11(_)));
253 }
254
255 #[test]
256 fn should_decode_metadatav12() {
257 let meta: RuntimeMetadataPrefixed =
258 Decode::decode(&mut load_metadata(12).as_slice()).unwrap();
259 assert!(matches!(meta.1, RuntimeMetadata::V12(_)));
260 }
261
262 #[test]
263 fn should_decode_metadatav13() {
264 let meta: RuntimeMetadataPrefixed =
265 Decode::decode(&mut load_metadata(13).as_slice()).unwrap();
266 assert!(matches!(meta.1, RuntimeMetadata::V13(_)));
267 }
268
269 #[test]
270 fn should_decode_metadatav14() {
271 let meta: RuntimeMetadataPrefixed =
272 Decode::decode(&mut load_metadata(14).as_slice()).unwrap();
273 assert!(matches!(meta.1, RuntimeMetadata::V14(_)));
274 }
275}