name: Publish to crates.io

on:
  push:
    tags:
      - "v*"

jobs:
  publish:
    name: Publish to crates.io
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6.0.2

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

      - name: Cache Cargo registry and build artefacts
        uses: actions/cache@v5.0.4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-publish-${{ hashFiles('Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-publish-

      - name: cargo build
        run: cargo build --release

      - name: cargo test (library)
        run: cargo test --lib

      - name: cargo publish (dry run)
        run: cargo publish --dry-run

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