Canonical ordering lefthook checker

This commit is contained in:
mfahampshire
2026-04-17 08:17:18 +01:00
parent 924d7d1ccc
commit 6ee1f16ce8
3 changed files with 46 additions and 1 deletions
+3
View File
@@ -51,3 +51,6 @@ pre-commit:
glob: "*.rs"
run: cargo fmt
stage_fixed: true
cargo-toml-order:
glob: "**/Cargo.toml"
run: python3 tools/internal/check_cargo_toml_field_order.py {staged_files}
+1 -1
View File
@@ -7,8 +7,8 @@ edition = "2021"
license = ""
repository = ""
rust-version = "1.85"
default-run = "NymWallet"
build = "src/build.rs"
default-run = "NymWallet"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -0,0 +1,42 @@
#!/usr/bin/env python3
"""Check that [package] fields in Cargo.toml follow canonical ordering."""
import re, sys
ORDER = [
"name", "description", "version", "authors", "edition", "license",
"repository", "homepage", "documentation", "rust-version", "readme",
"publish", "keywords", "exclude", "build", "links", "default-run",
"resolver",
]
bad = []
for path in sys.argv[1:]:
fields, in_pkg = [], False
for line in open(path):
s = line.strip()
if s == "[package]":
in_pkg = True
continue
if in_pkg and s.startswith("["):
break
if in_pkg:
m = re.match(r"^(\w[\w-]*)", s)
if m and "=" in line:
fields.append(m.group(1))
unknown = [f for f in fields if f not in ORDER]
if unknown:
bad.append((path, f"unknown field(s): {', '.join(unknown)}"))
continue
expected = sorted(fields, key=ORDER.index)
if fields != expected:
first = next(a for a, b in zip(fields, expected) if a != b)
bad.append((path, f"first mismatch: '{first}'"))
if bad:
print("[package] fields out of canonical order:")
for path, reason in bad:
print(f" {path} ({reason})")
print("\nCanonical order:")
for f in ORDER:
print(f" - {f}")
sys.exit(1)