name: Rust CI

on:
  push:
    branches:
      - main
    paths:
      - interfaces/*/src/**
      - interfaces/*/examples/**
      - interfaces/*/Cargo.toml
      - models/*/src/**
      - models/*/examples/**
      - models/*/Cargo.toml
      - floneum/*/src/**
      - floneum/*/examples/**
      - floneum/*/Cargo.toml
      - fusor-ml/*/src/**
      - fusor-ml/*/tests/**
      - fusor-ml/*/examples/**
      - fusor-ml/*/Cargo.toml
      - src/**
      - .github/**
      - Cargo.toml

  pull_request:
    types: [opened, synchronize, reopened, ready_for_review]
    branches:
      - main
    paths:
      - interfaces/*/src/**
      - interfaces/*/examples/**
      - interfaces/*/Cargo.toml
      - models/*/src/**
      - models/*/examples/**
      - models/*/Cargo.toml
      - floneum/*/src/**
      - floneum/*/examples/**
      - floneum/*/Cargo.toml
      - fusor-ml/*/src/**
      - fusor-ml/*/tests/**
      - fusor-ml/*/examples/**
      - fusor-ml/*/Cargo.toml
      - src/**
      - .github/**
      - Cargo.toml

# workflow_dispatch:
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  check:
    if: github.event.pull_request.draft == false
    name: Check
    runs-on: ubuntu-24.04
    steps:
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: sudo apt-get update
      - run: sudo apt install libwebkit2gtk-4.1-dev libgtk-3-dev libasound2-dev libayatana-appindicator3-dev libxdo-dev
      - uses: actions/checkout@v6
      - name: Check workspace
        run: cargo check --all --examples --tests --features kalosm/language,kalosm/sound,kalosm/vision,kalosm/remote,kalosm/scrape

  test:
    if: github.event.pull_request.draft == false
    name: Test Suite
    runs-on: ubuntu-24.04
    steps:
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: sudo apt-get update
      - run: sudo apt install libwebkit2gtk-4.1-dev libgtk-3-dev libasound2-dev libayatana-appindicator3-dev libxdo-dev
      - uses: actions/checkout@v6
      - uses: humbletim/setup-vulkan-sdk@v1.2.1
        with:
          vulkan-query-version: 1.4.328.1
      - name: Run tests
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
          GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
          FUSOR_CONFORMANCE_REQUIRE_GPU: "1"
        run: cargo test --lib --bins --tests --examples --workspace --features kalosm/language,kalosm/sound,kalosm/vision,kalosm/remote,kalosm/scrape --exclude fusor-core --exclude rbert --exclude rwhisper --exclude kalosm-learning

  doc:
    if: github.event.pull_request.draft == false
    name: Docs
    runs-on: ubuntu-24.04
    steps:
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: sudo apt-get update
      - run: sudo apt install libwebkit2gtk-4.1-dev libgtk-3-dev libasound2-dev libayatana-appindicator3-dev libxdo-dev
      - uses: actions/checkout@v6
      - run: cargo test --doc --workspace --features kalosm/language,kalosm/sound,kalosm/vision,kalosm/remote,kalosm/scrape

  fmt:
    if: github.event.pull_request.draft == false
    name: Rustfmt
    runs-on: ubuntu-24.04
    steps:
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: rustup component add rustfmt
      - uses: actions/checkout@v6
      - run: cargo fmt --all -- --check

  clippy:
    if: github.event.pull_request.draft == false
    name: Clippy
    runs-on: ubuntu-24.04
    steps:
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: sudo apt-get update
      - run: sudo apt install libwebkit2gtk-4.1-dev libgtk-3-dev libasound2-dev libayatana-appindicator3-dev libxdo-dev
      - run: rustup component add clippy
      - uses: actions/checkout@v6
      - run: cargo clippy --workspace --examples --tests --features kalosm/language,kalosm/sound,kalosm/vision,kalosm/remote,kalosm/scrape -- -D warnings