name: CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

env:
  CARGO_TERM_COLOR: always

jobs:
  format:
    name: Format
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      # cache-bin: false — never cache ~/.cargo/bin. Restoring it clobbers the fresh
      # toolchain dtolnay/rust-toolchain just installed; a stale archive once left `cargo`
      # acting as `rustup-init` on macOS. prefix-key bump abandons those poisoned v0 caches.
      - uses: Swatinem/rust-cache@v2
        with:
          prefix-key: "v1"
          cache-bin: false
      - name: Check formatting
        run: cargo fmt --all -- --check

  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - uses: Swatinem/rust-cache@v2
        with:
          prefix-key: "v1"
          cache-bin: false
      - name: Run clippy
        run: cargo clippy --all-targets -- -D warnings

  test:
    name: Test (${{ matrix.os }})
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: ${{ matrix.os == 'ubuntu-latest' && 'llvm-tools-preview' || '' }}
      - uses: Swatinem/rust-cache@v2
        with:
          prefix-key: "v1"
          cache-bin: false
      - if: matrix.os == 'ubuntu-latest'
        uses: taiki-e/install-action@cargo-llvm-cov
      - name: Run tests
        if: matrix.os != 'ubuntu-latest'
        run: cargo test --all-targets
      - name: Run tests with coverage
        if: matrix.os == 'ubuntu-latest'
        run: cargo llvm-cov --package splashboard --lcov --output-path lcov.info
      - name: Upload to Codecov
        if: matrix.os == 'ubuntu-latest'
        uses: codecov/codecov-action@v7
        with:
          files: lcov.info
          fail_ci_if_error: false
          token: ${{ secrets.CODECOV_TOKEN }}
