From 2c6ca5459a71022ca47db90f8acdf842485ac199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Thu, 13 Sep 2018 13:20:50 +0200 Subject: [PATCH] Migrate from String to properly typed TiffFormatError --- src/decoder/ifd.rs | 8 +++----- src/decoder/mod.rs | 22 +++++++--------------- src/error.rs | 28 +++++++++++++++++++++++++++- src/lib.rs | 2 +- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/decoder/ifd.rs b/src/decoder/ifd.rs index b349e64..81eeafd 100644 --- a/src/decoder/ifd.rs +++ b/src/decoder/ifd.rs @@ -4,7 +4,7 @@ use std::io::{self, Read, Seek}; use std::collections::{HashMap}; use super::stream::{ByteOrder, SmartReader, EndianReader}; -use ::{TiffError, TiffResult}; +use ::{TiffError, TiffFormatError, TiffResult}; use self::Value::{Unsigned, List, Rational}; @@ -86,7 +86,7 @@ pub enum Type { #[allow(unused_qualifications)] -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum Value { //Signed(i32), Unsigned(u32), @@ -98,9 +98,7 @@ impl Value { pub fn into_u32(self) -> TiffResult { match self { Unsigned(val) => Ok(val), - val => Err(TiffError::FormatError(format!( - "Expected unsigned integer, {:?} found.", val - ))) + val => Err(TiffError::FormatError(TiffFormatError::UnsignedIntegerExpected(val))), } } pub fn into_u32_vec(self) -> TiffResult> { diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index 045944a..c06824a 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -3,7 +3,7 @@ use std::mem; use num_traits::{FromPrimitive, Num}; use std::collections::HashMap; -use ::{ColorType, TiffError, TiffResult}; +use ::{ColorType, TiffError, TiffFormatError, TiffResult}; use self::ifd::Directory; @@ -187,12 +187,10 @@ impl Decoder { b"MM" => { self.byte_order = ByteOrder::BigEndian; self.reader.byte_order = ByteOrder::BigEndian; }, - _ => return Err(TiffError::FormatError( - "TIFF signature not found.".to_string() - )) + _ => return Err(TiffError::FormatError(TiffFormatError::TiffSignatureNotFound)) } if try!(self.read_short()) != 42 { - return Err(TiffError::FormatError("TIFF signature invalid.".to_string())) + return Err(TiffError::FormatError(TiffFormatError::TiffSignatureInvalid)) } self.next_ifd = match try!(self.read_long()) { 0 => None, @@ -323,9 +321,7 @@ impl Decoder { fn read_ifd(&mut self) -> TiffResult { let mut dir: Directory = HashMap::new(); match self.next_ifd { - None => return Err(TiffError::FormatError( - "Image file directory not found.".to_string()) - ), + None => return Err(TiffError::FormatError(TiffFormatError::ImageFileDirectoryNotFound)), Some(offset) => try!(self.goto_offset(offset)) } for _ in 0..try!(self.read_short()) { @@ -379,9 +375,7 @@ impl Decoder { pub fn get_tag(&mut self, tag: ifd::Tag) -> TiffResult { match try!(self.find_tag(tag)) { Some(val) => Ok(val), - None => Err(TiffError::FormatError(format!( - "Required tag `{:?}` not found.", tag - ))) + None => Err(TiffError::FormatError(TiffFormatError::RequiredTagNotFound(tag))), } } @@ -420,7 +414,7 @@ impl Decoder { }; Ok(match (color_type, buffer) { (ColorType:: RGB(8), DecodingBuffer::U8(ref mut buffer)) | - (ColorType::RGBA(8), DecodingBuffer::U8(ref mut buffer)) | + (ColorType::RGBA(8), DecodingBuffer::U8(ref mut buffer)) | (ColorType::CMYK(8), DecodingBuffer::U8(ref mut buffer)) => { try!(reader.read(&mut buffer[..bytes])) } @@ -533,9 +527,7 @@ impl Decoder { try!(self.colortype()) )) }, - None => return Err(TiffError::FormatError( - format!("Unknown predictor “{}” encountered", predictor) - )) + None => return Err(TiffError::FormatError(TiffFormatError::UnknownPredictor(predictor))), } } Ok(result) diff --git a/src/error.rs b/src/error.rs index bc6bf70..2774550 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,11 +2,13 @@ use std::error::Error; use std::fmt; use std::io; +use decoder::ifd::{Tag, Value}; + /// Tiff error kinds. #[derive(Debug)] pub enum TiffError { /// The Image is not formatted properly - FormatError(String), + FormatError(TiffFormatError), /// The Decoder does not support this image format UnsupportedError(String), @@ -15,6 +17,30 @@ pub enum TiffError { IoError(io::Error), } +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum TiffFormatError { + TiffSignatureNotFound, + TiffSignatureInvalid, + ImageFileDirectoryNotFound, + RequiredTagNotFound(Tag), + UnknownPredictor(u32), + UnsignedIntegerExpected(Value), +} + +impl fmt::Display for TiffFormatError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + use self::TiffFormatError::*; + match *self { + TiffSignatureNotFound => write!(fmt, "TIFF signature not found."), + TiffSignatureInvalid => write!(fmt, "TIFF signature invalid."), + ImageFileDirectoryNotFound => write!(fmt, "Image file directory not found."), + RequiredTagNotFound(ref tag) => write!(fmt, "Required tag `{:?}` not found.", tag), + UnknownPredictor(ref predictor) => write!(fmt, "Unknown predictor “{}” encountered", predictor), + UnsignedIntegerExpected(ref val) => write!(fmt, "Expected unsigned integer, {:?} found.", val), + } + } +} + impl fmt::Display for TiffError { fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { match *self { diff --git a/src/lib.rs b/src/lib.rs index f95658c..6e0e2ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ extern crate num_traits; pub mod decoder; mod error; -pub use self::error::{TiffError, TiffResult}; +pub use self::error::{TiffError, TiffFormatError, TiffResult}; /// An enumeration over supported color types and their bit depths #[derive(Copy, PartialEq, Eq, Debug, Clone, Hash)]