name: ci

on:
  push:
    branches:
      - main
  pull_request:

env:
  # Disable incremental compilation.
  CARGO_INCREMENTAL: 0
  # Allow more retries for network requests while downloading dependencies.
  CARGO_NET_RETRY: 10
  RUSTUP_MAX_RETRIES: 10
  # Emit short backtraces.
  RUST_BACKTRACE: short

jobs:
  check:
    name: cargo check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - name: Check
        run: cargo check --all --tests --benches

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

  warnings:
    name: cargo clippy
    runs-on: ubuntu-latest
    needs: check
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - run: cargo clippy --verbose

  test:
    name: "cargo test"
    needs: check
    runs-on: ubuntu-latest
    strategy:
      matrix:
        toolchain:
          - stable
          - beta
          - nightly
        target:
          - x86_64-unknown-linux-gnu
          - i686-unknown-linux-gnu
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: ${{ matrix.toolchain }}
      - uses: taiki-e/setup-cross-toolchain-action@v1
        with:
          target: ${{ matrix.target }}
      - run: cargo test --verbose

  miri:
    name: "cargo miri"
    needs: check
    runs-on: ubuntu-latest
    strategy:
      matrix:
        toolchain:
          - nightly
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: ${{ matrix.toolchain }}
          components: miri
      - run: cargo miri test --verbose
