name: CI

on:
  push:
    branches: [master]
  pull_request:

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always

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

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

      - name: Cache
        uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.os }}

      # `pam-client2` (gated to Linux in Cargo.toml) links against libpam,
      # so the Ubuntu runner needs the dev headers + shared library before
      # any `--all-features` build. macOS and Windows don't see the dep at
      # all (target-conditional) so nothing extra is needed there.
      - name: Install libpam (Linux only)
        if: runner.os == 'Linux'
        run: sudo apt-get update && sudo apt-get install -y libpam0g-dev

      - name: Format
        if: runner.os == 'Linux' # formatting is platform-independent
        run: cargo fmt --all --check

      - name: Clippy (all features, warnings denied)
        run: cargo clippy --all-targets --all-features -- -D warnings

      - name: Test (all features)
        run: cargo test --all-features

  no_std:
    name: no_std builds
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Cache
        uses: Swatinem/rust-cache@v2

      - name: Build no_std + alloc (no std)
        run: cargo build --no-default-features --features alloc

      - name: Build with all module features but no std
        run: cargo build --no-default-features --features "alloc,client,server"

  c_abi:
    name: C ABI smoke test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Cache
        uses: Swatinem/rust-cache@v2

      - name: Build the C library (static + shared)
        run: |
          cargo rustc --lib --release --features ffi --crate-type staticlib
          cargo rustc --lib --release --features ffi --crate-type cdylib

      - name: Compile and run the C smoke test
        run: |
          cc tests/ffi_smoke.c -I include target/release/libpuressh.a \
             -lpthread -ldl -lm -o ffi_smoke
          ./ffi_smoke

  docs:
    name: Docs build (warnings denied)
    runs-on: ubuntu-latest
    env:
      RUSTDOCFLAGS: -D warnings -D rustdoc::broken-intra-doc-links
    steps:
      - uses: actions/checkout@v6

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Cache
        uses: Swatinem/rust-cache@v2

      - name: Install libpam (for --all-features doc build)
        run: sudo apt-get update && sudo apt-get install -y libpam0g-dev

      - name: cargo doc --no-deps --all-features
        run: cargo doc --no-deps --all-features
