name: Rust

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

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        toolchain:
          - "stable"
          - "1.88" # MSRV

    steps:
    - uses: actions/checkout@v3
      with:
        submodules: recursive
    - name: Install Rust ${{ matrix.toolchain }}
      run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
    - name: Build
      run: cargo build --verbose --all-targets
    - if: ${{ matrix.toolchain == 'stable' }}
      name: Format
      run: cargo fmt --all -- --check
    - name: Unit Tests
      run: cargo test --verbose
    - if: ${{ matrix.toolchain == 'stable' }}
      name: Coverage Report
      run: |
        rustup component add llvm-tools-preview
        PATH=$(rustup show home | xargs -I '{}' find {} -name 'llvm-profdata' | xargs -I '{}' dirname {}):$PATH
        RUSTFLAGS="-C instrument-coverage" cargo test --lib
        find . -name '*.profraw' > profraw-files.txt
        llvm-profdata merge -sparse -f profraw-files.txt -o merged.profdata
        llvm-cov report \
          $( \
            for file in \
              $( \
                RUSTFLAGS="-C instrument-coverage" \
                  cargo test --lib --no-run --message-format=json \
                    | jq -r "select(.profile.test == true) | .filenames[]" \
                    | grep -v dSYM - \
              ); \
            do \
              printf "%s %s " -object $file; \
            done \
          ) \
        --instr-profile=merged.profdata \
        --summary-only \
        --use-color \
        --ignore-filename-regex='/.cargo/registry' \
        --ignore-filename-regex='rustc/' \
        --ignore-filename-regex='src/tests/'

