name: CI

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

env:
  CARGO_TERM_COLOR: always

jobs:
  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - uses: Swatinem/rust-cache@v2
      - run: cargo clippy --all-targets --locked -- -D warnings

  doc:
    name: Doc
    runs-on: ubuntu-latest
    timeout-minutes: 10
    env:
      RUSTDOCFLAGS: -D warnings
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo doc --locked --no-deps

  test:
    name: Build & test (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    timeout-minutes: 30
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest]
    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt

      - uses: Swatinem/rust-cache@v2

      - name: Format check
        run: cargo fmt --all --check

      # `cargo build` runs build.rs — the port-purity gate that rejects
      # any fn under src/ported/ with no counterpart in the htop C source.
      - name: Build (runs port-purity gate)
        run: cargo build --locked --verbose

      - name: Test
        run: cargo test --locked --verbose

      - name: Smoke-test the binary
        run: |
          cargo build --locked --bin htoprs
          ./target/debug/htoprs --version
          ./target/debug/htoprs --help | head -1
