From 22ea09d9c1fd342dcee13d153fedaf49a62db044 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 14 Mar 2019 16:26:06 +0100 Subject: =?UTF-8?q?Better=20error=20handling=20=F0=9F=8C=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pdf.rs | 59 +++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 20 deletions(-) (limited to 'src/pdf.rs') diff --git a/src/pdf.rs b/src/pdf.rs index 4cd78d0a..b188ab91 100644 --- a/src/pdf.rs +++ b/src/pdf.rs @@ -4,12 +4,12 @@ use std::collections::HashSet; use std::error; use std::fmt; use std::io::{self, Write}; -use pdf::{PdfWriter, Reference, Rect, Version, Trailer}; -use pdf::{DocumentCatalog, PageTree, Page, Resource, Text, Content}; -use pdf::font::{Type0Font, CMapEncoding, CIDFont, CIDFontType, CIDSystemInfo, - WidthRecord, FontDescriptor, FontFlags, EmbeddedFont, GlyphUnit}; +use pdf::{PdfWriter, Reference, Rect, Version, Trailer, DocumentCatalog}; +use pdf::{PageTree, Page, Resource, Text, Content}; +use pdf::font::{Type0Font, CMapEncoding, CIDFont, CIDFontType, CIDSystemInfo}; +use pdf::font::{WidthRecord, FontDescriptor, FontFlags, EmbeddedFont, GlyphUnit}; use crate::doc::{Document, Size, Text as DocText, TextCommand as DocTextCommand}; -use crate::font::Font; +use crate::font::{Font, FontError}; /// Writes documents in the _PDF_ format. @@ -279,34 +279,53 @@ impl std::ops::Deref for PdfFont { } /// Result type used for parsing. -type PdfResult = std::result::Result; +type PdfResult = std::result::Result; /// The error type for _PDF_ creation. -#[derive(Debug, Clone, Eq, PartialEq)] -pub struct PdfWritingError { - /// A message describing the error. - message: String, +pub enum PdfError { + /// An error occured while subsetting the font for the _PDF_. + Font(FontError), + /// An I/O Error on the underlying writable occured. + Io(io::Error), } -impl error::Error for PdfWritingError {} - -impl From for PdfWritingError { +impl error::Error for PdfError { #[inline] - fn from(err: io::Error) -> PdfWritingError { - PdfWritingError { message: format!("{}", err) } + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + match self { + PdfError::Font(err) => Some(err), + PdfError::Io(err) => Some(err), + } } } -impl From for PdfWritingError { +impl fmt::Display for PdfError { #[inline] - fn from(err: crate::font::FontError) -> PdfWritingError { - PdfWritingError { message: format!("{}", err) } + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + PdfError::Font(err) => write!(f, "font error: {}", err), + PdfError::Io(err) => write!(f, "io error: {}", err), + } } } -impl fmt::Display for PdfWritingError { +impl fmt::Debug for PdfError { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str(&self.message) + fmt::Display::fmt(self, f) + } +} + +impl From for PdfError { + #[inline] + fn from(err: io::Error) -> PdfError { + PdfError::Io(err) + } +} + +impl From for PdfError { + #[inline] + fn from(err: FontError) -> PdfError { + PdfError::Font(err) } } -- cgit v1.2.3