unparser_shared/lib.rs
1//! Shared plumbing for the `un*` document extraction family — [unpdf], [undoc], [unhwp].
2//!
3//! [unpdf]: https://crates.io/crates/unpdf
4//! [undoc]: https://crates.io/crates/undoc
5//! [unhwp]: https://crates.io/crates/unhwp
6//!
7//! # What this crate is for
8//!
9//! Those three libraries have the same shape — parse a container, build an intermediate
10//! representation, render Markdown — and expose it the same way: a Rust API, a C ABI, and
11//! C#, Python and WebAssembly bindings over that ABI. The *document* parts differ
12//! entirely and belong in each library. The parts that do not differ live here: thread-local
13//! last-error storage, catching panics before they cross `extern "C"`, and the integer space
14//! the error classifications live in. VLM/AI-assisted extraction is also here now, as an
15//! opt-in feature — see [Modules](#modules) below.
16//!
17//! # Scope, and what is kept out
18//!
19//! - **No domain content.** No document model, no rendering, no format knowledge. If a
20//! type would mention a page, a paragraph or a spreadsheet cell, it belongs upstream.
21//! Facts about the *output* syntax are not domain content: where CommonMark lets an
22//! emphasis delimiter stand is the same answer for every writer ([`markdown`]).
23//! - **The crate root has no dependencies.** Three published cdylibs link it statically, so
24//! a dependency here is a dependency in all of them. Everything in [`ffi`], [`kind`], [`markdown`]
25//! and [`scaffold`] is `std`-only. Optional capabilities each bring their own dependency
26//! behind their own feature flag instead of widening this baseline.
27//!
28//! It is `std`-only at the root — [`ffi::LastErrorSlot`] holds a `CString` and lives in a
29//! `thread_local!` — which suits every consumer including `wasm32-unknown-unknown`
30//! (the `refine` feature does too; `ai` is native-only — see its module docs).
31//!
32//! # Modules
33//!
34//! - [`kind`] — the error-kind values the family already shares, and the bands that keep
35//! future ones from colliding. Read its docs before adding a kind anywhere.
36//! - [`ffi`] — [`ffi::LastErrorSlot`], the [`ffi::catch`] panic guard, and the macros that
37//! export the ABI over them.
38//! - [`markdown`] — CommonMark syntax facts the Markdown writers share:
39//! [`markdown::emphasis_span`], the part of a styled run emphasis delimiters can wrap;
40//! [`markdown::image`], [`markdown::link`] and [`markdown::link_destination`], which write a
41//! picture's description, a link's text and title, and a link's target so that they stay
42//! inside the construct and read back as given.
43//! - [`refine`] *(feature `refine`)* — a lossless, idempotent markdown shape-refinement pass:
44//! table shape normalization, ordered-list renumbering, link/image path normalization,
45//! frontmatter normalization, and section anchors. Composes with a renderer's own
46//! `cleanup` stage in either order.
47//! - [`ai`] *(feature `ai`, native only)* — VLM-based image understanding and
48//! AI-assisted markdown refine, active only when a caller supplies an API key.
49//! - [`scaffold`] — macros that assemble an entry point out of those primitives. Reach for
50//! these before writing the five steps by hand; the sentinel differs by return type and
51//! that is where hand-written entry points drift.
52//!
53//! # A minimal library
54//!
55//! ```
56//! use std::ffi::c_int;
57//! use unparser_shared::ffi::{self, FfiError, LastErrorSlot};
58//! use unparser_shared::kind;
59//!
60//! // The slot lives here, not in unparser-shared — that is what keeps it per-library. See
61//! // `ffi`'s module docs.
62//! thread_local! {
63//! static LAST_ERROR: LastErrorSlot = const { LastErrorSlot::new() };
64//! }
65//!
66//! unparser_shared::export_last_error_abi!(LAST_ERROR, mylib_last_error, mylib_last_error_kind);
67//!
68//! /// Returns the page count, or -1 on failure.
69//! #[no_mangle]
70//! pub extern "C" fn mylib_page_count(handle: *const u8) -> c_int {
71//! let result: Result<c_int, FfiError> = ffi::catch(|| {
72//! if handle.is_null() {
73//! return Err(ffi::invalid_argument("handle was null"));
74//! }
75//! Ok(7)
76//! });
77//!
78//! match result {
79//! Ok(count) => {
80//! LAST_ERROR.with(|slot| slot.clear());
81//! count
82//! }
83//! Err(error) => {
84//! LAST_ERROR.with(|slot| slot.set_error(&error));
85//! -1
86//! }
87//! }
88//! }
89//!
90//! assert_eq!(mylib_page_count(std::ptr::null()), -1);
91//! assert_eq!(mylib_last_error_kind(), kind::INVALID_ARGUMENT);
92//!
93//! let data = 0u8;
94//! assert_eq!(mylib_page_count(&data), 7);
95//! assert_eq!(mylib_last_error_kind(), kind::NONE);
96//! ```
97
98#![doc(html_root_url = "https://docs.rs/unparser-shared/0.5.0")]
99#![cfg_attr(
100 not(all(feature = "refine", feature = "ai")),
101 allow(rustdoc::broken_intra_doc_links)
102)]
103
104#[cfg(feature = "ai")]
105pub mod ai;
106pub mod ffi;
107pub mod kind;
108pub mod markdown;
109#[cfg(feature = "refine")]
110pub mod refine;
111pub mod scaffold;