name: CI

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

on:
  push:
    branches:
      - master
  pull_request:

jobs:
  # == FORMATTING CHECK == #
  fmt:
    name: "Check formatting"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      # GitHub runners come with cargo fmt already installed, so we don't
      # even need to install a toolchain unless it turns out stale.
      - run: cargo fmt --check

  # == CHECK == #
  check:
    name: "Check beta stable and MSRV=1.96.0"
    runs-on: ubuntu-latest
    strategy:
      matrix:
        rust-toolchain:
          - beta
          - stable
          # Try to guard against a near-future regression.
          - 1.96.0
        cargo-locked: ['', '--locked']
        feature-nightly-dropck_eyepatch: ['', '--features nightly-dropck_eyepatch']
        exclude:
          # MSRV guarantee only stands for `.lock`-tested dependencies.
          - rust-toolchain: beta
            cargo-locked: ''
    steps:
      - name: Install Rust toolchain
        run: |
          rustup toolchain install "${{ matrix.rust-toolchain }}" --profile=minimal
          rustup override set "${{ matrix.rust-toolchain }}"

      - name: Clone repo
        uses: actions/checkout@v2

      - name: Update `Cargo.lock`
        if: matrix.cargo-locked != '--locked'
        run: cargo update -v

      - name: Cargo check
        env:
          RUSTC_BOOTSTRAP: 1
        run: cargo check ${{ matrix.cargo-locked }} ${{ matrix.feature-nightly-dropck_eyepatch }}

  # == BUILD & TEST == #
  build-and-test:
    name: Build and test
    runs-on: ${{ matrix.os }}
    needs: []
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest
        rust-toolchain:
          - 1.96.0
          - stable
    steps:
      - name: Install Rust toolchain
        run: |
          rustup toolchain install "${{ matrix.rust-toolchain }}"
          rustup override set "${{ matrix.rust-toolchain }}"

      - name: Clone repo
        uses: actions/checkout@v2

      - run: cargo test --lib --tests

      - name: cargo test --doc
        if: matrix.rust-toolchain == 'stable'
        env:
          RUSTC_BOOTSTRAP: 1
        run: cargo test --features better-docs --doc

  required-jobs:
    name: 'All the required jobs'
    needs:
      - check
      - build-and-test
    runs-on: ubuntu-latest
    if: ${{ always() }}
    steps:
      - name: 'Check success of the required jobs'
        run: |
          RESULT=$(echo "${{ join(needs.*.result, '') }}" | sed -e "s/success//g")
          if [ -n "$RESULT" ]; then
            echo "❌"
            false
          fi
          echo "✅"
