# This file is autogenerated by maturin v1.9.6
# To update, run
#
#    maturin generate-ci github
#
name: CI

on:
  push:
    branches:
      - main
    tags:
      - '*'
  pull_request:
  workflow_dispatch:

permissions:
  contents: read

jobs:
  linux:
    needs: [cargo-test, python-test]
    runs-on: ${{ matrix.platform.runner }}
    strategy:
      matrix:
        platform:
          - runner: ubuntu-latest
            target: x86_64
          - runner: ubuntu-latest
            target: x86
          - runner: ubuntu-latest
            target: aarch64
          - runner: ubuntu-latest
            target: armv7
          - runner: ubuntu-latest
            target: s390x
          - runner: ubuntu-latest
            target: ppc64le
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-python@v6
        with:
          python-version: 3.x
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.platform.target }}
          args: --release --out dist --interpreter python3.9 python3.10 python3.11 python3.12 python3.13 python3.14
          sccache: ${{ !startsWith(github.ref, 'refs/tags/') }}
          manylinux: auto
      - name: Upload wheels
        uses: actions/upload-artifact@v6
        with:
          name: wheels-linux-${{ matrix.platform.target }}
          path: dist

  musllinux:
    needs: [cargo-test, python-test]
    runs-on: ${{ matrix.platform.runner }}
    strategy:
      matrix:
        platform:
          - runner: ubuntu-latest
            target: x86_64
          - runner: ubuntu-latest
            target: x86
          - runner: ubuntu-latest
            target: aarch64
          - runner: ubuntu-latest
            target: armv7
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-python@v6
        with:
          python-version: 3.x
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.platform.target }}
          args: --release --out dist --interpreter python3.9 python3.10 python3.11 python3.12 python3.13 python3.14 python3.13t python3.14t
          sccache: ${{ !startsWith(github.ref, 'refs/tags/') }}
          manylinux: musllinux_1_2
      - name: Upload wheels
        uses: actions/upload-artifact@v6
        with:
          name: wheels-musllinux-${{ matrix.platform.target }}
          path: dist

  windows:
    needs: [cargo-test, python-test]
    runs-on: ${{ matrix.platform.runner }}
    strategy:
      matrix:
        platform:
          - runner: windows-latest
            target: x64
          - runner: windows-latest
            target: x86
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-python@v6
        with:
          python-version: 3.x
          architecture: ${{ matrix.platform.target }}
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.platform.target }}
          args: --release --out dist --find-interpreter
          sccache: ${{ !startsWith(github.ref, 'refs/tags/') }}
      - name: Upload wheels
        uses: actions/upload-artifact@v6
        with:
          name: wheels-windows-${{ matrix.platform.target }}
          path: dist

  macos:
    needs: [cargo-test, python-test]
    runs-on: ${{ matrix.platform.runner }}
    strategy:
      matrix:
        platform:
          - runner: macos-latest
            target: x86_64
          - runner: macos-latest
            target: aarch64
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-python@v6
        with:
          python-version: 3.x
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.platform.target }}
          args: --release --out dist --find-interpreter
          sccache: ${{ !startsWith(github.ref, 'refs/tags/') }}
      - name: Upload wheels
        uses: actions/upload-artifact@v6
        with:
          name: wheels-macos-${{ matrix.platform.target }}
          path: dist

  sdist:
    needs: [cargo-test, python-test]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-python@v6
        with:
          python-version: 3.x
      - name: Build sdist
        uses: PyO3/maturin-action@v1
        with:
          command: sdist
          args: --out dist
      - name: Upload sdist
        uses: actions/upload-artifact@v6
        with:
          name: wheels-sdist
          path: dist

  cargo-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - name: Cargo test
        run: |
          cargo install cargo-llvm-cov --locked
          cargo test --all --release
          cargo llvm-cov \
            --workspace \
            --ignore-filename-regex 'src/lib\.rs' \
            --codecov \
            --output-path codecov.json
      - name: Format check
        run: cargo fmt --all -- --check
      - name: Clippy check
        run: cargo clippy --all-targets --all-features -- -D warnings
      - name: Upload coverage reports to Codecov
        uses: codecov/codecov-action@v5
        with:
          fail_ci_if_error: true
          token: ${{ secrets.CODECOV_TOKEN }}

  python-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-python@v6
        with:
          python-version: 3.x
      - name: Install packages
        run: python -m pip install -e .[test,dev]
      - name: Run tests
        run: pytest --benchmark-skip
      - name: Docs check
        run: |
          pdoc fm_index \
            --output-directory docs \
            --no-search \
            --no-show-source \
            --docformat markdown \
            --footer-text "© 2026 Koki Watanabe"
          if ! git diff --exit-code -- docs; then
            echo "::error::Docs are out of date. Please run pdoc and commit generated docs."
            exit 1
          fi
      - name: Format check
        run: ruff check

  python-benchmark:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python:
          - "3.9"
          - "3.10"
          - "3.11"
          - "3.12"
          - "3.13"
          - "3.14"
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-python@v6
        with:
          python-version: ${{ matrix.python }}
      - name: Install packages
        run: |
          python -m pip install --upgrade pip
          python -m pip install -e .[test]
      - name: Run benchmarks
        run: pytest --benchmark-only

  # This project uses PyO3's string conversion features.
  # Which may depend on the decoding of a C bitfield.
  # https://pyo3.rs/main/doc/src/pyo3/types/string.rs#289
  wheels-test:
    runs-on: ubuntu-latest
    needs: linux
    strategy:
      fail-fast: false
      matrix:
        arch:
          - target: aarch64
          - target: armv7
          - target: s390x
          - target: ppc64le
    steps:
      - uses: actions/checkout@v6
      - uses: actions/download-artifact@v7
        with:
          name: wheels-linux-${{ matrix.arch.target }}
          path: dist
      - uses: docker/setup-qemu-action@v3
      - name: Run tests
        uses: uraimo/run-on-arch-action@v3
        with:
          arch: ${{ matrix.arch.target }}
          distro: ubuntu24.04
          dockerRunArgs: |
            -v ${{ github.workspace }}/dist:/dist
            -v ${{ github.workspace }}:/src
          install: |
            apt-get update
            apt-get install -y --no-install-recommends python3 python3-venv python3-pip python3-numpy ca-certificates
          run: |
            python3 -m venv --system-site-packages /venv
            /venv/bin/python -m pip install -U pip
            /venv/bin/python -m pip install --no-index --find-links /dist --only-binary=:all: fm-index
            /venv/bin/python -m pip install pytest pytest-benchmark
            cd /src
            /venv/bin/python -m pytest --benchmark-skip

  release:
    name: Release
    runs-on: ubuntu-latest
    if: ${{ startsWith(github.ref, 'refs/tags/') || github.event_name == 'workflow_dispatch' }}
    needs: [linux, musllinux, windows, macos, sdist, cargo-test, python-test, python-benchmark, wheels-test]
    permissions:
      # Use to sign the release artifacts
      id-token: write
      # Used to upload release artifacts
      contents: write
      # Used to generate artifact attestation
      attestations: write
    steps:
      - uses: actions/download-artifact@v7
      - name: Generate artifact attestation
        uses: actions/attest-build-provenance@v3
        with:
          subject-path: 'wheels-*/*'
      - name: Publish to PyPI
        if: ${{ startsWith(github.ref, 'refs/tags/') }}
        uses: PyO3/maturin-action@v1
        env:
          MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
        with:
          command: upload
          args: --non-interactive --skip-existing wheels-*/*
