// SPDX-FileCopyrightText: 2020 Robin Krahl // SPDX-License-Identifier: Apache-2.0 or MIT //! Error types for `genpdf`. use std::error; use std::fmt; use std::io; /// Helper trait for creating [`Error`][] instances. /// /// This trait is inspired by [`anyhow::Context`][]. /// /// [`Error`]: struct.Error.html /// [`anyhow::Context`]: https://docs.rs/anyhow/latest/anyhow/trait.Context.html pub trait Context { /// Maps the error to an [`Error`][] instance with the given message. /// /// [`Error`]: struct.Error.html fn context(self, msg: impl Into) -> Result; /// Maps the error to an [`Error`][] instance message produced by the given callback. /// /// [`Error`]: struct.Error.html fn with_context(self, cb: F) -> Result where F: Fn() -> S, S: Into; } impl> Context for Result { fn context(self, msg: impl Into) -> Result { self.map_err(|err| Error::new(msg, err)) } fn with_context(self, cb: F) -> Result where F: Fn() -> S, S: Into, { self.map_err(move |err| Error::new(cb(), err)) } } /// An error that occured in a `genpdf` function. /// /// The error consists of an error message (provided by the `Display` implementation) and an error /// kind, see [`kind`](#method.kind). #[derive(Debug)] pub struct Error { msg: String, kind: ErrorKind, } impl Error { /// Creates a new error. pub fn new(msg: impl Into, kind: impl Into) -> Error { Error { msg: msg.into(), kind: kind.into(), } } /// Returns the error kind for this error. pub fn kind(&self) -> &ErrorKind { &self.kind } } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(&self.msg) } } impl error::Error for Error { fn source(&self) -> Option<&(dyn error::Error + 'static)> { match &self.kind { ErrorKind::Internal => None, ErrorKind::InvalidData => None, ErrorKind::InvalidFont => None, ErrorKind::PageSizeExceeded => None, ErrorKind::UnsupportedEncoding => None, ErrorKind::IoError(err) => Some(err), ErrorKind::PdfError(err) => Some(err), ErrorKind::PdfIndexError(err) => Some(err), ErrorKind::RusttypeError(err) => Some(err), #[cfg(feature = "images")] ErrorKind::ImageError(err) => Some(err), } } } /// The kind of an [`Error`](struct.Error.html). #[derive(Debug)] #[non_exhaustive] pub enum ErrorKind { /// An internal error. Internal, /// An error caused by invalid data. InvalidData, /// An error caused by an invalid font. InvalidFont, /// An element exceeds the page size and could not be printed. PageSizeExceeded, /// A string with unsupported characters was used with a built-in font. UnsupportedEncoding, /// An IO error. IoError(io::Error), /// An error caused by invalid data in `printpdf`. PdfError(printpdf::PdfError), /// An error caused by an invalid index in `printpdf`. PdfIndexError(printpdf::IndexError), /// An error caused by `rusttype`. RusttypeError(rusttype::Error), /// An error caused by `image`. /// /// *Only available if the `images` feature is enabled.* #[cfg(feature = "images")] ImageError(image::ImageError), } impl From for ErrorKind { fn from(error: io::Error) -> ErrorKind { ErrorKind::IoError(error) } } impl From for ErrorKind { fn from(error: printpdf::Error) -> ErrorKind { match error { printpdf::Error::Io(err) => err.into(), printpdf::Error::Rusttype(err) => err.into(), printpdf::Error::Pdf(err) => err.into(), printpdf::Error::Index(err) => err.into(), } } } impl From for ErrorKind { fn from(error: printpdf::IndexError) -> ErrorKind { ErrorKind::PdfIndexError(error) } } impl From for ErrorKind { fn from(error: printpdf::PdfError) -> ErrorKind { ErrorKind::PdfError(error) } } impl From for ErrorKind { fn from(error: rusttype::Error) -> ErrorKind { ErrorKind::RusttypeError(error) } } #[cfg(feature = "images")] impl From for ErrorKind { fn from(error: image::ImageError) -> ErrorKind { ErrorKind::ImageError(error) } }