import os
import sys
import argparse
import subprocess
def run_rust_tool(crate_path, goal, hard_limit):
script_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(script_dir)
if os.path.isabs(crate_path):
abs_crate_path = crate_path
else:
abs_crate_path = os.path.join(project_root, crate_path)
cmd = [
'cargo', 'run', '--release', '-p', 'verify-rust-metrics',
'--',
abs_crate_path,
'--complexity-goal', str(goal),
'--complexity-hard', str(hard_limit)
]
try:
result = subprocess.run(
cmd,
cwd=project_root,
capture_output=True,
text=True
)
if result.stdout:
print(result.stdout, end='')
if result.stderr:
print(result.stderr, end='', file=sys.stderr)
return result.returncode
except Exception as e:
print(f"Error running Rust verification tool: {e}", file=sys.stderr)
print("Falling back to clippy...", file=sys.stderr)
return None
def main():
parser = argparse.ArgumentParser(
description='Verify cognitive complexity is within limits'
)
parser.add_argument(
'crate_path',
help='Path to the crate to check (e.g., multi-llm)'
)
parser.add_argument(
'--goal',
type=int,
default=10,
help='Goal cognitive complexity (default: 10)'
)
parser.add_argument(
'--hard-limit',
type=int,
default=15,
help='Hard limit cognitive complexity (default: 15)'
)
args = parser.parse_args()
exit_code = run_rust_tool(args.crate_path, args.goal, args.hard_limit)
if exit_code is not None:
return exit_code
print("❌ Rust verification tool not available. Please build it:", file=sys.stderr)
print(" cargo build --release -p verify-rust-metrics", file=sys.stderr)
return 2
if __name__ == '__main__':
sys.exit(main())