Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure tokens are generated in lexicographic order #51

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading