Skip to content

Commit

Permalink
Fix bug with ensure_unique_type_paths and prep for patch release. (#22
Browse files Browse the repository at this point in the history
)

* fix bug and bump version

* bump to 0.4.0
  • Loading branch information
tadeohepperle authored Mar 21, 2024
1 parent 4ce4efd commit 59d5747
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resolver = "2"
[workspace.package]
authors = ["Parity Technologies <[email protected]>"]
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"
Expand All @@ -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"] }
Expand Down
67 changes: 66 additions & 1 deletion typegen/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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::<Foo1>()).id;
let id_2 = registry.register_type(&meta_type::<Foo2>()).id;
let id_3 = registry.register_type(&meta_type::<Foo3>()).id;
let id_4 = registry.register_type(&meta_type::<Foo4>()).id;
let id_5 = registry.register_type(&meta_type::<Foo5>()).id;
let id_6 = registry.register_type(&meta_type::<Foo6>()).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");
}
}

0 comments on commit 59d5747

Please sign in to comment.