name: Check Rust Code

on:
  push:
  pull_request:
  workflow_dispatch:

permissions:
  actions: write
  contents: read

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

defaults:
  run:
    shell: bash
    working-directory: src-tauri

jobs:
  cargo-test:
    name: Test codebase on ${{ matrix.os }} (cargo test)
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - windows-latest
          - ubuntu-latest
          - macos-latest
      fail-fast: false
    steps:
      - uses: actions/checkout@v3
      - uses: actions-rust-lang/setup-rust-toolchain@v1
      - uses: rui314/setup-mold@v1
      - uses: taiki-e/install-action@v1
        with:
          tool: cargo-hack,nextest
      - name: Install dependencies for Ubuntu
        if: matrix.os == 'ubuntu-latest'
        uses: awalsh128/cache-apt-pkgs-action@v1.4.3
        with:
          packages: libjavascriptcoregtk-4.1-dev libwebkit2gtk-4.1-dev build-essential curl wget file libxdo-dev libssl-dev libgtk-3-dev libayatana-appindicator3-dev librsvg2-dev gnome-video-effects gnome-video-effects-extra
          version: 1.0
      - name: Run unit & integration tests with nextest
        run: cargo hack --feature-powerset --exclude-features cli-build nextest run --no-tests=pass

  cargo-clippy:
    name: Check codebase quality (cargo clippy)
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - windows-latest
          - ubuntu-latest
          - macos-latest
      fail-fast: false
    steps:
      - uses: actions/checkout@v3
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          components: clippy
      - uses: taiki-e/install-action@cargo-hack
      - name: Install dependencies for Ubuntu
        if: matrix.os == 'ubuntu-latest'
        uses: awalsh128/cache-apt-pkgs-action@v1.4.3
        with:
          packages: libjavascriptcoregtk-4.1-dev libwebkit2gtk-4.1-dev build-essential curl wget libssl-dev libgtk-3-dev libayatana-appindicator3-dev librsvg2-dev gnome-video-effects gnome-video-effects-extra
          version: 1.0
      - name: Run all-features code quality checks
        run: cargo hack --feature-powerset --exclude-features cli-build --no-dev-deps clippy
      - name: Run normal code quality check
        run: cargo clippy

  cargo-fmt:
    name: Enforce codebase style (cargo fmt)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          components: rustfmt
      - run: cargo fmt --all -- --color=always --check
