Skip to main content

karet_pdf/
error.rs

1//! The error type surfaced by PDF loading and rendering.
2
3use hayro::hayro_syntax::LoadPdfError;
4
5/// An error loading or rasterizing a PDF document.
6#[derive(Debug, thiserror::Error)]
7#[non_exhaustive]
8pub enum PdfError {
9    /// The bytes could not be parsed as a PDF document.
10    #[error("could not parse PDF document")]
11    Parse,
12    /// The document is encrypted / password-protected, which is not supported.
13    #[error("PDF document is encrypted (password-protected)")]
14    Encrypted,
15    /// The requested page index is out of range for the document.
16    #[error("page {index} is out of range (document has {count} page(s))")]
17    PageOutOfRange {
18        /// The requested 0-based page index.
19        index: usize,
20        /// The number of pages the document actually has.
21        count: usize,
22    },
23}
24
25impl PdfError {
26    /// Map a `hayro` load error into a [`PdfError`].
27    pub(crate) fn from_load(err: LoadPdfError) -> Self {
28        match err {
29            LoadPdfError::Decryption(_) => Self::Encrypted,
30            LoadPdfError::Invalid => Self::Parse,
31        }
32    }
33}