on:
  workflow_dispatch:
  merge_group:
  push:
    branches:
      - main
  pull_request:

env:
    CARGO_TERM_COLOR: always

name: Continuous integration

jobs:
  check-version:
    runs-on: ubuntu-latest
    outputs:
      version_changed: ${{ steps.version-check.outputs.version-changed }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Check if version changed
        id: version-check
        uses: joaommartins/cargo-version-check-action@v1
        with:
          base-ref: ${{ github.base_ref }}

  clippy-fmt-and-test:
    runs-on: ubuntu-latest
    needs:
      - check-version
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@nightly
        with:
          components: clippy, rustfmt

      - name: Cache cargo registry and build
        uses: Swatinem/rust-cache@v2

      - name: Install cargo-nextest
        uses: taiki-e/install-action@v2
        with:
          tool: nextest

      - name: Run rustfmt check
        run: cargo fmt --all -- --check

      - name: Run clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

      - name: Run tests (nextest)
        run: cargo nextest run

  build-release-binary:
    needs:
      - check-version
      - clippy-fmt-and-test
    if: needs.check-version.outputs.version_changed == 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Cache cargo registry and build
        uses: Swatinem/rust-cache@v2

      - name: Build release binary
        run: cargo build --release --locked

      - name: Verify binary exists
        run: |
          ls -lh target/release/imposer
          ./target/release/imposer --version
