on:
  # Test on PRs for any branch
  pull_request:
    branches:
      - "*"
  push:
    branches:
      - master

# NOTE: Should we use fail-fast: false?

name: Tests
permissions:
  contents: read

jobs:
  test:
    strategy:
      matrix:
        toolchain:
          - stable
          - beta
          - nightly
          - 1.83.0 # MSRV
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest

    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout sources
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
        with:
          persist-credentials: false

      - name: Install toolchain
        uses: dtolnay/rust-toolchain@b3b07ba8b418998c39fb20f53e8b695cdcc8de1b
        with:
          toolchain: ${{ matrix.toolchain }}

      - name: Test debug-mode, default features
        run: cargo test

      - name: Test debug-mode, all features
        run: cargo test --all-features

      - name: Test debug-mode, no default features
        run: cargo test --no-default-features

      - name: Test debug-mode, no-default + alloc feature
        run: cargo test --no-default-features --features alloc --tests

      - name: Test debug-mode, no-default + serde feature (enables alloc)
        run: cargo test --no-default-features --features serde --tests

      - name: Test release-mode, default features
        run: cargo test --release

      - name: Test release-mode, all features
        run: cargo test --release --all-features

      - name: Test release-mode, no default features
        run: cargo test --release --no-default-features

      - name: Test release-mode, no-default + alloc feature
        run: cargo test --release --no-default-features --features alloc --tests

      - name: Test release-mode, no-default + serde feature (enables alloc)
        run: cargo test --release --no-default-features --features serde --tests

  sanitizers:
    name: Tests w. sanitizers
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
        with:
          persist-credentials: false

      - name: Install toolchain
        uses: dtolnay/rust-toolchain@b3b07ba8b418998c39fb20f53e8b695cdcc8de1b
        with:
          toolchain: nightly
          targets: x86_64-unknown-linux-gnu

      # Release (LeakSanitizer is enabled by default with AddressSanitizer for x86_64 Linux builds)
      # https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer
      - run: RUSTFLAGS="-Z sanitizer=address" ASAN_OPTIONS="detect_odr_violation=0" cargo test --all-features --tests --release --target x86_64-unknown-linux-gnu
      - run: RUSTFLAGS="-Z sanitizer=address" ASAN_OPTIONS="detect_odr_violation=0" cargo test --no-default-features --tests --release --target x86_64-unknown-linux-gnu

  no_std:
    name: no_std build
    runs-on: ubuntu-latest
    strategy:
      matrix:
        toolchain:
          - stable
          - nightly
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
        with:
          persist-credentials: false

      - uses: houseabsolute/actions-rust-cross@f7da4010930154943c99d13df0151dece91a924f
        with:
          command: build
          target: thumbv7em-none-eabi
          args: "--release --no-default-features"

  cross_compilation:
    name: Linux/ARM - Release tests
    runs-on: ubuntu-latest
    strategy:
      matrix:
        arch:
          - i686-unknown-linux-gnu
          - armv7-unknown-linux-gnueabihf
          - powerpc64-unknown-linux-gnu
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
        with:
          persist-credentials: false

      - uses: houseabsolute/actions-rust-cross@f7da4010930154943c99d13df0151dece91a924f
        with:
          command: test
          target: ${{ matrix.arch }}
          args: "--release"

  # https://rustwasm.github.io/docs/book/reference/add-wasm-support-to-crate.html#maintaining-ongoing-support-for-webassembly
  web_assembly:
    name: WebAssembly - Release build
    runs-on: ubuntu-latest
    strategy:
      matrix:
        arch:
          - wasm32-unknown-unknown
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
        with:
          persist-credentials: false

      - uses: dtolnay/rust-toolchain@b3b07ba8b418998c39fb20f53e8b695cdcc8de1b
        with:
          toolchain: stable
          targets: ${{ matrix.arch }}

      - run: cargo check --no-default-features --target ${WASM_TARGET}
        # Remediation for potential template-injection: https://docs.zizmor.sh/audits/#template-injection
        env:
          WASM_TARGET: ${{ matrix.arch }}

  docs:
    name: Build documentation
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
        with:
          persist-credentials: false

      - uses: dtolnay/rust-toolchain@b3b07ba8b418998c39fb20f53e8b695cdcc8de1b
        with:
          toolchain: stable

      - run: cargo doc --no-deps --all-features

  benches:
    name: Build and check benchmarks
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
        with:
          persist-credentials: false

      - uses: dtolnay/rust-toolchain@b3b07ba8b418998c39fb20f53e8b695cdcc8de1b
        with:
          toolchain: stable

      - run: cargo test --benches

  semver_checks:
    name: Check SemVer
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
        with:
          persist-credentials: false

      - uses: obi1kenobi/cargo-semver-checks-action@de90d0688b68824ce9d50523ae0a4066961d8d0b
      
      - run: cargo semver-checks
