diff --git a/CHANGELOG.md b/CHANGELOG.md index 19ff7b2..0004ae5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# [0.4.0] - 2024-03-21 + +- Fix logic bug in `scale_typegen::utils::ensure_unique_type_paths`. + +- You can now specify a custom path to the `alloc` crate in the settings. It is used for type paths that are typically in the rust prelude or in the `std` library, like `Vec`, `Box` and `String`. This gives you the option to use the generated code in a `no_std` environment. + # [0.3.0] - 2024-03-19 - When generating rust code with `TypeGenerator::gerate_types_mod` we now validate that no type diff --git a/Cargo.lock b/Cargo.lock index aa7c308..7e8678e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -421,7 +421,7 @@ dependencies = [ [[package]] name = "scale-typegen" -version = "0.3.0" +version = "0.4.0" dependencies = [ "bitvec", "frame-metadata 16.0.0", @@ -439,7 +439,7 @@ dependencies = [ [[package]] name = "scale-typegen-description" -version = "0.3.0" +version = "0.4.0" dependencies = [ "anyhow", "indoc", diff --git a/Cargo.toml b/Cargo.toml index d3b54b9..68fa80c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ resolver = "2" [workspace.package] authors = ["Parity Technologies "] edition = "2021" -version = "0.3.0" +version = "0.4.0" rust-version = "1.70.0" license = "Apache-2.0 OR GPL-3.0" repository = "https://github.com/paritytech/scale-typegen" @@ -15,8 +15,8 @@ homepage = "https://www.parity.io/" [workspace.dependencies] -scale-typegen-description = { version = "0.3.0", path = "description" } -scale-typegen = { version = "0.3.0", path = "typegen" } +scale-typegen-description = { version = "0.4.0", path = "description" } +scale-typegen = { version = "0.4.0", 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 421635a..8b14bfc 100644 --- a/typegen/src/utils.rs +++ b/typegen/src/utils.rs @@ -34,7 +34,7 @@ pub fn ensure_unique_type_paths(types: &mut PortableRegistry) { let ty_id_b = group[0]; // all types in group are same shape; just check any one of them. let ty_b = types.resolve(ty_id_b).expect("ty exists"); if types_equal_extended_to_params(&ty.ty, ty_b) { - group.push(ty_id_b); + group.push(ty.id); added_to_existing_group = true; break; } @@ -166,3 +166,68 @@ pub(crate) fn types_equal_extended_to_params( _ => false, } } + +#[cfg(test)] +mod tests { + use scale_info::{meta_type, Path, PortableRegistry}; + + use crate::utils::ensure_unique_type_paths; + + #[test] + fn ensure_unique_type_paths_test() { + macro_rules! foo { + ($ty:ident, $prim:ident ) => { + struct $ty; + impl scale_info::TypeInfo for $ty { + type Identity = Self; + fn type_info() -> scale_info::Type { + scale_info::Type { + path: Path::new("Foo", "my::module"), + type_params: vec![], + type_def: scale_info::TypeDef::Primitive( + scale_info::TypeDefPrimitive::$prim, + ), + docs: vec![], + } + } + } + }; + } + + foo!(Foo1, Bool); + foo!(Foo2, Bool); + foo!(Foo3, U32); + foo!(Foo4, U128); + foo!(Foo5, U128); + foo!(Foo6, U128); + + let mut registry = scale_info::Registry::new(); + let id_1 = registry.register_type(&meta_type::()).id; + let id_2 = registry.register_type(&meta_type::()).id; + let id_3 = registry.register_type(&meta_type::()).id; + let id_4 = registry.register_type(&meta_type::()).id; + let id_5 = registry.register_type(&meta_type::()).id; + let id_6 = registry.register_type(&meta_type::()).id; + let mut registry = PortableRegistry::from(registry); + + // before: + let ident = |id: u32| registry.resolve(id).unwrap().path.ident().unwrap(); + assert_eq!(ident(id_1), "Foo"); + assert_eq!(ident(id_2), "Foo"); + assert_eq!(ident(id_3), "Foo"); + assert_eq!(ident(id_4), "Foo"); + assert_eq!(ident(id_5), "Foo"); + assert_eq!(ident(id_6), "Foo"); + + // after: + ensure_unique_type_paths(&mut registry); + + let ident = |id: u32| registry.resolve(id).unwrap().path.ident().unwrap(); + assert_eq!(ident(id_1), "Foo1"); + assert_eq!(ident(id_2), "Foo1"); + assert_eq!(ident(id_3), "Foo2"); + assert_eq!(ident(id_4), "Foo3"); + assert_eq!(ident(id_5), "Foo3"); + assert_eq!(ident(id_6), "Foo3"); + } +}