Skip to content

Commit

Permalink
no-std + legacy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jsdw committed Sep 27, 2024
1 parent 8319408 commit f43448b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/decoding/extrinsic_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use alloc::borrow::Cow;
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use core::write;

Expand Down
7 changes: 4 additions & 3 deletions src/decoding/storage_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use alloc::borrow::Cow;
use alloc::borrow::ToOwned;
use alloc::vec;
use alloc::vec::Vec;

/// This is implemented for all metadatas exposed from `frame_metadata` and is responsible for extracting the
Expand Down Expand Up @@ -635,11 +636,11 @@ mod legacy {
Ok(id)
}

fn sanitize_type_name(name: &str) -> std::borrow::Cow<'_, str> {
fn sanitize_type_name(name: &str) -> Cow<'_, str> {
if name.contains('\n') {
std::borrow::Cow::Owned(name.replace('\n', ""))
Cow::Owned(name.replace('\n', ""))
} else {
std::borrow::Cow::Borrowed(name)
Cow::Borrowed(name)
}
}
}
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub use list_storage_entries::{list_storage_entries, StorageEntry};
#[cfg(feature = "legacy")]
pub use type_registry_from_metadata::type_registry_from_metadata;

/// A utility function to unwrap the [`DecodeDifferent`] enum found in earlier metadata versions.
/// A utility function to unwrap the `DecodeDifferent` enum found in earlier metadata versions.
#[cfg(feature = "legacy")]
pub fn as_decoded<A, B>(item: &frame_metadata::decode_different::DecodeDifferent<A, B>) -> &B {
match item {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/list_storage_entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ const _: () = {
Either::Right(entries.iter().map(|entry_meta| {
let entry = as_decoded(&entry_meta.name);
StorageEntry {
pallet: Cow::Borrowed(pallet.as_str()),
entry: Cow::Borrowed(entry.as_str()),
pallet: Cow::Borrowed(pallet.as_ref()),
entry: Cow::Borrowed(entry.as_ref()),
}
}))
})
Expand Down
33 changes: 20 additions & 13 deletions src/utils/type_registry_from_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ pub trait ToTypeRegistry {

#[cfg(feature = "legacy")]
const _: () = {
use super::as_decoded;
use alloc::borrow::ToOwned;
use alloc::format;
use alloc::vec;
use alloc::vec::Vec;
use scale_info_legacy::type_shape::{Field, TypeShape, Variant, VariantDesc};
use scale_info_legacy::InsertName;
use scale_info_legacy::{LookupName, TypeRegistry};

macro_rules! impl_for_v8_to_v13 {
($path:path $(, $builtin_index:ident)?) => {
impl ToTypeRegistry for $path {
fn to_type_registry(&self) -> Result<scale_info_legacy::TypeRegistry, scale_info_legacy::lookup_name::ParseError> {
use super::as_decoded;
use scale_info_legacy::type_shape::{Field, TypeShape, Variant, VariantDesc};
use scale_info_legacy::InsertName;
use scale_info_legacy::{LookupName, TypeRegistry};

let metadata = self;
let mut new_types = TypeRegistry::empty();
let modules = as_decoded(&metadata.modules);
Expand Down Expand Up @@ -87,7 +91,9 @@ const _: () = {
};
)?

let module_name = as_decoded(&module.name);
// as_ref to work when scale-info returns `&static str`
// instead of `String` in no-std mode.
let module_name: &str = as_decoded(&module.name).as_ref();

//// 1. Add calls to the type registry
if let Some(calls) = &module.calls.as_ref() {
Expand All @@ -96,20 +102,21 @@ const _: () = {
// Iterate over each call in the module and turn into variants:
let mut call_variants: Vec<Variant> = vec![];
for (c_idx, call) in calls.iter().enumerate() {
let call_name = as_decoded(&call.name);
let call_name: &str = as_decoded(&call.name).as_ref();
let args = as_decoded(&call.arguments)
.iter()
.map(|arg| {
let name: &str = as_decoded(&arg.name).as_ref();
Ok(Field {
name: as_decoded(&arg.name).to_owned(),
name: name.to_owned(),
value: LookupName::parse(&as_decoded(&arg.ty))?.in_pallet(module_name),
})
})
.collect::<Result<_,_>>()?;

call_variants.push(Variant {
index: c_idx as u8,
name: call_name.clone(),
name: call_name.to_owned(),
fields: VariantDesc::StructOf(args)
});
}
Expand All @@ -123,7 +130,7 @@ const _: () = {
let call_enum_lookup_name = LookupName::parse(&call_enum_name_str).unwrap();
call_module_variants.push(Variant {
index: calls_index,
name: module_name.clone(),
name: module_name.to_owned(),
fields: VariantDesc::TupleOf(vec![call_enum_lookup_name])
});
}
Expand All @@ -134,7 +141,7 @@ const _: () = {

let mut event_variants: Vec<Variant> = vec![];
for (e_idx, event)in events.iter().enumerate() {
let event_name = as_decoded(&event.name);
let event_name: &str = as_decoded(&event.name).as_ref();
let args = as_decoded(&event.arguments)
.iter()
.map(|arg| {
Expand All @@ -144,7 +151,7 @@ const _: () = {

event_variants.push(Variant {
index: e_idx as u8,
name: event_name.clone(),
name: event_name.to_owned(),
fields: VariantDesc::TupleOf(args)
});
}
Expand All @@ -158,7 +165,7 @@ const _: () = {
let event_enum_lookup_name = LookupName::parse(&event_enum_name_str).unwrap();
event_module_variants.push(Variant {
index: events_index,
name: module_name.clone(),
name: module_name.to_owned(),
fields: VariantDesc::TupleOf(vec![event_enum_lookup_name])
});
}
Expand Down

0 comments on commit f43448b

Please sign in to comment.