name: "Turbo Rust Setup"
description: "Sets up the Rust toolchain for CI"
inputs:
  targets:
    description: "Comma-separated list of target triples to install for this toolchain"
    required: false
  github-token:
    description: "GitHub token. You can pass secrets.GITHUB_TOKEN"
    required: true
  shared-cache-key:
    description: "A cache key that is used instead of the automatic `job`-based key, and is stable over multiple jobs."
    required: false
  cache-key:
    description: "An additional cache key that is added alongside the automatic `job`-based cache key and can be used to further differentiate jobs."
    required: false
  save-cache:
    description: "Determiners whether the cache should be saved. If `false`, the cache is only restored."
    required: false
    default: "false"

runs:
  using: "composite"
  steps:
    - name: "Setup Rust toolchain"
      uses: actions-rust-lang/setup-rust-toolchain@v1
      with:
        target: ${{ inputs.targets }}
        # needed to not make it override the defaults
        rustflags: ""
        # we want more specific settings
        cache: false

    - name: "Install LLD (LLVM Linker) for Linux"
      if: runner.os == 'Linux'
      shell: bash
      run: sudo apt-get -y update && sudo apt-get install -y lld

    - name: Set Up Protoc
      id: set-up-protoc
      continue-on-error: true
      uses: arduino/setup-protoc@v3
      with:
        version: "26.x"
        repo-token: ${{ inputs.github-token }}

    - name: Set Up Protoc (second try)
      if: steps.set-up-protoc.outcome == 'failure'
      uses: arduino/setup-protoc@v3
      with:
        version: "26.x"
        repo-token: ${{ inputs.github-token }}

    - name: "Add cargo problem matchers"
      shell: bash
      run: echo "::add-matcher::${{ github.action_path }}/matchers.json"

    - uses: rui314/setup-mold@v1

    - name: "Setup Rust Cache"
      uses: Swatinem/rust-cache@v2
      with:
        shared-key: ${{ inputs.shared-cache-key }}
        key: ${{ inputs.cache-key }}
        # the cache is huge and we only get 10gb max, so we only save on master
        save-if: ${{ github.ref == 'refs/heads/main' && inputs.save-cache || 'false' }}

    - name: "Install cargo-sweep"
      uses: taiki-e/install-action@v2
      env:
        GITHUB_TOKEN: ${{ inputs.github-token }}
      with:
        tool: cargo-sweep@0.6.2,cargo-groups@0.1.9

    - name: "Run cargo-sweep"
      uses: ./.github/actions/cargo-sweep
