name: Test, Format, and Lint

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    name: Test
    runs-on: ${{ matrix.runner }}
    strategy:
      matrix:
        toolchain:
          - stable
          - beta
          - nightly
        runner:
          - ubuntu-latest
          - macos-latest
          - windows-latest
    environment:
      name: test
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Setup Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: ${{ matrix.toolchain }}
          cache-key: ${{ runner.os }}-${{ matrix.toolchain }}

      - name: Build
        run: cargo build --verbose

      - name: Test
        run: cargo test --verbose

  lint:
    name: Lint
    runs-on: ubuntu-latest
    environment:
      name: lint
    strategy:
      matrix:
        toolchain:
          - stable
          - beta
          - nightly
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Setup Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: ${{ matrix.toolchain }}
          components: clippy
          cache-key: ${{ runner.os }}-${{ matrix.toolchain }}

      - name: Run cargo checks
        run: cargo check --verbose

      - name: Run clippy
        run: cargo clippy --verbose -- -D warnings

  format:
    name: Format
    runs-on: ubuntu-latest
    environment:
      name: format
    strategy:
      matrix:
        toolchain:
          - stable
          - beta
          - nightly
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Setup Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: ${{ matrix.toolchain }}
          components: rustfmt
          cache-key: ${{ runner.os }}-${{ matrix.toolchain }}

      - name: Run rustfmt
        uses: actions-rust-lang/rustfmt@v1

  deny:
    name: Cargo deny
    runs-on: ubuntu-latest
    environment:
      name: deny
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Setup Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
          cache-key: ${{ runner.os }}-stable

      - name: Run cargo-deny
        uses: EmbarkStudios/cargo-deny-action@v2
