name: Publish to crates.io

on:
  push:
    tags:
      - 'v*'

jobs:
  publish:
    name: Publish
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable

      - name: Verify tag matches Cargo.toml version
        run: |
          tag="${GITHUB_REF#refs/tags/v}"
          ver=$(grep -E '^version *= *"' Cargo.toml | head -1 | cut -d'"' -f2)
          if [ "$tag" != "$ver" ]; then
            echo "::error::Tag v$tag does not match Cargo.toml version $ver"
            exit 1
          fi
          echo "Publishing $ver (from tag v$tag)"

      - name: Publish to crates.io
        run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
