name: Setup Rust with Optimized Caching
description: Install Rust toolchain with combined cargo cache for faster builds

inputs:
  toolchain:
    description: 'Rust toolchain (stable, beta, nightly, or version)'
    required: false
    default: 'stable'
  components:
    description: 'Rust components to install (comma-separated)'
    required: false
    default: ''
  cache-key-suffix:
    description: 'Additional cache key suffix'
    required: false
    default: ''

runs:
  using: composite
  steps:
    - name: Install Rust ${{ inputs.toolchain }}
      uses: dtolnay/rust-toolchain@stable
      with:
        toolchain: ${{ inputs.toolchain }}
        components: ${{ inputs.components }}

    - name: Cache Rust dependencies
      uses: actions/cache@v4
      with:
        path: |
          ~/.cargo/registry/index/
          ~/.cargo/registry/cache/
          ~/.cargo/git/db/
          target/
        key: ${{ runner.os }}-cargo-${{ inputs.toolchain }}-${{ inputs.cache-key-suffix }}-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: |
          ${{ runner.os }}-cargo-${{ inputs.toolchain }}-${{ inputs.cache-key-suffix }}-
          ${{ runner.os }}-cargo-${{ inputs.toolchain }}-
          ${{ runner.os }}-cargo-
