From b1cd459330b134bd8f13b73648f7b0fa3ebc3270 Mon Sep 17 00:00:00 2001 From: Alan Race Date: Tue, 22 Nov 2022 15:44:10 +0100 Subject: [PATCH] Updated error handling --- src/error.rs | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/error.rs b/src/error.rs index d191295..e302209 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,6 @@ use std::error::Error; use std::fmt; +use std::fmt::Display; use std::io; use std::str; use std::string; @@ -71,6 +72,7 @@ pub enum TiffFormatError { RequiredTagEmpty(Tag), StripTileTagConflict, CycleInOffsets, + JpegDecoder(JpegDecoderError), } impl fmt::Display for TiffFormatError { @@ -121,6 +123,7 @@ impl fmt::Display for TiffFormatError { RequiredTagEmpty(ref val) => write!(fmt, "Required tag {:?} was empty.", val), StripTileTagConflict => write!(fmt, "File should contain either (StripByteCounts and StripOffsets) or (TileByteCounts and TileOffsets), other combination was found."), CycleInOffsets => write!(fmt, "File contained a cycle in the list of IFDs"), + JpegDecoder(error) => write!(fmt, "{}", error), } } } @@ -324,20 +327,32 @@ impl From for TiffError { } } +#[derive(Debug, Clone, PartialEq)] +struct JpegDecoderError { + inner: jpeg::Error, +} + +impl From for JpegDecoderError { + fn from(error: jpeg::Error) -> Self { + Self { inner: error } + } +} + +impl Display for JpegDecoderError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.inner.fmt(f) + } +} + +impl From for TiffError { + fn from(error: JpegDecoderError) -> Self { + TiffError::FormatError(TiffFormatError::JpegDecoder(error)) + } +} + impl From for TiffError { fn from(error: jpeg::Error) -> Self { - match error { - jpeg::Error::Format(format) => { - TiffError::FormatError(TiffFormatError::Format(format!("JPEG format: {}", format))) - } - jpeg::Error::Unsupported(unsupported) => TiffError::UnsupportedError( - TiffUnsupportedError::UnsupportedJpegFeature(unsupported), - ), - jpeg::Error::Io(io_error) => TiffError::IoError(io_error), - jpeg::Error::Internal(internal) => { - panic!("According to jpeg create, this is not used, so this shouldn't be reached..") - } - } + JpegDecoderError::from(error).into() } }