gear_subxt/metadata/
metadata_type.rs

1// Copyright 2019-2023 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 crate::error::MetadataError;
6use std::sync::Arc;
7
8/// A cheaply clone-able representation of the runtime metadata received from a node.
9#[derive(Clone, Debug)]
10pub struct Metadata {
11    inner: Arc<subxt_metadata::Metadata>,
12}
13
14impl std::ops::Deref for Metadata {
15    type Target = subxt_metadata::Metadata;
16    fn deref(&self) -> &Self::Target {
17        &self.inner
18    }
19}
20
21impl Metadata {
22    pub(crate) fn new(md: subxt_metadata::Metadata) -> Self {
23        Metadata {
24            inner: Arc::new(md),
25        }
26    }
27
28    /// Identical to `metadata.pallet_by_name()`, but returns an error if the pallet is not found.
29    pub fn pallet_by_name_err(
30        &self,
31        name: &str,
32    ) -> Result<subxt_metadata::PalletMetadata, MetadataError> {
33        self.pallet_by_name(name)
34            .ok_or_else(|| MetadataError::PalletNameNotFound(name.to_owned()))
35    }
36
37    /// Identical to `metadata.pallet_by_index()`, but returns an error if the pallet is not found.
38    pub fn pallet_by_index_err(
39        &self,
40        index: u8,
41    ) -> Result<subxt_metadata::PalletMetadata, MetadataError> {
42        self.pallet_by_index(index)
43            .ok_or(MetadataError::PalletIndexNotFound(index))
44    }
45
46    /// Identical to `metadata.runtime_api_trait_by_name()`, but returns an error if the trait is not found.
47    pub fn runtime_api_trait_by_name_err(
48        &self,
49        name: &str,
50    ) -> Result<subxt_metadata::RuntimeApiMetadata, MetadataError> {
51        self.runtime_api_trait_by_name(name)
52            .ok_or_else(|| MetadataError::RuntimeTraitNotFound(name.to_owned()))
53    }
54}
55
56impl From<subxt_metadata::Metadata> for Metadata {
57    fn from(md: subxt_metadata::Metadata) -> Self {
58        Metadata::new(md)
59    }
60}
61
62impl TryFrom<frame_metadata::RuntimeMetadataPrefixed> for Metadata {
63    type Error = subxt_metadata::TryFromError;
64    fn try_from(value: frame_metadata::RuntimeMetadataPrefixed) -> Result<Self, Self::Error> {
65        subxt_metadata::Metadata::try_from(value).map(Metadata::from)
66    }
67}
68
69impl codec::Decode for Metadata {
70    fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
71        subxt_metadata::Metadata::decode(input).map(Metadata::new)
72    }
73}