#!/usr/bin/env bash
set -e

# Only check staged Rust files to keep the hook fast
STAGED_RS=$(git diff --cached --name-only --diff-filter=ACM -- '*.rs')
if [ -z "$STAGED_RS" ]; then
    exit 0
fi

echo "pre-commit: checking formatting..."
if ! cargo fmt --all -- --check 2>/dev/null; then
    echo ""
    echo "ERROR: formatting check failed. Run 'cargo fmt' and re-stage."
    exit 1
fi

echo "pre-commit: running clippy..."
if ! cargo clippy --all-targets -- -D warnings 2>/dev/null; then
    echo ""
    echo "ERROR: clippy check failed. Fix warnings and re-stage."
    exit 1
fi

echo "pre-commit: all checks passed."
