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 { expected: &'static str, found: String },
24 #[error("PDF document is already encrypted")]
26 AlreadyEncrypted,
27 #[error("invalid character encoding")]
29 CharacterEncoding,
30 #[error("couldn't decompress stream {0}")]
32 Decompress(#[from] DecompressError),
33 #[error("couldn't parse input: {0}")]
35 Parse(#[from] ParseError),
36 #[error("decryption error: {0}")]
38 Decryption(#[from] encryption::DecryptionError),
39 #[error("missing required dictionary key \"{0}\"")]
41 DictKey(String),
42 #[error("invalid inline image: {0}")]
44 InvalidInlineImage(String),
45 #[error("invalid document outline: {0}")]
47 InvalidOutline(String),
48 #[error("invalid stream: {0}")]
50 InvalidStream(String),
51 #[error("invalid object stream: {0}")]
53 InvalidObjectStream(String),
54 #[error("invalid byte offset")]
56 InvalidOffset(usize),
57 #[error("IO error: {0}")]
59 IO(#[from] std::io::Error),
60 #[error("PDF document does not have an outline")]
63 NoOutline,
64 #[error("PDF document is not encrypted")]
66 NotEncrypted,
67 #[error("invalid password for encrypted PDF")]
69 InvalidPassword,
70 #[error("missing xref entry")]
72 MissingXrefEntry,
73 #[error("object ID {} {} not found", .0.0, .0.1)]
75 ObjectNotFound(ObjectId),
76 #[error("reference cycle with object ID {} {}", .0.0, .0.1)]
78 ReferenceCycle(ObjectId),
79 #[error("page number not found")]
81 PageNumberNotFound(u32),
82 #[error("numberic type cast failed: {0}")]
84 NumericCast(String),
85 #[error("dereferencing object reached limit, may indicate a reference cycle")]
88 ReferenceLimit,
89 #[error("decoding text string failed")]
91 TextStringDecode,
92 #[error("failed parsing cross reference table: {0}")]
94 Xref(XrefError),
95 #[error("invalid indirect object at byte offset {offset}")]
97 IndirectObject { offset: usize },
98 #[error("found object ID does not match expected object ID")]
100 ObjectIdMismatch,
101 #[cfg(feature = "embed_image")]
103 #[error("image error: {0}")]
104 Image(#[from] image::ImageError),
105 #[error("syntax error in content stream: {0}")]
107 Syntax(String),
108 #[error("failed parsing ToUnicode CMap: {0}")]
110 ToUnicodeCMap(#[from] UnicodeCMapError),
111 #[error("converting integer: {0}")]
112 TryFromInt(#[from] std::num::TryFromIntError),
113 #[error("unsupported security handler")]
115 UnsupportedSecurityHandler(Vec<u8>),
116}
117
118#[derive(Error, Debug)]
119pub enum DecompressError {
120 #[error("decoding ASCII85 failed: {0}")]
121 Ascii85(&'static str),
122}
123
124#[derive(Error, Debug)]
125pub enum ParseError {
126 #[error("unexpected end of input")]
127 EndOfInput,
128 #[error("invalid content stream")]
129 InvalidContentStream,
130 #[error("invalid file header")]
131 InvalidFileHeader,
132 #[error("invalid file trailer")]
133 InvalidTrailer,
134 #[error("invalid cross reference table")]
135 InvalidXref,
136}
137
138#[derive(Debug, Error)]
139pub enum XrefError {
140 #[error("invalid start value")]
142 Start,
143 #[error("invalid start value in Prev field")]
145 PrevStart,
146 #[error("invalid start value of XRefStm")]
148 StreamStart,
149}