1use thiserror::Error;
2
3use crate::encodings::cmap::UnicodeCMapError;
4use crate::{encryption, ObjectId};
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, Error)]
9pub enum Error {
10    #[error("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")]
12    Unimplemented(&'static str),
13
14    #[error("object has wrong type; expected type {expected} but found type {found}")]
16    ObjectType {
17        expected: &'static str,
18        found: &'static str,
19    },
20    #[error("dictionary has wrong type: ")]
21    DictType { expected: &'static str, found: String },
22    #[error("PDF document is already encrypted")]
24    AlreadyEncrypted,
25    #[error("invalid character encoding")]
27    CharacterEncoding,
28    #[error("couldn't decompress stream {0}")]
30    Decompress(#[from] DecompressError),
31    #[error("couldn't parse input: {0}")]
33    Parse(#[from] ParseError),
34    #[error("decryption error: {0}")]
36    Decryption(#[from] encryption::DecryptionError),
37    #[error("missing required dictionary key \"{0}\"")]
39    DictKey(String),
40    #[error("invalid inline image: {0}")]
42    InvalidInlineImage(String),
43    #[error("invalid document outline: {0}")]
45    InvalidOutline(String),
46    #[error("invalid stream: {0}")]
48    InvalidStream(String),
49    #[error("invalid object stream: {0}")]
51    InvalidObjectStream(String),
52    #[error("invalid byte offset")]
54    InvalidOffset(usize),
55    #[error("IO error: {0}")]
57    IO(#[from] std::io::Error),
58    #[error("PDF document does not have an outline")]
61    NoOutline,
62    #[error("PDF document is not encrypted")]
64    NotEncrypted,
65    #[error("missing xref entry")]
67    MissingXrefEntry,
68    #[error("object ID {} {} not found", .0.0, .0.1)]
70    ObjectNotFound(ObjectId),
71    #[error("reference cycle with object ID {} {}", .0.0, .0.1)]
73    ReferenceCycle(ObjectId),
74    #[error("page number not found")]
76    PageNumberNotFound(u32),
77    #[error("numberic type cast failed: {0}")]
79    NumericCast(String),
80    #[error("dereferencing object reached limit, may indicate a reference cycle")]
83    ReferenceLimit,
84    #[error("decoding text string failed")]
86    TextStringDecode,
87    #[error("failed parsing cross reference table: {0}")]
89    Xref(XrefError),
90    #[error("invalid indirect object at byte offset {offset}")]
92    IndirectObject { offset: usize },
93    #[error("found object ID does not match expected object ID")]
95    ObjectIdMismatch,
96    #[cfg(feature = "embed_image")]
98    #[error("image error: {0}")]
99    Image(#[from] image::ImageError),
100    #[error("syntax error in content stream: {0}")]
102    Syntax(String),
103    #[error("failed parsing ToUnicode CMap: {0}")]
105    ToUnicodeCMap(#[from] UnicodeCMapError),
106    #[error("converting integer: {0}")]
107    TryFromInt(#[from] std::num::TryFromIntError),
108    #[error("unsupported security handler")]
110    UnsupportedSecurityHandler(Vec<u8>),
111}
112
113#[derive(Error, Debug)]
114pub enum DecompressError {
115    #[error("decoding ASCII85 failed: {0}")]
116    Ascii85(&'static str),
117}
118
119#[derive(Error, Debug)]
120pub enum ParseError {
121    #[error("unexpected end of input")]
122    EndOfInput,
123    #[error("invalid content stream")]
124    InvalidContentStream,
125    #[error("invalid file header")]
126    InvalidFileHeader,
127    #[error("invalid file trailer")]
128    InvalidTrailer,
129    #[error("invalid cross reference table")]
130    InvalidXref,
131}
132
133#[derive(Debug, Error)]
134pub enum XrefError {
135    #[error("invalid start value")]
137    Start,
138    #[error("invalid start value in Prev field")]
140    PrevStart,
141    #[error("invalid start value of XRefStm")]
143    StreamStart,
144}