name: Rust tests

on:
  workflow_call:
    inputs:
      compiler-ref:
        description: >
          Git revision of the LF repo to use. Provided only if this workflow
          is being run from the reactor-rs repository.
        required: false
        type: string
      runtime-ref:
        description: >
          Git revision of the reactor runtime repo to use. Provided
          only if this workflow is being run from the reactor-rs repository.
        required: false
        type: string
      all-platforms:
        required: false
        default: true
        type: boolean

jobs:
  regular-tests:
    strategy:
      matrix:
        platform: ${{ (inputs.all-platforms && fromJSON('["ubuntu-24.04", "macos-latest", "windows-latest"]')) || fromJSON('["ubuntu-24.04"]') }}
        rust: [stable]
    runs-on: ${{ matrix.platform }}
    timeout-minutes: 60
    steps:
      - name: Check out lingua-franca repository
        uses: actions/checkout@v3
        with:
          repository: lf-lang/lingua-franca
          submodules: true
          ref: ${{ inputs.compiler-ref }}
          fetch-depth: 0
      - name: Prepare build environment
        uses: ./.github/actions/prepare-build-env
      - name: Check out specific ref of reactor-rs
        uses: actions/checkout@v3
        with:
          repository: lf-lang/reactor-rs
          path: core/src/main/resources/lib/rs/reactor-rs
          ref: ${{ inputs.runtime-ref }}
        if: inputs.runtime-ref
      - name: Update runtime ref if we use a custom runtime version
        run: ./gradlew updateRustRuntime
        if: inputs.runtime-ref
      - name: Setup Rust
        id: rustup
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: ${{ matrix.rust }}
          components: clippy
          rustflags: "" # don't use -D warnings
        continue-on-error: true
      - name: Delete rustup cache
        run: rm -r ~/.rustup
        if: ${{ steps.rustup.outcome }} != "success"
      - name: Setup Rust (again)
        if: ${{ steps.rustup.outcome }} != "success"
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: ${{ matrix.rust }}
          components: clippy
          rustflags: "" # don't use -D warnings
      - name: Run Rust tests
        run: ./gradlew targetTest -Ptarget=Rust
      - name: Report to CodeCov
        uses: ./.github/actions/report-code-coverage
        with:
          files: core/build/reports/jacoco/integrationTestCodeCoverageReport/integrationTestCodeCoverageReport.xml
        if: ${{ github.repository == 'lf-lang/lingua-franca' }}
