name: Release

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  publish-crate:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v6
    
    - name: Install Rust
      uses: dtolnay/rust-toolchain@stable
    
    - name: Cache dependencies
      uses: actions/cache@v5
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
    
    - name: Run tests before publish
      run: cargo test --all-features
    
    - name: Install cargo-semver-checks
      run: cargo install cargo-semver-checks --locked
    
    - name: Check semver compatibility
      run: cargo semver-checks check-release
    
    - name: Publish to crates.io
      run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
