#!/usr/bin/env bash
# 启用：git config core.hooksPath .githooks；跳过：git commit --no-verify
set -euo pipefail
cd "$(git rev-parse --show-toplevel)"

# 装饰性分隔注释（`// ====` / `// ────`）不要：只做视觉分组、不带任何「为什么」，用空行加语义排序分组
staged_rs="$(git diff --cached --name-only --diff-filter=ACM -- '*.rs')"
if [[ -n "$staged_rs" ]]; then
  matches="$(echo "$staged_rs" | xargs grep -nE '^\s*//[/!]?\s*[=─-]{3,}\s*$' 2>/dev/null || true)"
  if [[ -n "$matches" ]]; then
    echo "$matches"
    echo "装饰性分隔注释不要，用空行 + 语义排序分组" >&2
    exit 1
  fi
fi

cargo fmt --all --check

# clippy 要按平台排除编不了的壳，划分与 ci.yml 的三个 job 一致。
# IMK 壳依赖 objc2，非 Apple 平台上是硬 compile_error!，排除不掉就只能整条命令失败；
# 其余平台排除它之后，Windows 三件套在本机也编得过（已在 Windows 上验过）。
case "$(uname -s)" in
  Darwin) clippy_scope=(--workspace) ;;
  *) clippy_scope=(--workspace --exclude qingjian-macos) ;;
esac
cargo clippy "${clippy_scope[@]}" --all-targets -- -D warnings
