Skip to content

feat(capi): Lexbor's users had no C API to switch to - #78

Merged
jamesconwaydev merged 9 commits into
mainfrom
capi
Nov 19, 2026
Merged

jamesconwaydev merged 9 commits into
mainfrom
capi

Conversation

@jamesconwaydev

Copy link
Copy Markdown
Collaborator

Problem

Parsec could only be called from Rust. Lexbor's users, and the bindings built on it (PHP, Ruby, Elixir, Crystal, selectolax), call C, so there was nothing for them to switch to: no library, no header, no packages, and no guide from Lexbor's calls to Parsec's.

Closes #20
Closes #21
Closes #22

Change

crates/parsec-capi, building libparsec as a shared and a static library (cdylib + staticlib) over parsec-html's public API. No change to crates/parsec.

  • include/parsec.h is generated by cbindgen from src/lib.rs and checked in; CI fails if it drifts. The rules every call keeps are at the top of the header.
  • Opaque handles only: parsec_document and parsec_selector, each with one free function. No struct layout is part of the API.
  • Nodes are numbers (parsec_node, a uint32_t), valid for the document's life. Removing a node only detaches it, so nothing dangles, and every number is checked against the document before use.
  • Strings in are pointer and length, UTF-8, with invalid bytes becoming U+FFFD. Strings out are either borrowed (const char * plus length, no NUL, valid until the next change) or owned copies (char *, NUL-terminated, freed with parsec_string_free, which keeps its own size so it needs only the pointer).
  • Errors are status codes (PARSEC_OK, PARSEC_ERR_NULL, ..._NO_SUCH_NODE, ..._CYCLE and the rest), each with a message from parsec_status_message. Every call is NULL-safe and runs inside catch_unwind, so a panic comes back as PARSEC_ERR_PANIC instead of unwinding into C.
  • What it covers, from the bindings' list:
    • parse, with limits, and parse a fragment;
    • navigation, node types (the DOM's nodeType numbers, same as Lexbor's), names and values;
    • attributes by name and by index;
    • text, inner and outer HTML;
    • selectors compiled once, run by callback, first match, or matches;
    • create, append, insert before and remove.
  • Threads: reads take const parsec_document * and can run concurrently; a compiled selector can be shared; edits need the document to themselves. This rests on the compile-time Send + Sync check already in crates/parsec.
  • package.sh OUT [TARGET] lays out include/, lib/, lib/pkgconfig/parsec.pc and lib/cmake/parsec/ (parsec::parsec and parsec::parsec_static, with the system libraries rustc reports for a static link), plus LICENSE and NOTICE. universal-apple-darwin builds both macOS architectures and joins them with lipo. The macOS dylib's install name is @rpath/libparsec.dylib (build.rs), so a prebuilt copy works from wherever it's unpacked.

.github/workflows/release-capi.yml, separate from release.yml:

  • On a v* tag it packages Linux x86-64 and ARM64 (on 22.04, for glibc 2.35), macOS universal, and Windows x86-64 and ARM64.
  • Each package is smoke-tested by building tests/capi.c against it through CMake, shared and static, then archived.
  • It then waits for the release that release.yml creates once the tag's signature checks out. Only then does it attest the archives and attach them, with parsec-capi-SHA256SUMS and the provenance bundle, so nothing is published for a tag that fails the check.
  • Pull requests touching the C API build all five packages too. Permissions are contents: read except the publish job, and every action is pinned by commit.

ci.yml:

  • capi builds the package and runs the CMake test on Linux, macOS (universal) and Windows.
  • capi-checks checks the header against cbindgen 0.29.4, compiles it as C++, and runs the C test through pkg-config under ASan, LSan and UBSan.

Docs:

  • README and ROADMAP move the C API and the guide to done.
  • SECURITY.md says how to verify the C archives.
  • The wiki pages (a new C-API reference, and Moving-from-Lexbor rewritten with a C column and side-by-side C programs) go up after merge.

I didn't add a drop-in lxb_* shim, as the design doc decided.

Tests

  • tests/capi.c calls every function. It parses, selects, walks, edits and serializes, and it checks each error path: NULL handles, out-of-range node numbers, bad flags and namespaces, refused edits, a bad selector with its message, a budget timeout, and invalid UTF-8.
  • cargo test builds and runs it on Unix (tests/c.rs). Locally on macOS, it passes as is and under -fsanitize=address,undefined against the static library.
  • The CMake test passes locally against a package.sh package, shared and static.
  • The guide's C side has a Lexbor program and a Parsec program that do the same job: parse, select, text, outerHTML, attributes, insert and remove, fragment. Both compile against Lexbor v3.0.0 (commit 2ae88a1, the one bench/ builds) and print identical output.
  • cargo fmt, cargo clippy --workspace --all-targets -D warnings, cargo test --workspace, cargo deny check, scripts/check-headers.sh, actionlint and shellcheck all pass.

Not tried locally: the Linux and Windows packages and the ARM runners. CI and the release-capi run on this pull request cover them.

crates/parsec-capi builds libparsec as a shared and a static library
over parsec-html's public API, with include/parsec.h generated by
cbindgen and checked in. Handles are opaque, nodes are plain numbers
checked against their document, strings come back either borrowed
(pointer and length) or as copies freed with parsec_string_free, and
every call is NULL-safe, returns a status code and runs inside
catch_unwind. Selectors compile once and run on any document, from any
thread.

package.sh lays out a package with the header, the libraries,
parsec.pc and a CMake config with parsec::parsec and
parsec::parsec_static targets.

tests/capi.c parses, selects, walks, edits and serializes, and tries
every error path. cargo test builds and runs it on Unix; CI's capi job
builds it through find_package on Linux, macOS (universal) and Windows,
shared and static; capi-checks runs it under ASan, LSan and UBSan
through pkg-config, checks the header matches cbindgen's output, and
compiles the header as C++.
release-capi.yml builds the C package for Linux x86-64 and ARM64
(on 22.04, for glibc 2.35), macOS universal, and Windows x86-64 and
ARM64, smoke-tests each with tests/capi.c through CMake, and archives
it. On a v* tag it waits for the release release.yml makes once the
tag's signature checks out, then attests the archives and attaches
them with a SHA256SUMS file and the provenance bundle. Pull requests
that touch the C API build and test all five packages.
Comment thread crates/parsec-capi/src/lib.rs Fixed
Comment thread crates/parsec-capi/src/lib.rs Fixed
is(f(&len), len, ...) leaves the order of its arguments to the
compiler. Clang works them out left to right, GCC and MSVC right to
left, so on Linux and Windows len was read before f wrote it. The
helpers now take the length's address and read it inside.
With CARGO_TERM_COLOR=always, as CI sets it, rustc's native-static-libs
note ends in an escape code, which package.sh copied onto the last
library in parsec.pc and the CMake config (-lc on Linux, -lm on macOS),
and the static link couldn't find it.
# Conflicts:
#	Cargo.lock
#	Cargo.toml
#	README.md
#	ROADMAP.md
parsed() and the selector's success path wrote through `out` relying on
a NULL check made earlier, in another function; CodeQL flagged both.
clear() now turns the checked pointer into a reference, and the writes
go through that.
@jamesconwaydev
jamesconwaydev merged commit d8c82a6 into main Nov 19, 2026
38 checks passed
@jamesconwaydev
jamesconwaydev deleted the capi branch November 19, 2026 02:38
@jamesconwaydev jamesconwaydev mentioned this pull request Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A guide for moving from Lexbor Prebuilt C libraries for Linux, macOS and Windows A C API with a generated header

2 participants