---
name: Rust

on:
  workflow_dispatch:
  push:
    branches: [ "main" ]
    paths:
      - 'Cargo.toml'
      - 'Cargo.lock'
      - 'src/**'
      - 'tests/**'
      - '.codespellrc'
      - '.github/workflows/rust.yml'
  pull_request:
    branches: [ "main" ]
    paths:
      - 'Cargo.toml'
      - 'Cargo.lock'
      - 'src/**'
      - 'tests/**'
      - '.codespellrc'
      - '.github/workflows/rust.yml'

env:
  CARGO_TERM_COLOR: always

jobs:
  codespell:
    name: Check for spelling mistakes
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Codespell
        uses: codespell-project/actions-codespell@v2

  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Check format
        run: cargo fmt --check
      - name: Clippy
        run: cargo clippy --all-features

  docs:
    name: Build documentation
    needs:
      - codespell
      - lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Build documentation
        run: cargo doc --all-features

  build-linux:
    name: Build on Linux
    needs:
      - lint
    runs-on: ubuntu-latest
    strategy:
      matrix:
        features: [ [ ], [ "--all-features" ] ]
    steps:
      - uses: actions/checkout@v4
      - name: Clippy
        run: cargo clippy ${{ join(matrix.features, ' ') }}
      - name: Build
        run: cargo build --verbose ${{ join(matrix.features, ' ') }}
      - name: Run doctests
        run: cargo test --doc --verbose ${{ join(matrix.features, ' ') }}
      - name: Run regular tests
        run: cargo test --tests --verbose ${{ join(matrix.features, ' ') }}

  codecov:
    name: Code Coverage
    needs:
      - lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview
      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov
      - name: Install nextest
        uses: taiki-e/install-action@nextest
      - name: Generate code coverage
        run: cargo llvm-cov nextest --all-features --lcov --output-path lcov.info
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v4.0.1
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: lcov.info
          fail_ci_if_error: true

  build-others:
    name: Build on ${{ matrix.os }}
    needs:
      - build-linux
      - codecov
      - docs
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ windows-latest, macos-latest ]
        features: [ [ ], [ "--all-features" ] ]
    steps:
      - uses: actions/checkout@v4
      - name: Build
        run: cargo build --verbose ${{ join(matrix.features, ' ') }}
      - name: Run tests
        run: cargo test --tests --verbose ${{ join(matrix.features, ' ') }}
      - name: Run doctests
        run: cargo test --doc --verbose ${{ join(matrix.features, ' ') }}
