From c927ac1c6e041f96326e4a32e76ca13da8f6f5be Mon Sep 17 00:00:00 2001 From: main Date: Tue, 31 Mar 2026 14:21:52 -0400 Subject: Initial import --- template/fresh/Cargo.lock | 7 ++ template/fresh/Cargo.toml | 118 ++++++++++++++++++ template/fresh/check.py | 225 ++++++++++++++++++++++++++++++++++ template/fresh/clippy.toml | 5 + template/fresh/crates/app/Cargo.toml | 9 ++ template/fresh/crates/app/src/main.rs | 3 + template/fresh/rust-toolchain.toml | 4 + 7 files changed, 371 insertions(+) create mode 100644 template/fresh/Cargo.lock create mode 100644 template/fresh/Cargo.toml create mode 100644 template/fresh/check.py create mode 100644 template/fresh/clippy.toml create mode 100644 template/fresh/crates/app/Cargo.toml create mode 100644 template/fresh/crates/app/src/main.rs create mode 100644 template/fresh/rust-toolchain.toml (limited to 'template') diff --git a/template/fresh/Cargo.lock b/template/fresh/Cargo.lock new file mode 100644 index 0000000..791fca9 --- /dev/null +++ b/template/fresh/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "app" +version = "0.1.0" diff --git a/template/fresh/Cargo.toml b/template/fresh/Cargo.toml new file mode 100644 index 0000000..4c6b49f --- /dev/null +++ b/template/fresh/Cargo.toml @@ -0,0 +1,118 @@ +# Replace the member list, package metadata, and toolchain/MSRV values deliberately. + +[workspace] +members = ["crates/app"] +resolver = "3" + +[workspace.package] +edition = "2024" +license = "MIT" +rust-version = "1.90" +version = "0.1.0" + +[workspace.lints.rust] +elided_lifetimes_in_paths = "deny" +unexpected_cfgs = "deny" +unsafe_code = "deny" +unused_crate_dependencies = "warn" +unused_lifetimes = "deny" +unused_qualifications = "deny" +unused_results = "deny" + +[workspace.lints.rustdoc] +bare_urls = "deny" +broken_intra_doc_links = "deny" + +[workspace.lints.clippy] +all = { level = "deny", priority = -2 } +pedantic = { level = "deny", priority = -1 } +cargo = { level = "warn", priority = -3 } + +dbg_macro = "deny" +expect_used = "deny" +panic = "deny" +todo = "deny" +unimplemented = "deny" +unwrap_used = "deny" +allow_attributes_without_reason = "deny" + +cargo_common_metadata = "allow" +missing_errors_doc = "allow" +missing_panics_doc = "allow" +multiple_crate_versions = "allow" + +items_after_statements = "allow" +many_single_char_names = "allow" +match_same_arms = "allow" +module_name_repetitions = "allow" +similar_names = "allow" +struct_field_names = "allow" +too_many_arguments = "allow" +too_many_lines = "allow" +unnested_or_patterns = "allow" + +cast_lossless = "allow" +cast_possible_truncation = "allow" +cast_possible_wrap = "allow" +cast_precision_loss = "allow" +cast_sign_loss = "allow" +float_cmp = "allow" +implicit_hasher = "allow" +manual_let_else = "allow" +map_unwrap_or = "allow" +uninlined_format_args = "allow" + +ignored_unit_patterns = "allow" +must_use_candidate = "allow" +needless_pass_by_value = "allow" +no_effect_underscore_binding = "allow" +redundant_closure_for_method_calls = "allow" +ref_option = "allow" +return_self_not_must_use = "allow" +trivially_copy_pass_by_ref = "allow" +unused_async = "allow" +used_underscore_binding = "allow" + +[workspace.metadata.rust-starter] +format_command = ["cargo", "fmt", "--all", "--check"] +clippy_command = [ + "cargo", + "clippy", + "--workspace", + "--all-targets", + "--all-features", + "--", + "-D", + "warnings", +] +test_command = ["cargo", "test", "--workspace", "--all-targets", "--all-features"] +doc_command = ["cargo", "doc", "--workspace", "--all-features", "--no-deps"] +canonicalize_commands = [ + [ + "cargo", + "fix", + "--workspace", + "--all-targets", + "--all-features", + "--allow-dirty", + "--allow-staged", + "--allow-no-vcs", + ], + [ + "cargo", + "clippy", + "--fix", + "--workspace", + "--all-targets", + "--all-features", + "--allow-dirty", + "--allow-staged", + "--allow-no-vcs", + ], + ["cargo", "fmt", "--all"], +] + +[workspace.metadata.rust-starter.source_files] +max_lines = 2500 +include = ["*.rs", "**/*.rs"] +exclude = [] diff --git a/template/fresh/check.py b/template/fresh/check.py new file mode 100644 index 0000000..424497e --- /dev/null +++ b/template/fresh/check.py @@ -0,0 +1,225 @@ +#!/usr/bin/env python3 +from __future__ import annotations + +import argparse +import os +import subprocess +import tomllib +from dataclasses import dataclass +from pathlib import Path +from pathlib import PurePosixPath + + +ROOT = Path(__file__).resolve().parent +WORKSPACE_MANIFEST = ROOT / "Cargo.toml" +DEFAULT_MAX_SOURCE_FILE_LINES = 2500 +DEFAULT_SOURCE_FILE_INCLUDE = ("*.rs", "**/*.rs") +IGNORED_SOURCE_DIRS = frozenset( + {".direnv", ".git", ".hg", ".jj", ".svn", "__pycache__", "node_modules", "target", "vendor"} +) +Command = tuple[str, ...] +CommandSequence = tuple[Command, ...] + + +@dataclass(frozen=True, slots=True) +class SourceFilePolicy: + max_lines: int + include: tuple[str, ...] + exclude: tuple[str, ...] + + +def load_workspace_metadata() -> dict[str, object]: + workspace = tomllib.loads(WORKSPACE_MANIFEST.read_text(encoding="utf-8")) + return workspace["workspace"]["metadata"]["rust-starter"] + + +def load_command(value: object, *, key_path: str) -> Command: + if not isinstance(value, list) or not value or not all(isinstance(part, str) and part for part in value): + raise SystemExit(f"[check] invalid {key_path}: expected a non-empty string list") + return tuple(value) + + +def load_command_sequence(value: object, *, key_path: str) -> CommandSequence: + if not isinstance(value, list) or not value: + raise SystemExit(f"[check] invalid {key_path}: expected a non-empty list of commands") + + commands: list[Command] = [] + for index, command in enumerate(value, start=1): + commands.append(load_command(command, key_path=f"{key_path}[{index}]")) + return tuple(commands) + + +def load_commands(metadata: dict[str, object]) -> dict[str, Command | CommandSequence]: + commands: dict[str, Command | CommandSequence] = { + "format_command": load_command( + metadata.get("format_command"), + key_path="workspace.metadata.rust-starter.format_command", + ), + "clippy_command": load_command( + metadata.get("clippy_command"), + key_path="workspace.metadata.rust-starter.clippy_command", + ), + "test_command": load_command( + metadata.get("test_command"), + key_path="workspace.metadata.rust-starter.test_command", + ), + "canonicalize_commands": load_command_sequence( + metadata.get("canonicalize_commands"), + key_path="workspace.metadata.rust-starter.canonicalize_commands", + ), + } + + raw_doc_command = metadata.get("doc_command") + if raw_doc_command is not None: + commands["doc_command"] = load_command( + raw_doc_command, + key_path="workspace.metadata.rust-starter.doc_command", + ) + return commands + + +def load_patterns( + value: object, + *, + default: tuple[str, ...], + key_path: str, + allow_empty: bool, +) -> tuple[str, ...]: + if value is None: + return default + if not isinstance(value, list) or not all(isinstance(pattern, str) and pattern for pattern in value): + raise SystemExit(f"[check] invalid {key_path}: expected a string list") + if not allow_empty and not value: + raise SystemExit(f"[check] invalid {key_path}: expected at least one pattern") + return tuple(value) + + +def load_source_file_policy(metadata: dict[str, object]) -> SourceFilePolicy: + raw_policy = metadata.get("source_files") + if raw_policy is None: + return SourceFilePolicy(DEFAULT_MAX_SOURCE_FILE_LINES, DEFAULT_SOURCE_FILE_INCLUDE, ()) + if not isinstance(raw_policy, dict): + raise SystemExit("[check] invalid workspace.metadata.rust-starter.source_files: expected a table") + + max_lines = raw_policy.get("max_lines", DEFAULT_MAX_SOURCE_FILE_LINES) + if not isinstance(max_lines, int) or max_lines <= 0: + raise SystemExit( + "[check] invalid workspace.metadata.rust-starter.source_files.max_lines: expected a positive integer" + ) + + include = load_patterns( + raw_policy.get("include"), + default=DEFAULT_SOURCE_FILE_INCLUDE, + key_path="workspace.metadata.rust-starter.source_files.include", + allow_empty=False, + ) + exclude = load_patterns( + raw_policy.get("exclude"), + default=(), + key_path="workspace.metadata.rust-starter.source_files.exclude", + allow_empty=True, + ) + return SourceFilePolicy(max_lines, include, exclude) + + +def run(name: str, argv: Command) -> None: + print(f"[check] {name}: {' '.join(argv)}", flush=True) + proc = subprocess.run(argv, cwd=ROOT) + if proc.returncode != 0: + raise SystemExit(proc.returncode) + + +def parse_args() -> argparse.Namespace: + parser = argparse.ArgumentParser(description="Thin Rust starter check runner") + parser.add_argument( + "mode", + nargs="?", + choices=("check", "verify", "deep", "fix", "canon"), + default="check", + help=( + "Run canonicalization plus the fast gate, run a non-mutating verification gate, " + "include docs for the deep gate, or run only canonicalization." + ), + ) + return parser.parse_args() + + +def run_command_sequence(name: str, commands: CommandSequence) -> None: + for index, command in enumerate(commands, start=1): + run(f"{name}.{index}", command) + + +def matches_pattern(path: PurePosixPath, pattern: str) -> bool: + if path.match(pattern): + return True + prefix = "**/" + return pattern.startswith(prefix) and path.match(pattern.removeprefix(prefix)) + + +def iter_source_files(policy: SourceFilePolicy) -> list[Path]: + paths: list[Path] = [] + for current_root, dirnames, filenames in os.walk(ROOT): + dirnames[:] = sorted(name for name in dirnames if name not in IGNORED_SOURCE_DIRS) + current = Path(current_root) + for filename in filenames: + path = current / filename + relative_path = PurePosixPath(path.relative_to(ROOT).as_posix()) + if not any(matches_pattern(relative_path, pattern) for pattern in policy.include): + continue + if any(matches_pattern(relative_path, pattern) for pattern in policy.exclude): + continue + paths.append(path) + return sorted(paths) + + +def line_count(path: Path) -> int: + return len(path.read_text(encoding="utf-8").splitlines()) + + +def enforce_source_file_policy(policy: SourceFilePolicy) -> None: + paths = iter_source_files(policy) + print(f"[check] source-files: max {policy.max_lines} lines", flush=True) + violations: list[tuple[str, int]] = [] + for path in paths: + lines = line_count(path) + if lines > policy.max_lines: + violations.append((path.relative_to(ROOT).as_posix(), lines)) + if not violations: + return + + print( + f"[check] source-files: {len(violations)} file(s) exceed the configured limit", + flush=True, + ) + for relative_path, lines in violations: + print(f"[check] source-files: {relative_path}: {lines} lines", flush=True) + raise SystemExit(1) + + +def main() -> None: + metadata = load_workspace_metadata() + commands = load_commands(metadata) + source_file_policy = load_source_file_policy(metadata) + args = parse_args() + + if args.mode in {"fix", "canon"}: + run_command_sequence("canonicalize", commands["canonicalize_commands"]) + return + + enforce_source_file_policy(source_file_policy) + if args.mode != "verify": + run_command_sequence("canonicalize", commands["canonicalize_commands"]) + + run("fmt", commands["format_command"]) + run("clippy", commands["clippy_command"]) + run("test", commands["test_command"]) + + if args.mode == "deep" and "doc_command" in commands: + run("doc", commands["doc_command"]) + + +if __name__ == "__main__": + try: + main() + except KeyboardInterrupt: + raise SystemExit(130) diff --git a/template/fresh/clippy.toml b/template/fresh/clippy.toml new file mode 100644 index 0000000..b3be953 --- /dev/null +++ b/template/fresh/clippy.toml @@ -0,0 +1,5 @@ +# Keep this file tiny. Do not move the repo-wide allow/deny architecture here. + +allow-expect-in-tests = true +allow-unwrap-in-tests = true +allow-panic-in-tests = true diff --git a/template/fresh/crates/app/Cargo.toml b/template/fresh/crates/app/Cargo.toml new file mode 100644 index 0000000..ce1808a --- /dev/null +++ b/template/fresh/crates/app/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "app" +edition.workspace = true +license.workspace = true +rust-version.workspace = true +version.workspace = true + +[lints] +workspace = true diff --git a/template/fresh/crates/app/src/main.rs b/template/fresh/crates/app/src/main.rs new file mode 100644 index 0000000..7527576 --- /dev/null +++ b/template/fresh/crates/app/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("hello"); +} diff --git a/template/fresh/rust-toolchain.toml b/template/fresh/rust-toolchain.toml new file mode 100644 index 0000000..66f329f --- /dev/null +++ b/template/fresh/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "1.90.0" +profile = "minimal" +components = ["clippy", "rustfmt"] -- cgit v1.2.3