Rust: preserve ordering in rust generated code

This is a small devex improvement to the rust code generator.

Usage of `sorted` in `rustgen.py` was causing the generated code to be
completely reshuffled on renames, which made diffs hard to follow. As an
example see [this generated file diff](https://github.com/github/codeql/pull/19059/files#diff-c938ba77a3398dd4c633ada5702a03477705c24740a2f7d1e40d4b270d8c3f86).

This will make the order deterministically based on the order of
definitions in the schema file. This means that renames will find the
same place in the generated file, and the place in the generated file
will generally be more predictable with respect to the schema.

However, that does mean this change is heavily reshuffling the generated
code.
This commit is contained in:
Paolo Tranquilli
2025-03-20 12:03:26 +01:00
parent b09669646d
commit 4110636032
3 changed files with 1771 additions and 1769 deletions

View File

@@ -96,7 +96,9 @@ class Processor:
name=name,
fields=fields,
detached_fields=detached_fields,
ancestors=sorted(set(a.name for a in _get_ancestors(cls, self._classmap))),
# remove duplicates but preserve ordering
# (`dict` preserves insertion order while `set` doesn't)
ancestors=[*{a.name: None for a in _get_ancestors(cls, self._classmap)}],
entry_table=inflection.tableize(cls.name) if not cls.derived else None,
)

View File

@@ -1,2 +1,2 @@
mod.rs 4bcb9def847469aae9d8649461546b7c21ec97cf6e63d3cf394e339915ce65d7 4bcb9def847469aae9d8649461546b7c21ec97cf6e63d3cf394e339915ce65d7
top.rs b290c599a2696c64c3391003de990c7036ac1990f43b507723d10519d18fcbee b290c599a2696c64c3391003de990c7036ac1990f43b507723d10519d18fcbee
top.rs 765a45234eacc510b2eeef9c44852766c3a818902108a2e921327804032bbc5a 765a45234eacc510b2eeef9c44852766c3a818902108a2e921327804032bbc5a

File diff suppressed because it is too large Load Diff