name: CI
on:
  pull_request:
  push:
    branches:
      - master

jobs:

  test:
    name: Test
    runs-on: ubuntu-latest
    strategy:
      matrix:
        rust: [1.0.0, 1.5.0, 1.10.0, 1.15.0, 1.20.0, 1.25.0, 1.30.0, 1.35.0,
               1.40.0, 1.45.0, 1.50.0, 1.55.0, 1.60.0, 1.65.0, 1.70.0, 1.75.0,
               1.80.0, # first version with rustc-check-cfg
               stable, beta, nightly]
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.rust }}
      - run: cargo build --verbose
      - run: cargo test --verbose
      - run: cargo run
        working-directory: ./ci/verify-check-cfg

  # try probing a target that doesn't have std at all
  no_std:
    name: No Std
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          target: thumbv6m-none-eabi
      - run: cargo test --verbose --lib
        env:
          TARGET: thumbv6m-none-eabi

  # we don't even need an installed target for version checks
  missing_target:
    name: Missing Target
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo test --verbose --lib -- version
        env:
          TARGET: thumbv6m-none-eabi

  fmt:
    name: Format
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@1.77.0
        with:
          components: rustfmt
      - run: cargo fmt --all --check

  # One job that "summarizes" the success state of this pipeline. This can then be added to branch
  # protection, rather than having to add each job separately.
  success:
    name: Success
    runs-on: ubuntu-latest
    needs: [test, no_std, missing_target, fmt]
    # Github branch protection is exceedingly silly and treats "jobs skipped because a dependency
    # failed" as success. So we have to do some contortions to ensure the job fails if any of its
    # dependencies fails.
    if: always() # make sure this is never "skipped"
    steps:
      # Manually check the status of all dependencies. `if: failure()` does not work.
      - name: check if any dependency failed
        run: jq --exit-status 'all(.result == "success")' <<< '${{ toJson(needs) }}'