Skip to main content

oxideav_pdf/
error.rs

1//! Crate-local error type.
2//!
3//! Internal modules return [`PdfError`]; the public surface (top-level
4//! `write_pdf`, the [`oxideav_core::Encoder`] impl) converts to
5//! [`oxideav_core::Error`] at the boundary.
6
7use std::io;
8
9/// Errors that can arise while building a PDF.
10#[derive(Debug)]
11pub enum PdfError {
12    /// I/O error while serialising. Currently only produced by the
13    /// `Vec<u8>`-backed writer when something pathological happens to
14    /// the underlying allocator.
15    Io(io::Error),
16    /// Catch-all for assembly-time problems (missing /Root, malformed
17    /// gradient, etc.). Carries a static or owned string message.
18    Other(String),
19}
20
21impl PdfError {
22    pub fn other(msg: impl Into<String>) -> Self {
23        Self::Other(msg.into())
24    }
25}
26
27impl std::fmt::Display for PdfError {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        match self {
30            Self::Io(e) => write!(f, "PDF I/O error: {e}"),
31            Self::Other(s) => write!(f, "PDF error: {s}"),
32        }
33    }
34}
35
36impl std::error::Error for PdfError {}
37
38impl From<io::Error> for PdfError {
39    fn from(e: io::Error) -> Self {
40        Self::Io(e)
41    }
42}
43
44impl From<PdfError> for oxideav_core::Error {
45    fn from(e: PdfError) -> Self {
46        match e {
47            PdfError::Io(io) => oxideav_core::Error::Io(io),
48            PdfError::Other(msg) => oxideav_core::Error::invalid(msg),
49        }
50    }
51}