Skip to content

Commit

Permalink
fix: ensure tokens are generated in lexicographic order (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm authored Aug 14, 2024
1 parent 6c90910 commit fb91215
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions crates/rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,39 @@ pub fn abi_to_tokenstream(

tokens.push(CairoContract::expand(contract_name.clone()));

for s in &abi_tokens.structs {
let mut sorted_structs = abi_tokens.structs.clone();
sorted_structs.sort_by(|a, b| {
let a_name = a
.to_composite()
.expect("composite expected")
.type_name_or_alias();
let b_name = b
.to_composite()
.expect("composite expected")
.type_name_or_alias();
a_name.cmp(&b_name)
});

let mut sorted_enums = abi_tokens.enums.clone();
sorted_enums.sort_by(|a, b| {
let a_name = a
.to_composite()
.expect("composite expected")
.type_name_or_alias();
let b_name = b
.to_composite()
.expect("composite expected")
.type_name_or_alias();
a_name.cmp(&b_name)
});

for s in &sorted_structs {
let s_composite = s.to_composite().expect("composite expected");
tokens.push(CairoStruct::expand_decl(s_composite, derives));
tokens.push(CairoStruct::expand_impl(s_composite));
}

for e in &abi_tokens.enums {
for e in &sorted_enums {
let e_composite = e.to_composite().expect("composite expected");
tokens.push(CairoEnum::expand_decl(e_composite, derives));
tokens.push(CairoEnum::expand_impl(e_composite));
Expand All @@ -198,6 +224,12 @@ pub fn abi_to_tokenstream(
functions.extend(funcs.clone());
}

functions.sort_by(|a, b| {
let a_name = a.to_function().expect("function expected").name.to_string();
let b_name = b.to_function().expect("function expected").name.to_string();
a_name.cmp(&b_name)
});

for f in functions {
let f = f.to_function().expect("function expected");
match f.state_mutability {
Expand Down

0 comments on commit fb91215

Please sign in to comment.