name: Code Style
on:
  pull_request:

jobs:
  format:
    name: Formatting
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v6
    - name: Install Rust
      uses: dtolnay/rust-toolchain@stable
      with:
        profile: minimal
        components: rustfmt
    - name: Check formatting
      run: cargo fmt --all -- --check

  clippy:
    name: Clippy (${{ matrix.name }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: all-features
            flags: "--all-features"
          - name: default
            flags: ""
          - name: libsql-only
            flags: "--no-default-features --features libsql"
    steps:
    - name: Checkout repository
      uses: actions/checkout@v6
    - name: Install Rust
      uses: dtolnay/rust-toolchain@stable
      with:
        profile: minimal
        components: clippy
    - uses: Swatinem/rust-cache@v2
      with:
        key: clippy-${{ matrix.name }}
    - name: Check lints
      run: cargo clippy --all --benches --tests --examples ${{ matrix.flags }} -- -D warnings

  # Roll-up job for branch protection
  code-style:
    name: Code Style (fmt + clippy)
    runs-on: ubuntu-latest
    if: always()
    needs: [format, clippy]
    steps:
      - run: |
          if [[ "${{ needs.format.result }}" != "success" || "${{ needs.clippy.result }}" != "success" ]]; then
            echo "One or more jobs failed"
            exit 1
          fi
