use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
Parse(ParseError),
Xref(XrefError),
DictKey(String),
ObjectNotFound(u32, u16),
Type {
expected: &'static str,
found: &'static str,
},
UnsupportedFilter(String),
Decompression(String),
NoOutline,
Io(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParseError {
InvalidFileHeader,
MissingStartXref,
InvalidXrefOffset(usize),
Unexpected {
offset: usize,
expected: &'static str,
},
InvalidObjectStream,
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum XrefError {
Start,
PrevStart,
StreamStart,
Generation,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Parse(p) => write!(f, "parse error: {p:?}"),
Self::Xref(x) => write!(f, "xref error: {x:?}"),
Self::DictKey(k) => write!(f, "missing required dictionary key \"{k}\""),
Self::ObjectNotFound(n, g) => write!(f, "object {n} {g} R not found in xref"),
Self::Type { expected, found } => {
write!(f, "type error: expected {expected}, got {found}")
}
Self::UnsupportedFilter(name) => write!(f, "unsupported PDF filter: {name}"),
Self::Decompression(msg) => write!(f, "decompression failed: {msg}"),
Self::NoOutline => write!(f, "no document outline"),
Self::Io(msg) => write!(f, "i/o error: {msg}"),
}
}
}
impl std::error::Error for Error {}
impl From<ParseError> for Error {
fn from(p: ParseError) -> Self {
Self::Parse(p)
}
}
impl From<XrefError> for Error {
fn from(x: XrefError) -> Self {
Self::Xref(x)
}
}