Skip to main content

pdfmuse_core/
error.rs

1//! Structured error type.
2//!
3//! Errors are split into two paths, per the "graceful degradation" principle:
4//!
5//! - **Fatal** — the document cannot be parsed at all (bad container, wrong
6//!   password, structurally broken). These return `Err(PdfmuseError)`.
7//! - **Degradable** — a single page/object is damaged, a font lacks a CMap, a
8//!   page needs OCR, etc. These do **not** error; they are recorded in
9//!   [`crate::ir::Document::warnings`] and parsing continues.
10//!
11//! The core never `panic!`s on malformed input — every failure surfaces as one
12//! of these two. Bindings map `PdfmuseError` onto each language's exception type.
13
14use thiserror::Error;
15
16/// Convenience alias used throughout the crate and by the public API.
17pub type Result<T> = std::result::Result<T, PdfmuseError>;
18
19/// A fatal parsing error. Non-fatal degradations use
20/// [`crate::ir::Warning`] instead.
21#[derive(Error, Debug)]
22pub enum PdfmuseError {
23    /// The bytes are not a recognized/supported document container.
24    #[error("unrecognized or unsupported document format")]
25    InvalidFormat,
26
27    /// The format is recognized but not yet implemented (e.g. DOCX before M3).
28    #[error("{0} is recognized but not yet supported")]
29    Unsupported(String),
30
31    /// The document is encrypted and no usable password was supplied.
32    /// Password support lands in PER-50; the password is never logged.
33    #[error("document is encrypted and requires a password")]
34    EncryptedNoPassword,
35
36    /// The document is structurally broken beyond recovery.
37    #[error("malformed document: {0}")]
38    Malformed(String),
39
40    /// An I/O failure (for future `Read`-based entry points).
41    #[error(transparent)]
42    Io(#[from] std::io::Error),
43}