rusty_pdf_core 0.1.0

Pure-Rust PDF reader core: objects, classic and stream cross-reference tables, object streams, incremental updates, broken-xref repair, RC4 and AES-128/256 decryption, stream filters, page tree, content streams, editing and save. Hostile input errors or stays bounded. No C, no unsafe.
Documentation
//! The error type.

use crate::object::ObjectId;
use std::fmt;

/// `Result` with this crate's [`Error`].
pub type Result<T> = std::result::Result<T, Error>;

/// Why a PDF could not be read, decoded or written.
///
/// Malformed and hostile input always ends here (or in a bounded result),
/// never in a panic.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
    /// No `%PDF-` header and no recognisable object: this is not a PDF.
    NotPdf,
    /// The bytes at `offset` are not what the grammar allows there.
    Syntax {
        /// Byte offset in the buffer being parsed.
        offset: usize,
        /// What was expected or what went wrong.
        message: &'static str,
    },
    /// The cross-reference data is unusable and could not be rebuilt.
    Xref(String),
    /// The object is not in the file.
    MissingObject(ObjectId),
    /// An object has a different type from the one the caller asked for.
    Type {
        /// The type asked for.
        expected: &'static str,
        /// The type found.
        found: &'static str,
    },
    /// A dictionary lacks a required key.
    MissingKey(String),
    /// A limit from [`Limits`](crate::Limits) or from the caller was reached.
    Limit {
        /// What was being bounded.
        what: &'static str,
        /// The bound.
        limit: u64,
    },
    /// References form a loop where the format requires a tree or a chain.
    Cycle(&'static str),
    /// A stream filter failed.
    Filter {
        /// The filter's PDF name.
        filter: String,
        /// What went wrong.
        message: String,
    },
    /// The stream uses a filter this build does not decode in this call
    /// (an image filter asked for as plain bytes, or a disabled feature).
    UnsupportedFilter(String),
    /// The file is encrypted and the password given (or the empty one) is wrong.
    Password,
    /// The file is encrypted in a way this crate does not handle.
    UnsupportedEncryption(String),
    /// An image could not be turned into samples.
    Image(String),
    /// The page structure is unusable.
    Page(String),
    /// A valid construct this crate does not support.
    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 })
}