name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  fmt:
    name: Rustfmt Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - name: Check formatting
        run: cargo fmt --all -- --check

  clippy:
    name: Clippy Check
    needs: [fmt]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - name: Restore Rust cache
        uses: Swatinem/rust-cache@v2
        with:
          shared-key: ci-${{ runner.os }}-${{ runner.arch }}
          save-if: ${{ github.event_name != 'pull_request' }}
          cache-on-failure: true
      - name: Run clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

  test:
    name: Test Suite
    needs: [fmt]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
      - name: Restore Rust cache
        uses: Swatinem/rust-cache@v2
        with:
          shared-key: ci-${{ runner.os }}-${{ runner.arch }}
          save-if: ${{ github.event_name != 'pull_request' }}
          cache-on-failure: true
      - name: Run tests
        run: cargo test --all-targets
