pdfluent_extract/error.rs
1//! Error types for `pdf-extract` operations.
2
3// Copyright (c) 2026 Innovation Trigger B.V.
4//
5// PDFluent is available under two licences, at your option: the GNU AGPLv3, or
6// the PDFluent Commercial Licence. See the LICENSE file in this repository --
7// that file travels with the copy you received, which a URL does not.
8
9use thiserror::Error;
10
11/// Errors returned by the `pdf-extract` crate while reading text or images
12/// from a PDF.
13///
14/// Customers usually see these wrapped inside `pdfluent::Error` after they
15/// bubble up through the facade. The inner variant tells the caller what
16/// kind of input prevented extraction.
17#[derive(Debug, Error)]
18pub enum ExtractError {
19 /// The underlying PDF byte stream could not be parsed.
20 ///
21 /// Typically a malformed cross-reference table, a truncated stream, or
22 /// an unsupported PDF construct. The wrapped `lopdf::Error` has the
23 /// detail; surface it to the user when reporting the problem.
24 #[error("PDF error: {0}")]
25 Pdf(#[from] lopdf::Error),
26
27 /// An I/O error occurred while reading the document or writing extracted
28 /// output (for image extraction targets).
29 #[error("IO error: {0}")]
30 Io(#[from] std::io::Error),
31
32 /// The caller asked for a page index that does not exist in the
33 /// document.
34 ///
35 /// Fields: `(requested_page, total_pages)`. Page numbers are 1-based at
36 /// the public API; this variant carries the same convention.
37 #[error("page {0} out of range (document has {1} pages)")]
38 PageOutOfRange(u32, u32),
39
40 /// The image embedded on the page uses a PDF stream filter that this
41 /// crate cannot decode (for example uncommon JPX profiles or unknown
42 /// custom filters). The wrapped string identifies the filter by its
43 /// PDF name.
44 #[error("unsupported image filter: {0}")]
45 UnsupportedFilter(String),
46
47 /// Image bytes were extracted but could not be decoded by the image
48 /// backend (corrupt JPEG/PNG/JPX payload, invalid colorspace, etc.).
49 #[error("image decode error: {0}")]
50 ImageDecode(String),
51
52 /// A non-categorised extraction failure. Reserved for cases the more
53 /// specific variants do not cover; the message describes the situation.
54 #[error("{0}")]
55 Other(String),
56}
57
58/// Convenience `Result` alias for fallible `pdf-extract` operations.
59pub type Result<T> = std::result::Result<T, ExtractError>;