1use thiserror::Error;
2
3use crate::encodings::cmap::UnicodeCMapError;
4use crate::{ObjectId, encryption};
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, Error)]
9pub enum Error {
10 #[error(
12 "missing feature of lopdf: {0}; please open an issue at https://github.com/J-F-Liu/lopdf/ to let the developers know of your usecase"
13 )]
14 Unimplemented(&'static str),
15
16 #[error("object has wrong type; expected type {expected} but found type {found}")]
18 ObjectType {
19 expected: &'static str,
20 found: &'static str,
21 },
22 #[error("dictionary has wrong type: ")]
23 DictType {
24 expected: &'static str,
25 found: String,
26 },
27 #[error("PDF document is already encrypted")]
29 AlreadyEncrypted,
30 #[error("invalid character encoding")]
32 CharacterEncoding,
33 #[error("couldn't decompress stream {0}")]
35 Decompress(#[from] DecompressError),
36 #[error("couldn't parse input: {0}")]
38 Parse(#[from] ParseError),
39 #[error("decryption error: {0}")]
41 Decryption(#[from] encryption::DecryptionError),
42 #[error("missing required dictionary key \"{0}\"")]
44 DictKey(String),
45 #[error("invalid inline image: {0}")]
47 InvalidInlineImage(String),
48 #[error("invalid document outline: {0}")]
50 InvalidOutline(String),
51 #[error("invalid stream: {0}")]
53 InvalidStream(String),
54 #[error("invalid object stream: {0}")]
56 InvalidObjectStream(String),
57 #[error("invalid byte offset")]
59 InvalidOffset(usize),
60 #[error("IO error: {0}")]
62 IO(#[from] std::io::Error),
63 #[error("PDF document does not have an outline")]
66 NoOutline,
67 #[error("PDF document is not encrypted")]
69 NotEncrypted,
70 #[error("invalid password for encrypted PDF")]
72 InvalidPassword,
73 #[error("missing xref entry")]
75 MissingXrefEntry,
76 #[error("object ID {} {} not found", .0.0, .0.1)]
78 ObjectNotFound(ObjectId),
79 #[error("reference cycle with object ID {} {}", .0.0, .0.1)]
81 ReferenceCycle(ObjectId),
82 #[error("page number not found")]
84 PageNumberNotFound(u32),
85 #[error("numberic type cast failed: {0}")]
87 NumericCast(String),
88 #[error("dereferencing object reached limit, may indicate a reference cycle")]
91 ReferenceLimit,
92 #[error("decoding text string failed")]
94 TextStringDecode,
95 #[error("failed parsing cross reference table: {0}")]
97 Xref(XrefError),
98 #[error("invalid indirect object at byte offset {offset}")]
100 IndirectObject { offset: usize },
101 #[error("found object ID does not match expected object ID")]
103 ObjectIdMismatch,
104 #[cfg(feature = "embed_image")]
106 #[error("image error: {0}")]
107 Image(#[from] image::ImageError),
108 #[error("syntax error in content stream: {0}")]
110 Syntax(String),
111 #[error("failed parsing ToUnicode CMap: {0}")]
113 ToUnicodeCMap(#[from] UnicodeCMapError),
114 #[error("converting integer: {0}")]
115 TryFromInt(#[from] std::num::TryFromIntError),
116 #[error("unsupported security handler")]
118 UnsupportedSecurityHandler(Vec<u8>),
119 #[error("document too large: {size} bytes exceeds limit of {limit} bytes")]
124 DocumentTooLarge { size: usize, limit: usize },
125 #[error("failed to decompress ObjStm object {container_id}")]
127 ObjStmDecompress { container_id: u32 },
128 #[error("decompressed stream too large: exceeds {limit} bytes")]
133 StreamTooLarge { limit: usize },
134}
135
136#[derive(Error, Debug)]
137pub enum DecompressError {
138 #[error("decoding ASCII85 failed: {0}")]
139 Ascii85(&'static str),
140 #[error("decoding ASCIIHex failed: {0}")]
141 AsciiHex(&'static str),
142}
143
144#[derive(Error, Debug)]
145pub enum ParseError {
146 #[error("unexpected end of input")]
147 EndOfInput,
148 #[error("invalid content stream")]
149 InvalidContentStream,
150 #[error("invalid file header")]
151 InvalidFileHeader,
152 #[error("invalid file trailer")]
153 InvalidTrailer,
154 #[error("invalid cross reference table")]
155 InvalidXref,
156}
157
158#[derive(Debug, Error)]
159pub enum XrefError {
160 #[error("invalid start value")]
162 Start,
163 #[error("invalid start value in Prev field")]
165 PrevStart,
166 #[error("invalid start value of XRefStm")]
168 StreamStart,
169}