laminapdf_syntax/lib.rs
1//! The PDF syntax layer: the object model, its exact serialization, the lexer, the object
2//! parser and the stream filters (ISO 32000-2 §7.2–7.4).
3//!
4//! ```
5//! use laminapdf_syntax::{Object, parse_object};
6//!
7//! let obj = parse_object(b"<< /Kids [3 0 R] /Count 1 >>")?;
8//! assert_eq!(obj.as_dict().and_then(|d| d.get_int("Count")), Some(1));
9//! assert_eq!(obj.to_bytes(), b"<< /Kids [3 0 R] /Count 1 >>");
10//! # Ok::<(), laminapdf_syntax::Error>(())
11//! ```
12//!
13//! This crate is part of Lamina PDF's pure core: it takes bytes and returns values, and never
14//! touches the file system, network, clocks or threads, so it builds for native, WebAssembly
15//! and mobile targets alike. Every parse path returns [`Result`]; no input panics. Recursion
16//! only happens in the object parser, which is depth-limited ([`Limits::max_nesting_depth`]).
17//!
18//! * [`Object`] and friends: the object model and its byte-exact serialization.
19//! * [`parse_object`] / [`Parser`]: the tolerant object parser (indirect objects and
20//! streams, with an `endstream` scan when `/Length` is wrong).
21//! * [`Lexer`] / [`Token`]: the tokenizer the parser and the cross-reference reader share.
22//! * [`filters`]: Flate encode/decode within [`Limits`], and PNG predictors.
23//! * [`crypt`]: standard security handler decryption (read only): password authentication
24//! for R2–R6 and RC4 / AES-128 / AES-256 string and stream decryption.
25//! * [`ErrorKind`], [`Position`] and [`Location`]: the shared parts of the error contract
26//! (ADR 0026): kinds and tagged positions; [`ErrorCode`] and [`WarningCode`], every code's
27//! kind, hint and `docsUrl` (generated from `fixtures/codes.json`); and, with the `json`
28//! feature, `Diagnostic`, the §1 object every boundary serialises.
29//! * [`Limits`]: the resource limits of ADR 0003, shared by every layer that reads input.
30//! * [`Rotation`]: a rotation by a multiple of 90°, read from `/Rotate` values and shared by
31//! the page model and the renderers.
32//! * [`text`]: text strings (`PDFDocEncoding`, UTF-16BE, UTF-8) and the `PDFDocEncoding` table.
33//!
34//! The file structure (cross-reference tables, object streams, repair, the page tree) and the
35//! writer live one layer up, in `laminapdf-cos`.
36#![forbid(unsafe_code)]
37
38mod cancel;
39pub mod crypt;
40mod diagnostic;
41mod error;
42pub mod filters;
43mod job;
44/// Generated by `cargo xtask gen codes` from `fixtures/codes.json`; never edited by hand.
45mod generated {
46 #[rustfmt::skip]
47 pub(crate) mod codes;
48}
49mod lexer;
50mod limits;
51mod object;
52mod parser;
53#[cfg(feature = "json")]
54mod record;
55mod rotation;
56mod suggest;
57pub mod text;
58// What `fixtures/codes.json` lists, for the kind tests.
59#[cfg(test)]
60use laminapdf_test_fixtures::codes as test_codes;
61
62pub use cancel::CancelToken;
63pub use diagnostic::{ErrorKind, Location, Position};
64pub use error::{Error, Result};
65pub use generated::codes::{DOCS_URL_BASE, DOCS_URL_SUFFIX, ErrorCode, WarningCode};
66pub use job::{Budget, Phase, Step};
67pub use lexer::{Lexer, Token, is_delimiter, is_regular, is_whitespace};
68pub use limits::{DecodeBudget, Limit, Limits};
69pub use object::{Dict, Name, ObjRef, Object, PdfString, Stream, StringFormat, format_real};
70pub use parser::{
71 IndirectHead, IndirectObject, Parser, StreamHead, endstream_at, find, parse_object, parse_object_with,
72 stream_data_end,
73};
74#[cfg(feature = "json")]
75pub use record::Diagnostic;
76pub use rotation::Rotation;
77pub use suggest::did_you_mean;