name: Build

on:
  push:
    branches:
      - "**"
  pull_request:
    branches:
      - main
      - "release/**"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    runs-on: ${{ matrix.os }}
    name: build (${{ matrix.os }} / ${{ matrix.tls }})

    strategy:
      fail-fast: false
      matrix:
        os: [ ubuntu-22.04, ubuntu-latest ]
        tls: [ rustls-aws-lc, rustls-ring, native-tls ]

    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable

      - name: Install dependencies
        # libssl-dev is only needed for the `native-tls` backend on Linux
        # but adding it unconditionally keeps the install step identical
        # across matrix cells.
        run: |
          sudo apt-get update && sudo apt-get install -y \
            make libfontconfig1-dev pkg-config build-essential libssl-dev

      # `--all-targets` = lib + bins + tests + examples + benches. This
      # gives us CI coverage of every `examples/*.rs` under every TLS
      # backend for free (per issue #54) instead of a dedicated
      # single-backend "compile examples" step — if an example ever
      # stops compiling, one of the six matrix cells fails.
      - name: Build --all-targets (${{ matrix.tls }})
        run: cargo build --all-targets --no-default-features --features ${{ matrix.tls
          }}

  # Negative-case build: asserts the `compile_error!` mutex in
  # `src/tls.rs` rejects two TLS backends simultaneously. If the build
  # ever *succeeds* with this combination, the mutex has silently
  # regressed and the job fails.
  mutex-check:
    runs-on: ubuntu-latest
    name: mutex-check (rustls-aws-lc + rustls-ring)

    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable

      - name: Install dependencies
        run: |
          sudo apt-get update && sudo apt-get install -y \
            make libfontconfig1-dev pkg-config build-essential

      - name: Confirm the TLS mutex rejects two simultaneous backends
        run: |
          set -eu
          if cargo build --no-default-features \
               --features "rustls-aws-lc,rustls-ring" 2>build.log
          then
            echo "::error::build should have failed with compile_error!"
            echo "---- build log ----"
            cat build.log
            exit 1
          fi
          if ! grep -q "select exactly one TLS backend" build.log
          then
            echo "::error::expected mutex diagnostic missing from build output"
            echo "---- build log ----"
            cat build.log
            exit 1
          fi
          echo "mutex correctly rejected the double-backend build"

  # Build rustdoc with warnings-as-errors (per issue #56). Catches dead
  # intra-doc links, missing crate-level docs, and private-item leaks
  # before they land on docs.rs. `--features integration-tests` is used
  # instead of `--all-features` because the latter activates all three
  # mutually-exclusive TLS backends at once and trips the compile_error!
  # mutex — same reasoning as `make lint`.
  rustdoc:
    runs-on: ubuntu-latest
    name: rustdoc (-D warnings)

    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable

      - name: Install dependencies
        run: |
          sudo apt-get update && sudo apt-get install -y \
            make libfontconfig1-dev pkg-config build-essential

      - name: cargo doc --features integration-tests
        env:
          RUSTDOCFLAGS: "-D warnings"
        run: cargo doc --no-deps --features integration-tests
