name: Publish to crates.io

on:
  push:
    tags:
      - "v*.*.*"

jobs:
  publish:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v5
        with:
          fetch-depth: 0

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

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2
        with:
          # Only save in the main branch.
          save-if: ${{ github.ref == 'refs/heads/main' }}

          # Determines which crates are cached.
          # If `true` all crates will be cached, otherwise only dependent crates will be cached.
          # Useful if additional crates are used for CI tooling.
          # default: "false"
          cache-all-crates: true

          # Determines whether workspace `target` directories are cached.
          # If `false`, only the cargo registry will be cached.
          # default: "true"
          cache-targets: true

      - name: Build
        run: cargo build

      - name: Check & Test
        run: cargo check && cargo test

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