use thiserror::Error;
use crate::encodings::cmap::UnicodeCMapError;
use crate::{encryption, ObjectId};
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Error)]
pub enum Error {
#[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")]
Unimplemented(&'static str),
#[error("object has wrong type; expected type {expected} but found type {found}")]
ObjectType {
expected: &'static str,
found: &'static str,
},
#[error("dictionary has wrong type: ")]
DictType { expected: &'static str, found: String },
#[error("PDF document is already encrypted")]
AlreadyEncrypted,
#[error("invalid character encoding")]
CharacterEncoding,
#[error("couldn't decompress stream {0}")]
Decompress(#[from] DecompressError),
#[error("couldn't parse input: {0}")]
Parse(#[from] ParseError),
#[error("decryption error: {0}")]
Decryption(#[from] encryption::DecryptionError),
#[error("missing required dictionary key \"{0}\"")]
DictKey(String),
#[error("invalid inline image: {0}")]
InvalidInlineImage(String),
#[error("invalid document outline: {0}")]
InvalidOutline(String),
#[error("invalid stream: {0}")]
InvalidStream(String),
#[error("invalid object stream: {0}")]
InvalidObjectStream(String),
#[error("invalid byte offset")]
InvalidOffset(usize),
#[error("IO error: {0}")]
IO(#[from] std::io::Error),
#[error("PDF document does not have an outline")]
NoOutline,
#[error("PDF document is not encrypted")]
NotEncrypted,
#[error("invalid password for encrypted PDF")]
InvalidPassword,
#[error("missing xref entry")]
MissingXrefEntry,
#[error("object ID {} {} not found", .0.0, .0.1)]
ObjectNotFound(ObjectId),
#[error("reference cycle with object ID {} {}", .0.0, .0.1)]
ReferenceCycle(ObjectId),
#[error("page number not found")]
PageNumberNotFound(u32),
#[error("numberic type cast failed: {0}")]
NumericCast(String),
#[error("dereferencing object reached limit, may indicate a reference cycle")]
ReferenceLimit,
#[error("decoding text string failed")]
TextStringDecode,
#[error("failed parsing cross reference table: {0}")]
Xref(XrefError),
#[error("invalid indirect object at byte offset {offset}")]
IndirectObject { offset: usize },
#[error("found object ID does not match expected object ID")]
ObjectIdMismatch,
#[cfg(feature = "embed_image")]
#[error("image error: {0}")]
Image(#[from] image::ImageError),
#[error("syntax error in content stream: {0}")]
Syntax(String),
#[error("failed parsing ToUnicode CMap: {0}")]
ToUnicodeCMap(#[from] UnicodeCMapError),
#[error("converting integer: {0}")]
TryFromInt(#[from] std::num::TryFromIntError),
#[error("unsupported security handler")]
UnsupportedSecurityHandler(Vec<u8>),
}
#[derive(Error, Debug)]
pub enum DecompressError {
#[error("decoding ASCII85 failed: {0}")]
Ascii85(&'static str),
}
#[derive(Error, Debug)]
pub enum ParseError {
#[error("unexpected end of input")]
EndOfInput,
#[error("invalid content stream")]
InvalidContentStream,
#[error("invalid file header")]
InvalidFileHeader,
#[error("invalid file trailer")]
InvalidTrailer,
#[error("invalid cross reference table")]
InvalidXref,
}
#[derive(Debug, Error)]
pub enum XrefError {
#[error("invalid start value")]
Start,
#[error("invalid start value in Prev field")]
PrevStart,
#[error("invalid start value of XRefStm")]
StreamStart,
}