name: Build and Publish Rust Crate

on:
  push:
    tags:
      - "v*.*.*"

permissions:
  contents: write

jobs:
  build:
    runs-on: ubuntu-latest
    environment: crates-io
    steps:
      - name: Checkout repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6

      - name: Verify tag matches Cargo.toml version
        run: |
          CARGO_VERSION="v$(grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)"/\1/')"
          TAG_VERSION="${GITHUB_REF#refs/tags/}"
          if [ "$CARGO_VERSION" != "$TAG_VERSION" ]; then
            echo "::error::Tag $TAG_VERSION does not match Cargo.toml version $CARGO_VERSION"
            exit 1
          fi

      - name: Install Linux dependencies
        uses: ./.github/actions/install-linux-deps

      - name: Set up Rust toolchain
        uses: dtolnay/rust-toolchain@3c5f7ea28cd621ae0bf5283f0e981fb97b8a7af9 # master
        with:
          toolchain: stable

      - name: Fetch dependencies
        run: cargo +stable fetch --locked

      - name: Build
        run: cargo +stable build --verbose --locked --offline

      - name: Run tests
        run: cargo +stable test --verbose --locked --offline

      - name: Publish Crate
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish --locked

      - name: Create Release
        uses: softprops/action-gh-release@b4309332981a82ec1c5618f44dd2e27cc8bfbfda # v2
        with:
          tag_name: ${{ github.ref_name }}
          name: Release ${{ github.ref_name }}
          generate_release_notes: true
