name: CI

on:
  pull_request:
    branches: [main]

env:
  CARGO_TERM_COLOR: always

jobs:
  version-check:
    name: Version bump check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Check version was bumped
        run: |
          PR_VERSION=$(grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
          MAIN_VERSION=$(git show origin/main:Cargo.toml | grep '^version' | head -1 | sed 's/.*"\(.*\)".*/\1/')
          echo "PR version:   $PR_VERSION"
          echo "main version: $MAIN_VERSION"
          if [ "$PR_VERSION" = "$MAIN_VERSION" ]; then
            echo "::error::Version in Cargo.toml ($PR_VERSION) has not been bumped from main. Update the version before merging."
            exit 1
          fi

  miri:
    name: Miri
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - name: Install Miri
        run: |
          rustup toolchain install nightly --component miri
          rustup override set nightly
          cargo miri setup

      - name: Cache cargo registry and build
        uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-miri-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-miri-

      - name: Test with Miri
        env:
          MIRIFLAGS: "-Zmiri-disable-isolation -Zmiri-permissive-provenance -Zmiri-ignore-leaks"
        run: cargo miri test

  check:
    name: Format, Lint, Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - name: Install Rust
        run: |
          rustup toolchain install stable --profile default --component clippy
          rustup default stable

      - name: Cache cargo registry and build
        uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-

      - uses: extractions/setup-just@v4

      - name: Run checks
        run: just check
