From 4cb91df73d19f4dbc3b17cfe8cb82e3bdbbcc58d Mon Sep 17 00:00:00 2001 From: James Wilson Date: Tue, 26 Mar 2024 15:11:14 +0000 Subject: [PATCH] Use type index for now, and prep 0.4.2 release --- CHANGELOG.md | 4 ++++ Cargo.lock | 4 ++-- Cargo.toml | 6 +++--- typegen/src/utils.rs | 23 +++++++++++++++-------- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78fdd2e..9cfab89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# [0.4.2] - 2024-03-26 + +- Revert some code simplification in 0.4.1 so that older metadata (built with scale-info versions prior to 2.11.1 and using the `retain()` method) is more likely to work. + # [0.4.1] - 2024-03-22 - Bump `scale-info` to 2.11.1 to fix an issue in `scale_typegen::utils::ensure_unique_type_paths` for cases where a type's index and id do not line up, and simplify code a touch. diff --git a/Cargo.lock b/Cargo.lock index 3a245c1..5610262 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -421,7 +421,7 @@ dependencies = [ [[package]] name = "scale-typegen" -version = "0.4.1" +version = "0.4.2" dependencies = [ "bitvec", "frame-metadata 16.0.0", @@ -439,7 +439,7 @@ dependencies = [ [[package]] name = "scale-typegen-description" -version = "0.4.1" +version = "0.4.2" dependencies = [ "anyhow", "indoc", diff --git a/Cargo.toml b/Cargo.toml index 07e1c8f..55c3615 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,15 +6,15 @@ resolver = "2" [workspace.package] authors = ["Parity Technologies "] edition = "2021" -version = "0.4.1" +version = "0.4.2" rust-version = "1.70.0" license = "Apache-2.0 OR GPL-3.0" repository = "https://github.com/paritytech/scale-typegen" homepage = "https://www.parity.io/" [workspace.dependencies] -scale-typegen-description = { version = "0.4.1", path = "description" } -scale-typegen = { version = "0.4.1", path = "typegen" } +scale-typegen-description = { version = "0.4.2", path = "description" } +scale-typegen = { version = "0.4.2", path = "typegen" } # external dependencies parity-scale-codec = { version = "3.6.5", features = ["derive"] } diff --git a/typegen/src/utils.rs b/typegen/src/utils.rs index b7cfd3a..ffa4869 100644 --- a/typegen/src/utils.rs +++ b/typegen/src/utils.rs @@ -16,15 +16,22 @@ pub fn ensure_unique_type_paths(types: &mut PortableRegistry) { let mut types_with_same_type_path_grouped_by_shape = HashMap::<&[String], Vec>>::new(); // First, group types if they are similar (same path, same shape). - for ty in types.types.iter() { + for (ty_idx, ty) in types.types.iter().enumerate() { + // We use the index of the type in the types registry instead of `ty.id`. The two + // _should_ be identical, but prior to `scale-info` 2.11.1 they sometimes weren't + // when `registry.retain()` was used, and so to avoid older metadata files breaking + // things, let's stick to using the index for a while: + let ty_idx = ty_idx as u32; + let ty = &ty.ty; + // Ignore types without a path (i.e prelude types). - if ty.ty.path.namespace().is_empty() { + if ty.path.namespace().is_empty() { continue; }; // get groups that share this path already, if any. let groups_with_same_path = types_with_same_type_path_grouped_by_shape - .entry(&ty.ty.path.segments) + .entry(&ty.path.segments) .or_default(); // Compare existing groups to check which to add our type ID to. @@ -33,9 +40,9 @@ pub fn ensure_unique_type_paths(types: &mut PortableRegistry) { let other_ty_in_group_idx = group[0]; // all types in group are same shape; just check any one of them. let other_ty_in_group = types .resolve(other_ty_in_group_idx) - .expect("type is present; qed;"); - if types_equal_extended_to_params(&ty.ty, other_ty_in_group) { - group.push(ty.id); + .expect("type is present (1); qed;"); + if types_equal_extended_to_params(ty, other_ty_in_group) { + group.push(ty_idx); added_to_existing_group = true; break; } @@ -43,7 +50,7 @@ pub fn ensure_unique_type_paths(types: &mut PortableRegistry) { // We didn't find a matching group, so add it to a new one. if !added_to_existing_group { - groups_with_same_path.push(vec![ty.id]) + groups_with_same_path.push(vec![ty_idx]) } } @@ -60,7 +67,7 @@ pub fn ensure_unique_type_paths(types: &mut PortableRegistry) { let ty = types .types .get_mut(ty_id as usize) - .expect("type is present; qed;"); + .expect("type is present (2); qed;"); let name = ty.ty.path.segments.last_mut().expect("This is only empty for builtin types, that are filtered out with namespace().is_empty() above; qed;"); *name = format!("{name}{n}"); // e.g. Header1, Header2, Header3, ... }