Migrate from String to properly typed TiffFormatError

This commit is contained in:
Felix Schütt committed 2018-09-13 13:20:50 +02:00
1 parent daf23f34a0
commit 2c6ca5459a
4 files changed
+38 -22

No files matched your search

+3 -5
View File
@@ -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<u32> {
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<Vec<u32>> {
+7 -15
View File
@@ -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<R: Read + Seek> Decoder<R> {
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<R: Read + Seek> Decoder<R> {
fn read_ifd(&mut self) -> TiffResult<Directory> {
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<R: Read + Seek> Decoder<R> {
pub fn get_tag(&mut self, tag: ifd::Tag) -> TiffResult<ifd::Value> {
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<R: Read + Seek> Decoder<R> {
};
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<R: Read + Seek> Decoder<R> {
try!(self.colortype())
))
},
None => return Err(TiffError::FormatError(
format!("Unknown predictor “{}” encountered", predictor)
))
None => return Err(TiffError::FormatError(TiffFormatError::UnknownPredictor(predictor))),
}
}
Ok(result)
+27 -1
View File
@@ -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 {
+1 -1
View File
@@ -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)]