name: Rust

on:
  workflow_dispatch:
  schedule:
    # Everyday at 5:00am
    - cron: '0 5 * * *'
  pull_request:
  push:
    # trying and staging branches are for Bors config
    branches:
      - trying
      - staging

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1
  RUSTFLAGS: "-D warnings"

jobs:
  test-linux:
    name: Tests on ubuntu-18.04
    runs-on: ubuntu-latest
    container:
      # Use ubuntu-18.04 to compile with glibc 2.27, which are the production expectations
      image: ubuntu:18.04
    steps:
    - uses: actions/checkout@v3
    - name: Install needed dependencies
      run: |
        apt-get update && apt-get install -y curl
        apt-get install build-essential -y
    - name: Run test with Rust stable
      if: github.event_name != 'schedule'
      uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        override: true
    - name: Run test with Rust nightly
      if: github.event_name == 'schedule'
      uses: actions-rs/toolchain@v1
      with:
        toolchain: nightly
        override: true
    # Disable cache due to disk space issues with Windows workers in CI
    # - name: Cache dependencies
    #   uses: Swatinem/rust-cache@v2.2.0
    - name: Run cargo check without any default features
      uses: actions-rs/cargo@v1
      with:
        command: build
        args: --locked --release --no-default-features --all
    - name: Run cargo test
      uses: actions-rs/cargo@v1
      with:
        command: test
        args: --locked --release --all

  test-others:
    name: Tests on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [macos-12, windows-2022]
    steps:
    - uses: actions/checkout@v3
#     - name: Cache dependencies
#       uses: Swatinem/rust-cache@v2.2.0
    - name: Run cargo check without any default features
      uses: actions-rs/cargo@v1
      with:
        command: build
        args: --locked --release --no-default-features --all
    - name: Run cargo test
      uses: actions-rs/cargo@v1
      with:
        command: test
        args: --locked --release --all

  # We run tests in debug also, to make sure that the debug_assertions are hit
  test-debug:
    name: Run tests in debug
    runs-on: ubuntu-latest
    container:
      # Use ubuntu-18.04 to compile with glibc 2.27, which are the production expectations
      image: ubuntu:18.04
    steps:
      - uses: actions/checkout@v3
      - name: Install needed dependencies
        run: |
          apt-get update && apt-get install -y curl
          apt-get install build-essential -y
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
      # - name: Cache dependencies
      #   uses: Swatinem/rust-cache@v2.2.0
      - name: Run tests in debug
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --locked --all

  clippy:
    name: Run Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: 1.67.0
          override: true
          components: clippy
      # - name: Cache dependencies
      #   uses: Swatinem/rust-cache@v2.2.0
      - name: Run cargo clippy
        uses: actions-rs/cargo@v1
        with:
          command: clippy
          # allow unlined_format_args https://github.com/rust-lang/rust-clippy/issues/10087
          args: --all-targets -- --deny warnings --allow clippy::uninlined_format_args

  fmt:
    name: Run Rustfmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: nightly
          override: true
          components: rustfmt
      # - name: Cache dependencies
      #   uses: Swatinem/rust-cache@v2.2.0
      - name: Run cargo fmt
        # Since we never ran the `build.rs` script in the benchmark directory we are missing one auto-generated import file.
        # Since we want to trigger (and fail) this action as fast as possible, instead of building the benchmark crate
        # we are going to create an empty file where rustfmt expects it.
        run: |
          echo -ne "\n" > benchmarks/benches/datasets_paths.rs
          cargo fmt --all -- --check