Skip to content

Commit

Permalink
Merge pull request #240 from JonathonReinhart/rust-codefmt
Browse files Browse the repository at this point in the history
Add Rust code formatting to code_format.py
  • Loading branch information
JonathonReinhart authored Jan 7, 2024
2 parents 1e26d0e + e82f46e commit 18135bb
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions code_format.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#!/usr/bin/env python3
import argparse
import black
import subprocess
from pathlib import Path

PROJECT_DIR = Path(__file__).parent.resolve()
SCUBAINIT_DIR = PROJECT_DIR / "scubainit"


def _run_black(fix: bool) -> bool:
args = [
".",
str(PROJECT_DIR),
]

if not fix:
Expand All @@ -25,6 +30,27 @@ def _run_black(fix: bool) -> bool:
raise Exception(f"Unexpected exit status: {status}")


def _rust_fmt(fix: bool) -> bool:
args = [
"cargo",
"fmt",
]

if not fix:
# check only
args += [
"--check",
]

status = subprocess.call(args, cwd=SCUBAINIT_DIR)
if status == 0:
print("Ok")
return True
if status == 1:
return False
raise Exception(f"Unexpected exit status: {status}")


def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
Expand All @@ -38,10 +64,17 @@ def _parse_args() -> argparse.Namespace:
def main() -> None:
args = _parse_args()

print(f"{'Fixing' if args.fix else 'Checking'} code formatting...")
ok = _run_black(args.fix)
ok = True

print(f"\n{'Fixing' if args.fix else 'Checking'} Python code formatting...")
ok &= _run_black(args.fix)

print(f"\n{'Fixing' if args.fix else 'Checking'} Rust code formatting...")
ok &= _rust_fmt(args.fix)

if not ok:
print("\nTo fix, rerun with --fix")
raise SystemExit(1)


if __name__ == "__main__":
Expand Down

0 comments on commit 18135bb

Please sign in to comment.