Introduce Metadata type (#974)

* WIP new Metadata type

* Finish basic Metadata impl inc hashing and validation

* remove caching from metadata; can add that higher up

* remove caches

* update retain to use Metadata

* clippy fixes

* update codegen to use Metadata

* clippy

* WIP fixing subxt lib

* WIP fixing tests, rebuild artifacts, fix OrderedMap::retain

* get --all-targets compiling

* move DispatchError type lookup back to being optional

* cargo clippy

* fix docs

* re-use VariantIndex to get variants

* add docs and enforce docs on metadata crate

* fix docs

* add test and fix docs

* cargo fmt

* address review comments

* update lockfiles

* ExactSizeIter so we can ask for len() of things (and hopefully soon is_empty()
This commit is contained in:
James Wilson
2023-05-25 10:35:21 +01:00
committed by GitHub
parent f344d0dd4d
commit b9f5419095
64 changed files with 6818 additions and 5719 deletions
+14 -25
View File
@@ -1,16 +1,11 @@
use crate::utils::type_description::print_type_description;
use crate::utils::{print_docs_with_indent, with_indent};
use clap::Args;
use color_eyre::eyre::eyre;
use std::fmt::Write;
use std::write;
use subxt::metadata::{types::PalletMetadata, Metadata};
use color_eyre::eyre::eyre;
use frame_metadata::v15::PalletMetadata;
use scale_info::form::PortableForm;
use subxt::Metadata;
use crate::utils::type_description::print_type_description;
use crate::utils::{print_docs_with_indent, with_indent};
#[derive(Debug, Clone, Args)]
pub struct ConstantsSubcommand {
@@ -20,9 +15,9 @@ pub struct ConstantsSubcommand {
pub(crate) fn explore_constants(
command: ConstantsSubcommand,
metadata: &Metadata,
pallet_metadata: &PalletMetadata<PortableForm>,
pallet_metadata: PalletMetadata,
) -> color_eyre::Result<()> {
let pallet_name = pallet_metadata.name.as_str();
let pallet_name = pallet_metadata.name();
let Some(constant_name) = command.constant else {
let available_constants = print_available_constants(pallet_metadata, pallet_name);
println!("Usage:\n subxt explore {pallet_name} constants <CONSTANT>\n explore a specific call within this pallet\n\n{available_constants}", );
@@ -30,7 +25,7 @@ pub(crate) fn explore_constants(
};
// if specified constant is wrong, show user the constants to choose from (but this time as an error):
let Some(constant) = pallet_metadata.constants.iter().find(|constant| constant.name.to_lowercase() == constant_name.to_lowercase()) else {
let Some(constant) = pallet_metadata.constants().find(|constant| constant.name().to_lowercase() == constant_name.to_lowercase()) else {
let available_constants = print_available_constants(pallet_metadata, pallet_name);
let description = format!("Usage:\n subxt explore {pallet_name} constants <CONSTANT>\n explore a specific call within this pallet\n\n{available_constants}", );
let err = eyre!("constant \"{constant_name}\" not found in \"{pallet_name}\" pallet!\n\n{description}");
@@ -39,13 +34,13 @@ pub(crate) fn explore_constants(
// docs
let mut output = String::new();
let doc_string = print_docs_with_indent(&constant.docs, 4);
let doc_string = print_docs_with_indent(constant.docs(), 4);
if !doc_string.is_empty() {
write!(output, "Description:\n{doc_string}")?;
}
// shape
let mut type_description = print_type_description(&constant.ty.id, metadata.types())?;
let mut type_description = print_type_description(&constant.ty(), metadata.types())?;
type_description = with_indent(type_description, 4);
write!(
output,
@@ -53,11 +48,8 @@ pub(crate) fn explore_constants(
)?;
// value
let scale_val = scale_value::scale::decode_as_type(
&mut &constant.value[..],
constant.ty.id,
metadata.types(),
)?;
let scale_val =
scale_value::scale::decode_as_type(&mut constant.value(), constant.ty(), metadata.types())?;
write!(
output,
"\n\nThe value of the constant is:\n {}",
@@ -68,15 +60,12 @@ pub(crate) fn explore_constants(
Ok(())
}
fn print_available_constants(
pallet_metadata: &PalletMetadata<PortableForm>,
pallet_name: &str,
) -> String {
if pallet_metadata.constants.is_empty() {
fn print_available_constants(pallet_metadata: PalletMetadata, pallet_name: &str) -> String {
if pallet_metadata.constants().len() == 0 {
return format!("No <CONSTANT>'s available in the \"{pallet_name}\" pallet.");
}
let mut output = format!("Available <CONSTANT>'s in the \"{pallet_name}\" pallet:");
let mut strings: Vec<_> = pallet_metadata.constants.iter().map(|c| &c.name).collect();
let mut strings: Vec<_> = pallet_metadata.constants().map(|c| c.name()).collect();
strings.sort();
for constant in strings {
output.push_str("\n ");