name: Rust

on: [push, pull_request]

jobs:

  rustfmt-clippy:
    name: Format and Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install stable
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable
          components: clippy, rustfmt

      - name: Run rustfmt
        run: |
          cargo fmt --all -- --check

      - name: Run clippy
        uses: actions-rs-plus/clippy-check@v2.2.1
        with:
          args: --all -- -D warnings

  coverage:
    needs: arch-test
    name: Code coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable

      - name: Install grcov
        env:
          LINK: https://github.com/mozilla/grcov/releases/download
          GRCOV_VERSION: 0.8.7
        run: |
          curl -L "$LINK/v$GRCOV_VERSION/grcov-x86_64-unknown-linux-gnu.tar.bz2" |
          tar xj -C $HOME/.cargo/bin

      - name: Set up MinGW
        uses: egor-tensin/setup-mingw@v2
        with:
          platform: x64
          cc: false

      - name: Run grcov
        id: coverage
        run: bash coverage.sh

      - name: Codecov upload
        uses: codecov/codecov-action@v5
        with:
          files: coverage.lcov

  arch-test:
    needs: rustfmt-clippy
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-13, macos-latest]
        toolchain: [nightly, stable]
        include:
          - toolchain: nightly-gnu
            os: windows-latest
          - toolchain: stable-gnu
            os: windows-latest

    name: ${{matrix.os}}-${{matrix.toolchain}}
    runs-on: ${{matrix.os}}
    steps:
      - uses: actions/checkout@v4

      - name: Install ${{matrix.toolchain}}
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: ${{matrix.toolchain}}

      - name: Build
        run: |
          cargo build --verbose

      - name: Run tests
        run: |
          cargo test --verbose



