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,
)