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 /// The requested region does not lie inside the page's device box.
38 #[error(
39 "render region {width}x{height} at ({x}, {y}) is outside the page's {page_width}x{page_height} device box"
40 )]
41 RegionOutOfBounds {
42 /// The region's left edge.
43 x: u32,
44 /// The region's top edge.
45 y: u32,
46 /// The region's width.
47 width: u32,
48 /// The region's height.
49 height: u32,
50 /// The page's full device width under the render transform.
51 page_width: u32,
52 /// The page's full device height under the render transform.
53 page_height: u32,
54 },
55 /// This renderer cannot draw a part of the page; ask for the whole of it.
56 #[error("this renderer cannot draw a region of the page, only the whole page")]
57 RegionUnsupported,
58 /// [`RenderSession::deadline`](crate::RenderSession::deadline) or
59 /// [`RenderSession::cancel`](crate::RenderSession::cancel) passed:
60 /// before the target was allocated, or during the walk, which stops at
61 /// the next object and reports here rather than handing back a page with
62 /// the rest missing.
63 #[error(transparent)]
64 Limit(pdfrum_common::LimitExceeded),
65 /// A PNG could not be encoded — only with the `png` feature. The
66 /// encoder's own message, since its error type is neither comparable nor
67 /// cloneable and this one is both.
68 #[cfg(feature = "png")]
69 #[error("PNG encoding failed: {0}")]
70 Png(String),
71 /// A file could not be written — only with the `png` feature; the I/O
72 /// error's message.
73 #[cfg(feature = "png")]
74 #[error("writing the file failed: {0}")]
75 Io(String),
76}
77
78#[cfg(feature = "png")]
79impl From<png::EncodingError> for Error {
80 fn from(error: png::EncodingError) -> Self {
81 Error::Png(error.to_string())
82 }
83}
84
85#[cfg(feature = "png")]
86impl From<std::io::Error> for Error {
87 fn from(error: std::io::Error) -> Self {
88 Error::Io(error.to_string())
89 }
90}