name: Publish to crates.io

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      version:
        description: "Version to publish (must match Cargo.toml)"
        required: true
      fast_mode:
        description: "Skip clippy/fmt — run cargo test only (patch/doc-only releases)"
        required: false
        default: "false"
        type: choice
        options: ["false", "true"]

jobs:
  test:
    name: Tests (pre-publish gate)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy
      - name: Format check
        if: inputs.fast_mode != 'true'
        run: cargo fmt --check
      - name: Clippy
        if: inputs.fast_mode != 'true'
        run: cargo clippy -- -D warnings
      - run: cargo test -- --test-threads=1
        env:
          RUST_BACKTRACE: "1"

  publish:
    name: Publish to crates.io
    needs: test
    runs-on: ubuntu-latest
    environment: crates
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - name: Publish
        run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
