pdf_redact/error.rs
1//! Error types for `pdf-redact` operations.
2
3use thiserror::Error;
4
5/// Errors returned by the `pdf-redact` crate while marking or applying
6/// content redaction.
7///
8/// Redaction is high-stakes (compliance, legal disclosure) so each variant
9/// describes a *refusal to proceed* rather than a best-effort fallback. If
10/// you receive any variant, the input is left untouched.
11#[derive(Debug, Error)]
12pub enum RedactError {
13 /// The underlying PDF byte stream could not be parsed.
14 ///
15 /// Typically a malformed cross-reference table, a truncated stream, or
16 /// an encrypted PDF that was not opened with the right password.
17 #[error("PDF error: {0}")]
18 Pdf(#[from] lopdf::Error),
19
20 /// An I/O error occurred while reading the source PDF or writing the
21 /// redacted output.
22 #[error("IO error: {0}")]
23 Io(#[from] std::io::Error),
24
25 /// The caller asked to redact a page index that does not exist in the
26 /// document.
27 ///
28 /// Fields: `(requested_page, total_pages)`. Page numbers are 1-based.
29 #[error("page {0} out of range (document has {1} pages)")]
30 PageOutOfRange(u32, u32),
31
32 /// The redaction call had no regions and no search query — nothing to
33 /// remove. Returned eagerly so callers do not silently produce
34 /// no-op output.
35 #[error("no redaction areas specified")]
36 NoAreas,
37
38 /// An image embedded inside a redaction region uses a PDF stream filter
39 /// that the redactor cannot rewrite (for example JBIG2 or some JPEG2000
40 /// profiles). The redaction is aborted because partially-stripped
41 /// images would leak content. The caller can either rasterise the page
42 /// before redacting or skip the affected page.
43 #[error("unsupported image filter: {0}")]
44 UnsupportedImageFilter(String),
45
46 /// A page touched by the redaction has a `/ToUnicode` CMap whose shape
47 /// the conservative `pdf-redact` parser cannot rewrite safely.
48 ///
49 /// The redactor refuses to silently drop the CMap because that would
50 /// break text-extraction tools downstream. The caller's options are
51 /// to rasterise the page first or to redact a different page range
52 /// that does not include this font.
53 #[error(
54 "unsupported /ToUnicode CMap on redacted page (font {font_resource_name}): \
55 redaction strip refuses to silently drop /ToUnicode; CMap shape not understood by the \
56 conservative pdf-redact parser ({reason})"
57 )]
58 UnsupportedToUnicodeCMap {
59 /// The PDF resource name of the font whose CMap is unsupported,
60 /// e.g. `"F1"`.
61 font_resource_name: String,
62 /// A human-readable reason for the parser's refusal, e.g.
63 /// `"surrogate pair without continuation"`.
64 reason: String,
65 },
66
67 /// A non-categorised redaction failure. Reserved for cases the more
68 /// specific variants do not cover; the message describes the situation.
69 #[error("{0}")]
70 Other(String),
71}
72
73/// Convenience `Result` alias for fallible `pdf-redact` operations.
74pub type Result<T> = std::result::Result<T, RedactError>;