# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

# workspace wide tests
name: rust

concurrency:
  group: ${{ github.repository }}-${{ github.head_ref || github.sha }}-${{ github.workflow }}
  cancel-in-progress: true

# trigger for all PRs and changes to main
on:
  push:
    branches:
      - main
  pull_request:

jobs:
  # Check workspace wide compile and test with default features for
  # mac
  macos:
    name: Test on Mac
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0  # v7.0.0
        with:
          submodules: true
      - name: Install protoc with brew
        run: brew install protobuf
      - name: Setup Rust toolchain
        run: |
          rustup toolchain install stable --no-self-update
          rustup default stable
      - name: Run tests
        shell: bash
        run: |
          # do not produce debug symbols to keep memory usage down
          export RUSTFLAGS="-C debuginfo=0"
          # PyArrow tests happen in integration.yml.
          cargo test --workspace

  # Check workspace wide compile and test with default features for
  # windows
  windows:
    name: Test on Windows
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0  # v7.0.0
        with:
          submodules: true
      - name: Install protobuf compiler in /d/protoc
        shell: bash
        run: |
          mkdir /d/protoc
          cd /d/protoc
          curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v21.4/protoc-21.4-win64.zip
          unzip protoc-21.4-win64.zip
          export PATH=$PATH:/d/protoc/bin
          protoc --version

      - name: Setup Rust toolchain
        run: |
          rustup toolchain install stable --no-self-update
          rustup default stable
      - name: Run tests
        shell: bash
        run: |
          # do not produce debug symbols to keep memory usage down
          export RUSTFLAGS="-C debuginfo=0"
          export PATH=$PATH:/d/protoc/bin
          cargo test --workspace

  # Run cargo fmt for all crates
  lint:
    name: Lint (cargo fmt)
    runs-on: ubuntu-latest
    container:
      image: amd64/rust
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0  # v7.0.0
      - name: Setup Rust toolchain
        uses: ./.github/actions/setup-builder
      - name: Setup rustfmt
        run: rustup component add rustfmt
      - name: Format arrow
        run: cargo fmt --all -- --check
      - name: Format parquet
        # Many modules in parquet are skipped, so check parquet separately
        # https://github.com/apache/arrow-rs/issues/6179
        working-directory: parquet
        run: |
          # if this fails, run this from the parquet directory:
          # cargo fmt -p parquet -- --config skip_children=true `find . -name "*.rs" \! -name format.rs`
          cargo fmt -p parquet -- --check --config skip_children=true `find . -name "*.rs" \! -name format.rs`

  msrv:
    name: Verify MSRV (Minimum Supported Rust Version)
    runs-on: ubuntu-latest
    container:
      image: amd64/rust
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0  # v7.0.0
      - name: Setup Rust toolchain
        uses: ./.github/actions/setup-builder
      - name: Install cargo-msrv (if needed)
        # cargo-msrv binary may be cached by the cargo cache step in setup-builder, and cargo install will error if it is already installed
        # --locked uses cargo-msrv's pinned Cargo.lock; without it an unpinned transitive
        # dep (aws-runtime) resolves to a version that fails to compile (E0282)
        run: if which cargo-msrv ; then echo "using existing cargo-msrv binary" ; else cargo install cargo-msrv --locked ; fi
      - name: Check all packages
        run: |
          # run `cargo msrv verify --manifest-path "path/to/Cargo.toml"` to see problematic dependencies
          find . -mindepth 2 -name Cargo.toml | while read -r dir
          do
            echo "Checking package '$dir'"
            cargo msrv verify --manifest-path "$dir" --output-format=json || exit 1
          done
