pdf2 1.1.0

A PDF 2.0 (ISO 32000-2) reader, writer and text extractor.
Documentation
name: CI

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

permissions:
  contents: read

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

env:
  CARGO_TERM_COLOR: always
  CARGO_INCREMENTAL: 0

jobs:
  gate:
    name: Gate (fmt, clippy, test, docs)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      # Every cargo invocation below names its toolchain with `+…` on purpose:
      # `rust-toolchain.toml` pins stable for local work, and an explicit
      # `+toolchain` argument outranks that file, so CI keeps testing exactly
      # the toolchains this workflow lists.
      - name: Install Rust
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: stable
          components: rustfmt, clippy

      - uses: Swatinem/rust-cache@v2

      # These are the objective file's own checks, run verbatim.
      - name: Format
        run: cargo +stable fmt --all --check

      - name: Clippy
        run: cargo +stable clippy --workspace --all-targets -- -D warnings

      - name: Test
        run: cargo +stable test --workspace

      - name: Test every feature
        run: cargo +stable test -p pdf2 --all-features

      - name: Docs
        env:
          RUSTDOCFLAGS: -D warnings
        run: cargo +stable doc --workspace --no-deps --lib

  test:
    name: Test (${{ matrix.toolchain }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        # 1.87 is the declared MSRV (`rust-version` in Cargo.toml).
        toolchain: ["1.87", beta]
    steps:
      - uses: actions/checkout@v6

      - name: Install Rust ${{ matrix.toolchain }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.toolchain }}

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

      - name: Test
        run: cargo +${{ matrix.toolchain }} test --workspace

  package:
    name: Package
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

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

      - uses: Swatinem/rust-cache@v2

      # Catches whatever would only break on upload: a missing README include,
      # a file the tests need but the package drops, metadata cargo rejects.
      - name: Build the packaged crates
        run: |
          cargo +stable package -p pdf2 --list
          cargo +stable package -p pdf2
          # The CLI requires the library version in this tree. Between a version
          # bump and its first publish (the release pull request itself) that
          # version is not on crates.io yet, so packaging cannot resolve it.
          # The publish workflow packages every crate in dependency order once
          # the library lands; any other packaging failure still fails here.
          if ! output="$(cargo +stable package -p pdf2-cli 2>&1)"; then
            if grep -q 'failed to select a version for the requirement `pdf2 =' <<<"$output"; then
              echo "::notice::pdf2-cli needs the library version published first; the publish workflow packages it"
            else
              echo "$output"
              exit 1
            fi
          fi

  no-std:
    name: no_std (${{ matrix.target }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        target: [x86_64-unknown-linux-gnu, thumbv7em-none-eabihf]
    steps:
      - uses: actions/checkout@v6

      - name: Install Rust for ${{ matrix.target }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: stable
          targets: ${{ matrix.target }}

      - uses: Swatinem/rust-cache@v2
        with:
          key: no-std-${{ matrix.target }}

      - name: Build without std
        run: cargo +stable build -p pdf2 --no-default-features --features alloc --target ${{ matrix.target }}

      # Tests and examples use std, so only the library is linted here.
      - name: Clippy without std
        if: matrix.target == 'x86_64-unknown-linux-gnu'
        run: cargo +stable clippy -p pdf2 --no-default-features --features alloc -- -D warnings

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

      # Both targets are installed up front; Node is already on the runner.
      - name: Install Rust for the wasm targets
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: stable
          targets: wasm32-unknown-unknown,wasm32-wasip1

      - uses: Swatinem/rust-cache@v2
        with:
          key: wasm

      - name: Build the browser module
        run: cargo +stable build -p pdf2-wasm-smoke --release --target wasm32-unknown-unknown --lib

      - name: Build the WASI binary
        run: cargo +stable build -p pdf2-wasm-smoke --release --target wasm32-wasip1 --bin pdf2-wasi-smoke

      # Checks the size budgets and runs both modules: the browser one through
      # its C ABI, the WASI one against a preopened fixture.
      - name: Smoke test both targets in Node
        run: node --no-warnings crates/pdf2-wasm-smoke/smoke.mjs

  wasm-package:
    name: wasm package
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Install Rust for wasm
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: stable
          targets: wasm32-unknown-unknown

      - uses: Swatinem/rust-cache@v2
        with:
          key: wasm-package

      - uses: actions/setup-node@v7
        with:
          node-version: "22"

      - name: Cache wasm-bindgen-cli
        uses: actions/cache@v6
        with:
          path: ~/.cargo/bin/wasm-bindgen
          key: wasm-bindgen-cli-0.2.127

      - name: Install wasm-bindgen-cli
        run: |
          if ! command -v wasm-bindgen >/dev/null; then
            cargo +stable install wasm-bindgen-cli --version 0.2.127 --locked
          fi
          wasm-bindgen --version

      # One chore builds the published package (wasm, glue, types, manifest,
      # README, licences), copies what the demo loads, runs the API smoke test
      # and size budget against the package, and then drives the demo page in
      # headless Chrome — the runner has one, so a broken interaction fails here
      # instead of in someone's browser.
      - name: Build the package and drive the demo
        run: cargo +stable run -q -p xtask -- demo

  deny:
    name: Licences and advisories
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: EmbarkStudios/cargo-deny-action@v2
        with:
          command: check

  semver:
    name: Semver
    runs-on: ubuntu-latest
    # The action's own bundle still requires Node's deprecated `punycode`
    # module; the warning is about its dependencies, not about this repository,
    # so the one code is disabled rather than every Node warning.
    env:
      NODE_OPTIONS: --disable-warning=DEP0040
    steps:
      - uses: actions/checkout@v6
      - uses: obi1kenobi/cargo-semver-checks-action@v2.9
        with:
          package: pdf2

  bench:
    name: Benchmarks
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

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

      - uses: Swatinem/rust-cache@v2

      # Deterministic metrics (allocation counts and bytes) are gated against
      # the committed baseline on every platform; throughput is gated only for
      # a platform that has a baseline entry of its own.
      - name: Measure and compare with the committed baseline
        run: cargo +stable run --release -p pdf2-bench -- --check