import os import sys import re def audit_file(filepath): with open(filepath, 'r', encoding='utf-8') as f: content = f.read() lines = content.split('\n') errors = [] # 1. Check header header_found = False for i, line in enumerate(lines[:10]): if line.startswith('//! **File:**') or line.startswith('//! MIT License'): header_found = True break if not header_found and not filepath.endswith('mod.rs'): errors.append("Missing or invalid `//!` header block.") # 2. Check function length and blank lines in_function = False func_start = 0 func_name = "" brace_level = 0 blank_lines_in_func = [] inline_comment_count = 0 for i, line in enumerate(lines): stripped = line.strip() # Check inline comments if '//' in line and not line.lstrip().startswith('//!'): if not line.lstrip().startswith('///'): inline_comment_count += 1 if not in_function: if stripped.startswith("fn ") or stripped.startswith("pub fn ") or " fn " in stripped: in_function = True func_start = i func_name = stripped.split("fn ")[1].split("(")[0].strip() brace_level += stripped.count('{') - stripped.count('}') else: brace_level += stripped.count('{') - stripped.count('}') if stripped == "": blank_lines_in_func.append(i + 1) if brace_level == 0: in_function = False func_len = i - func_start + 1 if func_len > 8: errors.append(f"Function `{func_name}` exceeds 8 lines (length: {func_len}, lines {func_start+1}-{i+1}).") if blank_lines_in_func: errors.append(f"Function `{func_name}` contains blank lines at: {blank_lines_in_func}") blank_lines_in_func = [] if inline_comment_count == 0 and not filepath.endswith('mod.rs'): errors.append("File contains 0 inline (`//`) comments. Must have logic explanations.") return errors def main(directory): total_errors = 0 for root, _, files in os.walk(directory): if 'target' in root or '.git' in root: continue for file in files: if file.endswith('.rs'): filepath = os.path.join(root, file) errors = audit_file(filepath) if errors: print(f"\n[FAIL] {filepath}") for err in errors: print(f" - {err}") total_errors += 1 else: print(f"[PASS] {filepath}") if total_errors > 0: sys.exit(1) if __name__ == "__main__": if len(sys.argv) < 2: print("Usage: python auditor.py ") sys.exit(1) main(sys.argv[1])