pdf2 0.2.0-alpha.2

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

  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
          cargo +stable package -p pdf2-cli

  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

      - name: Cache wasm-bindgen-cli
        uses: actions/cache@v4
        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

      - name: Build the package
        run: cargo +stable build -p pdf2-wasm --release --target wasm32-unknown-unknown

      - name: Generate the bindings
        run: |
          wasm-bindgen --target nodejs --out-dir crates/pdf2-wasm/pkg-node \
            target/wasm32-unknown-unknown/release/pdf2_wasm.wasm
          wasm-bindgen --target web --no-typescript --out-dir crates/pdf2-wasm/demo/pkg \
            target/wasm32-unknown-unknown/release/pdf2_wasm.wasm

      - name: API smoke test and size budget
        run: node --no-warnings crates/pdf2-wasm/check.mjs

  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