name: Release crate

on:
  workflow_dispatch:
    inputs:
      version:
        description: "Expected Cargo.toml version"
        required: false

concurrency: release-crate

permissions:
  contents: write

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Verify requested version
        id: crate
        env:
          EXPECTED_VERSION: ${{ github.event.inputs.version }}
        run: |
          actual="$(
            cargo metadata --no-deps --format-version 1 \
              | python3 -c 'import json,sys; print(json.load(sys.stdin)["packages"][0]["version"])'
          )"
          if [ -n "${EXPECTED_VERSION}" ] && [ "${EXPECTED_VERSION}" != "${actual}" ]; then
            echo "Expected version ${EXPECTED_VERSION}, but Cargo.toml declares ${actual}." >&2
            exit 1
          fi
          echo "version=${actual}" >> "${GITHUB_OUTPUT}"
      - name: Publish crate
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish --locked
      - name: Push release tag
        env:
          version: ${{ steps.crate.outputs.version }}
        run: |
          git tag "release/${version}"
          git push origin "release/${version}"
