subxt_metadata/from/
mod.rs

1// Copyright 2019-2025 Parity Technologies (UK) Ltd.
2// This file is dual-licensed as Apache-2.0 or GPL-3.0.
3// see LICENSE for license details.
4
5use alloc::string::String;
6use thiserror::Error as DeriveError;
7mod v14;
8mod v15;
9mod v16;
10
11/// Legacy translation hidden behind the corresponding feature flag.
12#[cfg(feature = "legacy")]
13pub mod legacy;
14
15/// The metadata versions that we support converting into [`crate::Metadata`].
16/// These are ordest from highest to lowest, so that the metadata we'd want to
17/// pick first is first in the array.
18pub const SUPPORTED_METADATA_VERSIONS: [u32; 3] = [16, 15, 14];
19
20/// An error emitted if something goes wrong converting [`frame_metadata`]
21/// types into [`crate::Metadata`].
22#[derive(Debug, PartialEq, Eq, DeriveError)]
23#[non_exhaustive]
24pub enum TryFromError {
25    /// Type missing from type registry
26    #[error("Type id {0} is expected but not found in the type registry")]
27    TypeNotFound(u32),
28    /// Type was not a variant/enum type
29    #[error("Type {0} was not a variant/enum type, but is expected to be one")]
30    VariantExpected(u32),
31    /// An unsupported metadata version was provided.
32    #[error("Cannot convert v{0} metadata into Metadata type")]
33    UnsupportedMetadataVersion(u32),
34    /// Type name missing from type registry
35    #[error("Type name {0} is expected but not found in the type registry")]
36    TypeNameNotFound(String),
37    /// Invalid type path.
38    #[error("Type has an invalid path {0}")]
39    InvalidTypePath(String),
40    /// Cannot decode storage entry information.
41    #[error("Error decoding storage entry information: {0}")]
42    StorageInfoError(#[from] frame_decode::storage::StorageInfoError<'static>),
43    /// Cannot decode Runtime API information.
44    #[error("Error decoding Runtime API information: {0}")]
45    RuntimeInfoError(#[from] frame_decode::runtime_apis::RuntimeApiInfoError<'static>),
46    /// Cannot decode View Function information.
47    #[error("Error decoding View Function information: {0}")]
48    ViewFunctionInfoError(#[from] frame_decode::view_functions::ViewFunctionInfoError<'static>),
49}
50
51impl TryFrom<frame_metadata::RuntimeMetadataPrefixed> for crate::Metadata {
52    type Error = TryFromError;
53
54    fn try_from(value: frame_metadata::RuntimeMetadataPrefixed) -> Result<Self, Self::Error> {
55        match value.1 {
56            frame_metadata::RuntimeMetadata::V0(_) => {
57                Err(TryFromError::UnsupportedMetadataVersion(0))
58            }
59            frame_metadata::RuntimeMetadata::V1(_) => {
60                Err(TryFromError::UnsupportedMetadataVersion(1))
61            }
62            frame_metadata::RuntimeMetadata::V2(_) => {
63                Err(TryFromError::UnsupportedMetadataVersion(2))
64            }
65            frame_metadata::RuntimeMetadata::V3(_) => {
66                Err(TryFromError::UnsupportedMetadataVersion(3))
67            }
68            frame_metadata::RuntimeMetadata::V4(_) => {
69                Err(TryFromError::UnsupportedMetadataVersion(4))
70            }
71            frame_metadata::RuntimeMetadata::V5(_) => {
72                Err(TryFromError::UnsupportedMetadataVersion(5))
73            }
74            frame_metadata::RuntimeMetadata::V6(_) => {
75                Err(TryFromError::UnsupportedMetadataVersion(6))
76            }
77            frame_metadata::RuntimeMetadata::V7(_) => {
78                Err(TryFromError::UnsupportedMetadataVersion(7))
79            }
80            frame_metadata::RuntimeMetadata::V8(_) => {
81                Err(TryFromError::UnsupportedMetadataVersion(8))
82            }
83            frame_metadata::RuntimeMetadata::V9(_) => {
84                Err(TryFromError::UnsupportedMetadataVersion(9))
85            }
86            frame_metadata::RuntimeMetadata::V10(_) => {
87                Err(TryFromError::UnsupportedMetadataVersion(10))
88            }
89            frame_metadata::RuntimeMetadata::V11(_) => {
90                Err(TryFromError::UnsupportedMetadataVersion(11))
91            }
92            frame_metadata::RuntimeMetadata::V12(_) => {
93                Err(TryFromError::UnsupportedMetadataVersion(12))
94            }
95            frame_metadata::RuntimeMetadata::V13(_) => {
96                Err(TryFromError::UnsupportedMetadataVersion(13))
97            }
98            frame_metadata::RuntimeMetadata::V14(m) => m.try_into(),
99            frame_metadata::RuntimeMetadata::V15(m) => m.try_into(),
100            frame_metadata::RuntimeMetadata::V16(m) => m.try_into(),
101        }
102    }
103}