name: Check Rust

on:
  push:
    branches-ignore:
      - gh-readonly-queue/**
  pull_request:
  merge_group:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/master' && !startsWith(github.ref, 'refs/tags/') }}

jobs:
  rustdoc:
    runs-on: ubuntu-latest
    env:
      CARGO_HTTP_MULTIPLEXING: false
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
    - name: Cache Rust dependencies
      uses: Swatinem/rust-cache@v2
      with:
          cache-bin: false # WORKAROUND(https://github.com/Swatinem/rust-cache/issues/341)
          workspaces: |
              src/mastersrv
              src/masterping
              ./
    - name: Run Rustdoc
      run: |
        RUSTDOCFLAGS=-Dwarnings DDNET_TEST_NO_LINK=1 cargo doc

  cxxbridge:
    runs-on: ubuntu-latest
    env:
      CARGO_HTTP_MULTIPLEXING: false
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
    - name: Cache Rust dependencies
      uses: Swatinem/rust-cache@v2
      with:
          cache-bin: false # WORKAROUND(https://github.com/Swatinem/rust-cache/issues/341)
          workspaces: |
              src/mastersrv
              src/masterping
              ./
    - name: Run cxxbridge
      run: |
        cargo install cxxbridge-cmd@"$(python scripts/generate_rust_bridge.py --cxx-version)"
        python scripts/generate_rust_bridge.py
        git diff --exit-code

  rustfmt:
    runs-on: ubuntu-latest
    env:
      CARGO_HTTP_MULTIPLEXING: false
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
    - name: Cache Rust dependencies
      uses: Swatinem/rust-cache@v2
      with:
          cache-bin: false # WORKAROUND(https://github.com/Swatinem/rust-cache/issues/341)
          workspaces: |
              src/mastersrv
              src/masterping
              ./
    - name: Run Rustfmt
      run:
        cargo fmt -- --check

  cargo-deny:
    runs-on: ubuntu-latest
    env:
      CARGO_HTTP_MULTIPLEXING: false
    strategy:
      matrix:
        checks:
          - bans licenses sources
    continue-on-error: ${{ matrix.checks == 'advisories' }}
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
    - name: Cache Rust dependencies
      uses: Swatinem/rust-cache@v2
      with:
          cache-bin: false # WORKAROUND(https://github.com/Swatinem/rust-cache/issues/341)
          workspaces:  |
              src/mastersrv
              src/masterping
              ./
    - uses: EmbarkStudios/cargo-deny-action@76cd80eb775d7bbbd2d80292136d74d39e1b4918 # v2.0.14
      with:
        command: check ${{ matrix.checks }}
