Skip to main content

pdfrum_render/
error.rs

1//! What can go wrong badly enough to abandon a render.
2//!
3//! Almost nothing does. Rendering a damaged page is the normal case, and
4//! every recovery — an undrawable matrix, a shading whose function will not
5//! evaluate, a pattern whose steps are degenerate — is a skipped object and a
6//! [`Diagnostic`](pdfrum_common::Diagnostic), not an error. `Err` is reserved
7//! for "there is no output at all".
8
9/// A render that could not produce a pixmap.
10#[derive(Debug, thiserror::Error, PartialEq, Eq, Clone)]
11#[non_exhaustive]
12pub enum Error {
13    /// The requested target exceeds what a backend can allocate.
14    ///
15    /// `vello_cpu` sizes every scene, pixmap and mask with `u16`, so 65535 is
16    /// the hard ceiling in either axis; the bound is enforced in the engine so
17    /// both backends agree rather than one of them failing later.
18    #[error("render target {width}x{height} exceeds the {limit}px backend limit")]
19    TargetTooLarge {
20        /// The requested width in pixels.
21        width: u32,
22        /// The requested height in pixels.
23        height: u32,
24        /// The largest dimension a backend accepts.
25        limit: u32,
26    },
27
28    /// The requested target has a zero axis, so there is nothing to render
29    /// into.
30    #[error("render target {width}x{height} is empty")]
31    TargetEmpty {
32        /// The requested width in pixels.
33        width: u32,
34        /// The requested height in pixels.
35        height: u32,
36    },
37    /// [`RenderSession::deadline`](crate::RenderSession::deadline) passed:
38    /// before the target was allocated, or during the walk, which stops at
39    /// the next object and reports here rather than handing back a page with
40    /// the rest missing.
41    #[error(transparent)]
42    Limit(pdfrum_common::LimitExceeded),
43    /// A PNG could not be encoded — only with the `png` feature. The
44    /// encoder's own message, since its error type is neither comparable nor
45    /// cloneable and this one is both.
46    #[cfg(feature = "png")]
47    #[error("PNG encoding failed: {0}")]
48    Png(String),
49    /// A file could not be written — only with the `png` feature; the I/O
50    /// error's message.
51    #[cfg(feature = "png")]
52    #[error("writing the file failed: {0}")]
53    Io(String),
54}
55
56#[cfg(feature = "png")]
57impl From<png::EncodingError> for Error {
58    fn from(error: png::EncodingError) -> Self {
59        Error::Png(error.to_string())
60    }
61}
62
63#[cfg(feature = "png")]
64impl From<std::io::Error> for Error {
65    fn from(error: std::io::Error) -> Self {
66        Error::Io(error.to_string())
67    }
68}