#!/usr/bin/env sh
set -eu

log="$(mktemp)"
trap 'rm -f "$log"' EXIT HUP INT TERM

run() {
  name="$1"
  shift

  if "$@" >"$log" 2>&1; then
    printf '✓ %s..\n' "$name"
    : >"$log"
    return 0
  fi

  printf 'x %s..\n' "$name"
  printf '%s\n' "check failed: $name" >&2
  cat "$log" >&2
  exit 1
}

run "format" cargo fmt --all --check
run "clippy" cargo clippy --workspace --all-features --lib --bins --tests -- -D warnings
run "build" cargo build --workspace --all-features --lib --bins --tests
run "test" env RUSTFLAGS="-D warnings" cargo test --workspace --all-features --lib --bins --tests
run "docs" env RUSTDOCFLAGS="-D warnings" cargo doc --workspace --all-features --no-deps --document-private-items
