#!/usr/bin/env bash
set -euo pipefail

repo_root="$(git rev-parse --show-toplevel)"
cd "$repo_root"

echo "Running cargo fmt --all -- --check..."
if ! cargo fmt --all -- --check; then
    echo "cargo fmt failed. Run 'cargo fmt --all' to format files." >&2
    exit 1
fi

echo "Running cargo clippy --all-targets --all-features -- -D warnings..."
if ! cargo clippy --all-targets --all-features -- -D warnings; then
    echo "cargo clippy failed. Fix the diagnostics above before committing." >&2
    exit 1
fi

echo "Running cargo test --all-targets --all-features..."
if ! cargo test --all-targets --all-features; then
    echo "cargo test failed. Fix the failing tests before committing." >&2
    exit 1
fi

echo "pre-commit checks passed."
