name: Publish to crates.io

on:
  release:
    types: [published]

concurrency:
  group: lasersell-crates-publish
  cancel-in-progress: false

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

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

      - name: Verify version matches tag
        run: |
          CARGO_VERSION=$(cargo metadata --no-deps --format-version 1 \
            | python3 -c "import sys,json; print(json.load(sys.stdin)['packages'][0]['version'])")
          TAG_VERSION="${GITHUB_REF_NAME#v}"
          if [ "${CARGO_VERSION}" != "${TAG_VERSION}" ]; then
            echo "::error::Cargo.toml version (${CARGO_VERSION}) does not match tag (${TAG_VERSION})"
            exit 1
          fi

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