name: CI

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

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  test:
    name: Test (${{ matrix.os }}, ${{ matrix.rust }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        # macos-latest (macOS 15). Previously pinned to macos-14
        # because the macos-15 image shipped a broken `rustup` (the
        # `rustup-init` stub intercepted `cargo clippy` and exited 1);
        # verified fixed upstream and un-pinned (#274).
        os: [ubuntu-latest, macos-latest, windows-latest]
        rust: [stable, beta]
        exclude:
          - os: macos-latest
            rust: beta
          - os: windows-latest
            rust: beta
    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.rust }}
          components: rustfmt, clippy

      - uses: swatinem/rust-cache@v2
        with:
          shared-key: "${{ matrix.os }}-${{ matrix.rust }}"

      - name: Check formatting
        if: matrix.rust == 'stable' && matrix.os == 'ubuntu-latest'
        run: cargo fmt --all -- --check

      - name: Clippy
        if: matrix.rust == 'stable'
        run: cargo clippy --all-targets --all-features -- -D warnings

      - name: Build
        run: cargo build --all-features

      - name: Unit tests
        run: cargo test --lib --all-features

      - name: Mechanical CLI tests
        run: cargo test --test cli --all-features

      - name: Doc tests
        run: cargo test --doc --all-features

  docs:
    name: Documentation
    runs-on: ubuntu-latest
    env:
      RUSTDOCFLAGS: -D warnings
    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@stable

      - uses: swatinem/rust-cache@v2
        with:
          shared-key: "docs"

      - name: Build docs
        run: cargo doc --no-deps --all-features
