pdfox 0.1.0

A pure-Rust PDF library — create, parse, and render PDF documents with zero C dependencies
Documentation
/// Unified error type for all pdfox operations.

use std::fmt;

#[derive(Debug)]
pub enum PdfError {
    // ── I/O ──────────────────────────────────────────────────────────────────
    Io(std::io::Error),

    // ── Parse errors ─────────────────────────────────────────────────────────
    InvalidSignature,
    MalformedXRef(String),
    MalformedObject(String),
    UnexpectedEof,
    UnsupportedFeature(String),
    InvalidEncoding(String),

    // ── Build errors ─────────────────────────────────────────────────────────
    /// Tried to use a font key that was never registered on this page
    UnregisteredFont(String),
    /// Image data is invalid or corrupted
    InvalidImage(String),
    /// A table was built with inconsistent column/cell counts
    TableStructureError(String),
    /// Attempted to write to an already-finalized document
    AlreadyFinalized,

    // ── Generic ───────────────────────────────────────────────────────────────
    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>;