name: check
# This workflow runs whenever a PR is opened or updated, or a commit is pushed
# to main. It runs several checks:
# - fmt: checks that the code is formatted according to `rustfmt`.
# - clippy: checks that the code does not contain any `clippy` warnings.
# - doc: checks that the code can be documented without errors.
# - typos: checks for typos across the repo.
permissions:
  contents: read
# This configuration allows maintainers of this repo to create a branch and
# pull request based on the new branch. Restricting the push trigger to the
# main branch ensures that the PR only gets built once.
on:
  push:
    branches: [main, v*]
  pull_request:
# If new code is pushed to a PR branch, then cancel in progress workflows for
# that PR. Ensures that we don't waste CI time, and returns results quicker.
# https://github.com/jonhoo/rust-ci-conf/pull/5
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true
env:
  CARGO_TERM_COLOR: always

jobs:
  cargo-build:
    name: Cargo Build
    runs-on: ubuntu-latest

    steps:
      - name: Harden the runner (Audit all outbound calls)
        uses: step-security/harden-runner@95d9a5deda9de15063e7595e9719c11c38c90ae2 # v2.13.2
        with:
          egress-policy: audit

      - name: Fetch Repository
        uses: actions/checkout@71cf2267d89c5cb81562390fa70a37fa40b1305e

      - name: Install stable toolchain
        uses: actions-rust-lang/setup-rust-toolchain@1780873c7b576612439a134613cc4cc74ce5538c # v1.15.2

      - name: cargo build
        run: cargo b --locked --all-targets --all-features

  cargo-fmt:
    name: Cargo fmt
    runs-on: ubuntu-latest

    steps:
      - name: Harden the runner (Audit all outbound calls)
        uses: step-security/harden-runner@95d9a5deda9de15063e7595e9719c11c38c90ae2 # v2.13.2
        with:
          egress-policy: audit

      - name: Fetch Repository
        uses: actions/checkout@71cf2267d89c5cb81562390fa70a37fa40b1305e

      - name: Install stable toolchain
        uses: actions-rust-lang/setup-rust-toolchain@1780873c7b576612439a134613cc4cc74ce5538c # v1.15.2
        with:
          components: rustfmt
          toolchain: nightly

      - name: Rustfmt Check
        run: cargo fmt --all --check

  cargo-clippy:
    name: Cargo clippy
    runs-on: ubuntu-latest

    steps:
      - name: Harden the runner (Audit all outbound calls)
        uses: step-security/harden-runner@95d9a5deda9de15063e7595e9719c11c38c90ae2 # v2.13.2
        with:
          egress-policy: audit

      - name: Fetch Repository
        uses: actions/checkout@71cf2267d89c5cb81562390fa70a37fa40b1305e

      - name: Install stable toolchain
        uses: actions-rust-lang/setup-rust-toolchain@1780873c7b576612439a134613cc4cc74ce5538c # v1.15.2
        with:
          components: clippy

      - name: Clippy Check
        run: cargo clippy --all-targets --all-features -- -D warnings -D clippy::pedantic

  cargo-doc:
    name: Cargo doc
    runs-on: ubuntu-latest

    steps:
      - name: Harden the runner (Audit all outbound calls)
        uses: step-security/harden-runner@95d9a5deda9de15063e7595e9719c11c38c90ae2 # v2.13.2
        with:
          egress-policy: audit

      - name: Fetch Repository
        uses: actions/checkout@71cf2267d89c5cb81562390fa70a37fa40b1305e

      - name: Install stable toolchain
        uses: actions-rust-lang/setup-rust-toolchain@1780873c7b576612439a134613cc4cc74ce5538c # v1.15.2

      - name: Rustdoc Check
        run: cargo doc --no-deps --all-features
        env:
          RUSTDOCFLAGS: -D warnings

  typos-cli:
    name: typos
    runs-on: ubuntu-latest

    steps:
      - name: Harden the runner (Audit all outbound calls)
        uses: step-security/harden-runner@95d9a5deda9de15063e7595e9719c11c38c90ae2 # v2.13.2
        with:
          egress-policy: audit

      - name: Fetch Repository
        uses: actions/checkout@71cf2267d89c5cb81562390fa70a37fa40b1305e

      - name: Check spelling of files in the workspace
        uses: crate-ci/typos@07d900b8fa1097806b8adb6391b0d3e0ac2fdea7 # v1.39.0
