Skip to main content

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//! - [`refine`] *(feature `refine`)* — a lossless, idempotent markdown shape-refinement pass:
41//!   table shape normalization, ordered-list renumbering, link/image path normalization,
42//!   frontmatter normalization, and section anchors. Composes with a renderer's own
43//!   `cleanup` stage in either order.
44//! - [`ai`] *(feature `ai`, native only)* — VLM-based image understanding and
45//!   AI-assisted markdown refine, active only when a caller supplies an API key.
46//! - [`scaffold`] — macros that assemble an entry point out of those primitives. Reach for
47//!   these before writing the five steps by hand; the sentinel differs by return type and
48//!   that is where hand-written entry points drift.
49//!
50//! # A minimal library
51//!
52//! ```
53//! use std::ffi::c_int;
54//! use unparser_shared::ffi::{self, FfiError, LastErrorSlot};
55//! use unparser_shared::kind;
56//!
57//! // The slot lives here, not in unparser-shared — that is what keeps it per-library. See
58//! // `ffi`'s module docs.
59//! thread_local! {
60//!     static LAST_ERROR: LastErrorSlot = const { LastErrorSlot::new() };
61//! }
62//!
63//! unparser_shared::export_last_error_abi!(LAST_ERROR, mylib_last_error, mylib_last_error_kind);
64//!
65//! /// Returns the page count, or -1 on failure.
66//! #[no_mangle]
67//! pub extern "C" fn mylib_page_count(handle: *const u8) -> c_int {
68//!     let result: Result<c_int, FfiError> = ffi::catch(|| {
69//!         if handle.is_null() {
70//!             return Err(ffi::invalid_argument("handle was null"));
71//!         }
72//!         Ok(7)
73//!     });
74//!
75//!     match result {
76//!         Ok(count) => {
77//!             LAST_ERROR.with(|slot| slot.clear());
78//!             count
79//!         }
80//!         Err(error) => {
81//!             LAST_ERROR.with(|slot| slot.set_error(&error));
82//!             -1
83//!         }
84//!     }
85//! }
86//!
87//! assert_eq!(mylib_page_count(std::ptr::null()), -1);
88//! assert_eq!(mylib_last_error_kind(), kind::INVALID_ARGUMENT);
89//!
90//! let data = 0u8;
91//! assert_eq!(mylib_page_count(&data), 7);
92//! assert_eq!(mylib_last_error_kind(), kind::NONE);
93//! ```
94
95#![doc(html_root_url = "https://docs.rs/unparser-shared/0.3.0")]
96#![cfg_attr(
97    not(all(feature = "refine", feature = "ai")),
98    allow(rustdoc::broken_intra_doc_links)
99)]
100
101#[cfg(feature = "ai")]
102pub mod ai;
103pub mod ffi;
104pub mod kind;
105pub mod markdown;
106#[cfg(feature = "refine")]
107pub mod refine;
108pub mod scaffold;