name: CI

on:
  pull_request:
  push:
    branches:
      - main

env:
  RUSTFLAGS: -Dwarnings

jobs:
  test:
    name: Build and test
    runs-on: ubuntu-latest
    strategy:
      matrix:
        rust: [nightly, stable]

    steps:
    - uses: actions/checkout@master
    - name: Install ${{ matrix.rust }}
      uses: actions-rs/toolchain@v1
      with:
        toolchain: ${{ matrix.rust }}
        override: true

    - uses: Swatinem/rust-cache@v2.8.0
    - uses: taiki-e/cache-cargo-install-action@v2
      with:
        tool: cargo-expand
    - name: Tests
      run: cargo test
      env:
        RUST_LOG: trace

  verify_docs_are_current:
    name: Verify docs expansions are current
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - uses: actions-rs/toolchain@v1
      with:
        toolchain: nightly
        override: true
    - uses: taiki-e/cache-cargo-install-action@v2
      with:
        tool: cargo-expand
    - name: docs-gen --verify
      run: cargo run -p docs-gen -- --verify

  check_fmt_and_docs:
    name: Checking fmt, clippy, and docs
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        components: rustfmt, clippy
        override: true

    - uses: giraffate/clippy-action@v1
      with:
        reporter: 'github-pr-review'
        github_token: ${{ secrets.GITHUB_TOKEN }}
        
    - name: fmt
      run: cargo fmt --all -- --check

    - name: Build Documentation
      run: cargo doc --all --no-deps

  coverage:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
      - uses: taiki-e/cache-cargo-install-action@v2
        with:
          tool: cargo-expand
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview
      - uses: Swatinem/rust-cache@v2
      - name: Install latest nextest release
        uses: taiki-e/install-action@nextest
      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov
      - name: Collect coverage data
        run: cargo llvm-cov nextest --workspace --codecov --output-path codecov.json
        env:
          RUST_LOG: trace
      - name: Upload coverage reports to Codecov
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: codecov.json
          slug: jbr/fieldwork

