name: Qdrant Edge Rust Release

on:
  workflow_dispatch:
    inputs:
      publish:
        description: 'Publish to crates.io'
        type: boolean
        default: false
        required: true

permissions:
  contents: read

jobs:
  edge-rust-check:
    name: Check Qdrant Edge Rust Package [${{ matrix.os }}]

    runs-on: ${{ matrix.os }}

    strategy:
      fail-fast: false
      matrix:
        os:
          - macos-latest
          - ubuntu-24.04-arm
          - ubuntu-latest
          - windows-11-arm
          - windows-latest

    steps:
      - name: Checkout Qdrant
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Install Rust
        uses: dtolnay/rust-toolchain@631a55b12751854ce901bb631d5902ceb48146f7 # stable

      - name: Install mold
        if: startsWith(matrix.os, 'ubuntu')
        uses: rui314/setup-mold@9c9c13bf4c3f1adef0cc596abc155580bcb04444 # v1

      - name: Install Just
        uses: ./.github/actions/setup-just

      - name: Install uv
        uses: astral-sh/setup-uv@11f9893b081a58869d3b5fccaea48c9e9e46f990 # v8.3.2

      - name: Install ast-grep
        run: uv tool install ast-grep-cli

      - name: Setup Qdrant (for testing examples)
        id: setup-qdrant
        # No Qdrant release for Windows ARM yet.
        # So, build only, but not test.
        if: matrix.os != 'windows-11-arm'
        uses: ./.github/actions/setup-qdrant

      - name: Restore Rust build cache
        uses: Swatinem/rust-cache@e18b497796c12c097a38f9edb9d0641fb99eee32 # v2
        with:
          shared-key: edge
          # edge-test (edge-test.yml) is the broadest build in this group and owns saving the cache
          save-if: "false"

      - name: Prepare data files (for testing examples)
        if: steps.setup-qdrant.conclusion == 'success'
        working-directory: lib/edge
        run: just prepare-data

      - name: Build examples
        working-directory: lib/edge
        run: just rs-examples-build

      - name: Run examples
        if: steps.setup-qdrant.conclusion == 'success'
        working-directory: lib/edge
        run: just rs-examples


  publish:
    name: Publish to crates.io

    runs-on: ubuntu-latest
    needs:
      - edge-rust-check

    if: inputs.publish

    steps:
      - name: Install Rust
        uses: dtolnay/rust-toolchain@631a55b12751854ce901bb631d5902ceb48146f7 # stable

      - name: Install uv
        uses: astral-sh/setup-uv@11f9893b081a58869d3b5fccaea48c9e9e46f990 # v8.3.2

      - name: Install ast-grep
        run: uv tool install ast-grep-cli

      - name: Checkout Qdrant
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Amalgamate
        run: uv run lib/edge/publish/amalgamate.py

      - name: Publish to crates.io
        working-directory: lib/edge/publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_TOKEN_EDGE }}
        run: cargo publish -p qdrant-edge --no-verify
