use crate::object::ObjectId;
use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
NotPdf,
Syntax {
offset: usize,
message: &'static str,
},
Xref(String),
MissingObject(ObjectId),
Type {
expected: &'static str,
found: &'static str,
},
MissingKey(String),
Limit {
what: &'static str,
limit: u64,
},
Cycle(&'static str),
Filter {
filter: String,
message: String,
},
UnsupportedFilter(String),
Password,
UnsupportedEncryption(String),
Image(String),
Page(String),
Unsupported(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotPdf => f.write_str("not a PDF file"),
Self::Syntax { offset, message } => write!(f, "syntax error at byte {offset}: {message}"),
Self::Xref(m) => write!(f, "cross-reference: {m}"),
Self::MissingObject((n, g)) => write!(f, "object {n} {g} is not in the file"),
Self::Type { expected, found } => write!(f, "expected {expected}, found {found}"),
Self::MissingKey(k) => write!(f, "missing /{k}"),
Self::Limit { what, limit } => write!(f, "{what} exceeds the limit of {limit}"),
Self::Cycle(what) => write!(f, "reference cycle in {what}"),
Self::Filter { filter, message } => write!(f, "{filter}: {message}"),
Self::UnsupportedFilter(n) => write!(f, "filter {n} is not decoded here"),
Self::Password => f.write_str("the file is encrypted and the password is wrong"),
Self::UnsupportedEncryption(m) => write!(f, "unsupported encryption: {m}"),
Self::Image(m) => write!(f, "image: {m}"),
Self::Page(m) => write!(f, "page: {m}"),
Self::Unsupported(m) => write!(f, "unsupported: {m}"),
}
}
}
impl std::error::Error for Error {}
pub(crate) fn syntax<T>(offset: usize, message: &'static str) -> Result<T> {
Err(Error::Syntax { offset, message })
}