name: Code Quality

on:
  push:
    branches:
      - main
      - master
      - dev*
      - feat*
      - fix*
      - chore*
      - test*
    paths:
      - "**/*.rs"
      - "**/Cargo.toml"
      - "Cargo.lock"
      - ".github/workflows/code-quality.yml"
  pull_request:
    types: [ opened, synchronize, reopened, ready_for_review ]
    paths:
      - "**/*.rs"
      - "**/Cargo.toml"
      - "Cargo.lock"
      - ".github/workflows/code-quality.yml"

env:
  CARGO_TERM_COLOR: always

jobs:
  check:
    name: Code Quality Checks
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy, rustfmt

      - name: Cache Cargo dependencies
        uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry/index
            ~/.cargo/registry/cache
            ~/.cargo/git/db
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-

      - name: Check code formatting
        run: cargo fmt --all -- --check

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

      - name: Run tests
        run: cargo test --all-features

      - name: Check documentation
        run: cargo doc --all-features --no-deps --document-private-items
        env:
          RUSTDOCFLAGS: -D warnings
