name: Rust

concurrency:
  group: check-rust-${{ github.ref }}
  cancel-in-progress: true

on:
  push:
    branches: [main]
  pull_request:

env:
  SCCACHE_GHA_ENABLED: "true"
  RUSTC_WRAPPER: "sccache"
  OPENSSL_NO_VENDOR: "1"

jobs:
  cargo-check:
    name: cargo check
    runs-on: ubuntu-22.04-8core
    strategy:
      matrix:
        include:
          - name: default
            flags: ""
          - name: all-features
            flags: "--all-features"
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y build-essential libudev-dev clang llvm-dev libclang-dev pkg-config libssl-dev
      - name: Run sccache-cache
        uses: mozilla-actions/sccache-action@v0.0.9
      - name: cargo check (${{ matrix.name }})
        run: cargo check --workspace ${{ matrix.flags }}

  cargo-test:
    name: cargo test
    runs-on: ubuntu-22.04-8core
    strategy:
      matrix:
        include:
          - name: default
            flags: ""
          - name: all-features
            flags: "--all-features"
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y build-essential libudev-dev clang llvm-dev libclang-dev pkg-config libssl-dev
      - name: Run sccache-cache
        uses: mozilla-actions/sccache-action@v0.0.9
      - name: cargo test (${{ matrix.name }})
        run: cargo test --workspace ${{ matrix.flags }}

  cargo-fmt:
    name: cargo fmt
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y build-essential libudev-dev clang llvm-dev libclang-dev pkg-config libssl-dev
      - name: Run sccache-cache
        uses: mozilla-actions/sccache-action@v0.0.9
      - name: Install cargo-fmt
        run: rustup component add rustfmt
      - name: cargo fmt
        run: cargo fmt -- --check

  cargo-doc:
    name: cargo doc
    runs-on: ubuntu-22.04-8core
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y build-essential libudev-dev clang llvm-dev libclang-dev pkg-config libssl-dev
      - name: Run sccache-cache
        uses: mozilla-actions/sccache-action@v0.0.9
      - name: cargo doc
        run: cargo doc

  cargo-clippy:
    name: cargo clippy
    runs-on: ubuntu-22.04-8core
    strategy:
      matrix:
        include:
          - name: default
            flags: ""
          - name: all-features
            flags: "--all-features"
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y build-essential libudev-dev clang llvm-dev libclang-dev pkg-config libssl-dev
      - name: Run sccache-cache
        uses: mozilla-actions/sccache-action@v0.0.9
      - name: Install cargo-clippy
        run: rustup component add clippy
      - name: cargo clippy (${{ matrix.name }})
        run: cargo clippy --workspace ${{ matrix.flags }}

  cargo-fix:
    name: cargo fix
    runs-on: ubuntu-22.04-8core
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y build-essential libudev-dev clang llvm-dev libclang-dev pkg-config libssl-dev
      - name: Run sccache-cache
        uses: mozilla-actions/sccache-action@v0.0.9
      - name: cargo fix --workspace
        run: |
          # Run cargo fix on the project
          cargo fix --workspace

          # Check for local git changes
          if ! git diff --exit-code; then
              echo "There are local changes after running 'cargo fix --workspace' ❌"
              exit 1
          else
              echo "No changes detected after running 'cargo fix --workspace' ✅"
          fi
