use std::fmt;
#[derive(Debug)]
pub enum PdfError {
Io(std::io::Error),
InvalidSignature,
MalformedXRef(String),
MalformedObject(String),
UnexpectedEof,
UnsupportedFeature(String),
InvalidEncoding(String),
UnregisteredFont(String),
InvalidImage(String),
TableStructureError(String),
AlreadyFinalized,
Custom(String),
}
impl fmt::Display for PdfError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PdfError::Io(e) => write!(f, "I/O error: {}", e),
PdfError::InvalidSignature => write!(f, "Not a valid PDF file (bad signature)"),
PdfError::MalformedXRef(s) => write!(f, "Malformed cross-reference table: {}", s),
PdfError::MalformedObject(s) => write!(f, "Malformed PDF object: {}", s),
PdfError::UnexpectedEof => write!(f, "Unexpected end of file"),
PdfError::UnsupportedFeature(s) => write!(f, "Unsupported PDF feature: {}", s),
PdfError::InvalidEncoding(s) => write!(f, "Invalid text encoding: {}", s),
PdfError::UnregisteredFont(k) => write!(f, "Font key '{}' was never registered on this page", k),
PdfError::InvalidImage(s) => write!(f, "Invalid image data: {}", s),
PdfError::TableStructureError(s) => write!(f, "Table structure error: {}", s),
PdfError::AlreadyFinalized => write!(f, "Document has already been finalized"),
PdfError::Custom(s) => write!(f, "{}", s),
}
}
}
impl std::error::Error for PdfError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
if let PdfError::Io(e) = self { Some(e) } else { None }
}
}
impl From<std::io::Error> for PdfError {
fn from(e: std::io::Error) -> Self {
PdfError::Io(e)
}
}
impl From<String> for PdfError {
fn from(s: String) -> Self {
PdfError::Custom(s)
}
}
impl From<&str> for PdfError {
fn from(s: &str) -> Self {
PdfError::Custom(s.to_string())
}
}
pub type PdfResult<T> = Result<T, PdfError>;