name: CI # Continuous Integration

on:
  push:
    branches:
      - main
  pull_request:

jobs:

  test:
    name: Test Suite (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      - name: Install Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
          # Defer caching to the dedicated `Swatinem/rust-cache@v2`
          # step below; this action's bundled cache is coarser-grained
          # and would just race the rust-cache one for the same paths.
          cache: false
      - uses: Swatinem/rust-cache@v2
        with:
          # Do not cache `~/.cargo/bin/` — its rustup proxy detection
          # is unreliable on macos-arm64 runners and can occasionally
          # restore a stale `rustup-init` binary on top of the freshly
          # installed `cargo`, which then fails with
          # `error: unexpected argument 'test' found / Usage: rustup-init`.
          cache-bin: false
      - name: Run tests
        if: matrix.os != 'ubuntu-latest'
        run: cargo test --all-features --workspace
      - name: Install cargo-llvm-cov
        if: matrix.os == 'ubuntu-latest'
        uses: taiki-e/install-action@cargo-llvm-cov
      - name: Generate code coverage
        if: matrix.os == 'ubuntu-latest'
        run: cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info --ignore-filename-regex 'main\.rs'
      - name: Upload coverage to Codecov
        if: matrix.os == 'ubuntu-latest'
        uses: codecov/codecov-action@v6
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: lcov.info
          fail_ci_if_error: true

  rustfmt:
    name: Rustfmt
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      - name: Install Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
          components: rustfmt
          cache: false
      - uses: Swatinem/rust-cache@v2
        with:
          cache-bin: false
      - name: Check formatting
        run: cargo fmt --all --check

  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      - name: Install Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
          components: clippy
          cache: false
      - uses: Swatinem/rust-cache@v2
        with:
          cache-bin: false
      - name: Clippy check
        run: cargo clippy --all-targets --all-features --workspace -- -D warnings

  docs:
    name: Docs
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      - name: Install Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
          cache: false
      - uses: Swatinem/rust-cache@v2
        with:
          cache-bin: false
      - name: Check documentation
        env:
          RUSTDOCFLAGS: -D warnings
        run: cargo doc --no-deps --document-private-items --all-features --workspace --examples
