#!/usr/bin/env bash set -euo pipefail MODE="${1:-}" if [[ "$MODE" != "style-clippy" && "$MODE" != "test" ]]; then echo "Usage: $0 {style-clippy|test}" >&2 exit 2 fi if ROOT_DIR="$(git rev-parse --show-toplevel 2>/dev/null)"; then : else SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)" ROOT_DIR="$(cd -- "${SCRIPT_DIR}/../.." && pwd -P)" fi cd "$ROOT_DIR" export CARGO_TERM_COLOR="${CARGO_TERM_COLOR:-always}" export CARGO_HOME="${CARGO_HOME:-$HOME/.cargo}" export RUSTUP_HOME="${RUSTUP_HOME:-$HOME/.rustup}" export PATH="$CARGO_HOME/bin:$PATH" CARGO_BINSTALL_VERSION="${CARGO_BINSTALL_VERSION:-1.20.1}" UV_VERSION="${UV_VERSION:-0.11.28}" UV_SCRIPT_PYTHON_VERSION="${UV_SCRIPT_PYTHON_VERSION:-3.12}" CARGO_SORT_VERSION_REQ="${CARGO_SORT_VERSION_REQ:-2}" CARGO_DENY_VERSION_REQ="${CARGO_DENY_VERSION_REQ:-0.20}" CARGO_NEXTEST_VERSION_REQ="${CARGO_NEXTEST_VERSION_REQ:-0.9}" log_section() { echo "--- $*" } rust_toolchain() { awk -F '"' '/channel[[:space:]]*=/ { print $2; exit }' rust-toolchain.toml } install_rust_toolchain() { log_section "Installing Rust toolchain" if ! command -v rustup >/dev/null 2>&1; then curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \ | sh -s -- -y --profile minimal --default-toolchain none fi local toolchain toolchain="$(rust_toolchain)" rustup toolchain install "$toolchain" --profile minimal --component rustfmt,clippy rustup component add --toolchain "$toolchain" rustfmt clippy } install_cargo_binstall() { log_section "Installing cargo-binstall ${CARGO_BINSTALL_VERSION}" curl -L --proto '=https' --tlsv1.2 -sSf \ "https://raw.githubusercontent.com/cargo-bins/cargo-binstall/v${CARGO_BINSTALL_VERSION}/install-from-binstall-release.sh" \ | env BINSTALL_VERSION="$CARGO_BINSTALL_VERSION" bash cargo-binstall -V } install_cargo_sort() { log_section "Installing cargo-sort ${CARGO_SORT_VERSION_REQ}" cargo binstall --no-confirm --force --locked "cargo-sort@${CARGO_SORT_VERSION_REQ}" } install_cargo_deny() { log_section "Installing cargo-deny ${CARGO_DENY_VERSION_REQ}" cargo binstall --no-confirm --force --locked "cargo-deny@${CARGO_DENY_VERSION_REQ}" } install_cargo_nextest() { log_section "Installing cargo-nextest ${CARGO_NEXTEST_VERSION_REQ}" cargo binstall \ --no-confirm \ --force \ --secure \ --locked \ "cargo-nextest@${CARGO_NEXTEST_VERSION_REQ}" } install_uv() { log_section "Installing uv ${UV_VERSION}" curl -L --proto '=https' --tlsv1.2 -sSf \ "https://github.com/astral-sh/uv/releases/download/${UV_VERSION}/uv-installer.sh" \ | env UV_INSTALL_DIR="$CARGO_HOME/bin" sh uv --version uv python install "$UV_SCRIPT_PYTHON_VERSION" } run_style_clippy() { install_cargo_binstall install_cargo_sort install_cargo_deny log_section "Checking Rust formatting" cargo fmt --manifest-path rust/Cargo.toml --all -- --check log_section "Checking Cargo.toml ordering" cargo sort --workspace --check rust log_section "Checking Rust dependency bans" cargo deny \ --manifest-path rust/Cargo.toml \ --config rust/deny.toml \ check \ bans log_section "Running clippy" cargo clippy \ --manifest-path rust/Cargo.toml \ --workspace \ --all-targets \ --all-features \ --locked \ -- \ -D warnings } run_tests() { install_uv install_cargo_binstall install_cargo_nextest log_section "Running cargo nextest" cargo nextest run \ --manifest-path rust/Cargo.toml \ --workspace \ --all-features \ --locked \ --no-fail-fast } install_rust_toolchain case "$MODE" in style-clippy) run_style_clippy ;; test) run_tests ;; esac