#!/usr/bin/env bash
# Runs the same fmt and clippy checks CI runs (see .github/workflows/ci.yml).
# Install with: git config core.hooksPath .githooks
# Bypass with: git commit --no-verify (not recommended)

set -euo pipefail

if ! command -v cargo >/dev/null 2>&1; then
    echo "pre-commit: cargo not found on PATH; skipping checks" >&2
    exit 0
fi

echo "pre-commit: cargo fmt --check"
cargo fmt --check

echo "pre-commit: cargo clippy -- -D warnings"
cargo clippy --quiet -- -D warnings

# Match CI's third gate: full lib test suite. Keeps targeted-test-only
# commits (which can land breakage in unrelated modules) from passing
# pre-commit only to immediately fail CI. Skip with --no-verify if you
# really need to commit a WIP — but expect CI red.
echo "pre-commit: cargo test --lib --quiet"
cargo test --lib --quiet
