Migrate from String to properly typed TiffFormatError
This commit is contained in:
1 parent
daf23f34a0
commit
2c6ca5459a
4 files changed
+38
-22
No files matched your search
+3
-5
@@ -4,7 +4,7 @@ use std::io::{self, Read, Seek};
|
|||||||
use std::collections::{HashMap};
|
use std::collections::{HashMap};
|
||||||
|
|
||||||
use super::stream::{ByteOrder, SmartReader, EndianReader};
|
use super::stream::{ByteOrder, SmartReader, EndianReader};
|
||||||
use ::{TiffError, TiffResult};
|
use ::{TiffError, TiffFormatError, TiffResult};
|
||||||
|
|
||||||
use self::Value::{Unsigned, List, Rational};
|
use self::Value::{Unsigned, List, Rational};
|
||||||
|
|
||||||
@@ -86,7 +86,7 @@ pub enum Type {
|
|||||||
|
|
||||||
|
|
||||||
#[allow(unused_qualifications)]
|
#[allow(unused_qualifications)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum Value {
|
pub enum Value {
|
||||||
//Signed(i32),
|
//Signed(i32),
|
||||||
Unsigned(u32),
|
Unsigned(u32),
|
||||||
@@ -98,9 +98,7 @@ impl Value {
|
|||||||
pub fn into_u32(self) -> TiffResult<u32> {
|
pub fn into_u32(self) -> TiffResult<u32> {
|
||||||
match self {
|
match self {
|
||||||
Unsigned(val) => Ok(val),
|
Unsigned(val) => Ok(val),
|
||||||
val => Err(TiffError::FormatError(format!(
|
val => Err(TiffError::FormatError(TiffFormatError::UnsignedIntegerExpected(val))),
|
||||||
"Expected unsigned integer, {:?} found.", val
|
|
||||||
)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn into_u32_vec(self) -> TiffResult<Vec<u32>> {
|
pub fn into_u32_vec(self) -> TiffResult<Vec<u32>> {
|
||||||
|
|||||||
+7
-15
@@ -3,7 +3,7 @@ use std::mem;
|
|||||||
use num_traits::{FromPrimitive, Num};
|
use num_traits::{FromPrimitive, Num};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use ::{ColorType, TiffError, TiffResult};
|
use ::{ColorType, TiffError, TiffFormatError, TiffResult};
|
||||||
|
|
||||||
use self::ifd::Directory;
|
use self::ifd::Directory;
|
||||||
|
|
||||||
@@ -187,12 +187,10 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
b"MM" => {
|
b"MM" => {
|
||||||
self.byte_order = ByteOrder::BigEndian;
|
self.byte_order = ByteOrder::BigEndian;
|
||||||
self.reader.byte_order = ByteOrder::BigEndian; },
|
self.reader.byte_order = ByteOrder::BigEndian; },
|
||||||
_ => return Err(TiffError::FormatError(
|
_ => return Err(TiffError::FormatError(TiffFormatError::TiffSignatureNotFound))
|
||||||
"TIFF signature not found.".to_string()
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
if try!(self.read_short()) != 42 {
|
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()) {
|
self.next_ifd = match try!(self.read_long()) {
|
||||||
0 => None,
|
0 => None,
|
||||||
@@ -323,9 +321,7 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
fn read_ifd(&mut self) -> TiffResult<Directory> {
|
fn read_ifd(&mut self) -> TiffResult<Directory> {
|
||||||
let mut dir: Directory = HashMap::new();
|
let mut dir: Directory = HashMap::new();
|
||||||
match self.next_ifd {
|
match self.next_ifd {
|
||||||
None => return Err(TiffError::FormatError(
|
None => return Err(TiffError::FormatError(TiffFormatError::ImageFileDirectoryNotFound)),
|
||||||
"Image file directory not found.".to_string())
|
|
||||||
),
|
|
||||||
Some(offset) => try!(self.goto_offset(offset))
|
Some(offset) => try!(self.goto_offset(offset))
|
||||||
}
|
}
|
||||||
for _ in 0..try!(self.read_short()) {
|
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> {
|
pub fn get_tag(&mut self, tag: ifd::Tag) -> TiffResult<ifd::Value> {
|
||||||
match try!(self.find_tag(tag)) {
|
match try!(self.find_tag(tag)) {
|
||||||
Some(val) => Ok(val),
|
Some(val) => Ok(val),
|
||||||
None => Err(TiffError::FormatError(format!(
|
None => Err(TiffError::FormatError(TiffFormatError::RequiredTagNotFound(tag))),
|
||||||
"Required tag `{:?}` not found.", tag
|
|
||||||
)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -420,7 +414,7 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
};
|
};
|
||||||
Ok(match (color_type, buffer) {
|
Ok(match (color_type, buffer) {
|
||||||
(ColorType:: RGB(8), DecodingBuffer::U8(ref mut 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)) => {
|
(ColorType::CMYK(8), DecodingBuffer::U8(ref mut buffer)) => {
|
||||||
try!(reader.read(&mut buffer[..bytes]))
|
try!(reader.read(&mut buffer[..bytes]))
|
||||||
}
|
}
|
||||||
@@ -533,9 +527,7 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
try!(self.colortype())
|
try!(self.colortype())
|
||||||
))
|
))
|
||||||
},
|
},
|
||||||
None => return Err(TiffError::FormatError(
|
None => return Err(TiffError::FormatError(TiffFormatError::UnknownPredictor(predictor))),
|
||||||
format!("Unknown predictor “{}” encountered", predictor)
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(result)
|
Ok(result)
|
||||||
|
|||||||
+27
-1
@@ -2,11 +2,13 @@ use std::error::Error;
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
|
use decoder::ifd::{Tag, Value};
|
||||||
|
|
||||||
/// Tiff error kinds.
|
/// Tiff error kinds.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum TiffError {
|
pub enum TiffError {
|
||||||
/// The Image is not formatted properly
|
/// The Image is not formatted properly
|
||||||
FormatError(String),
|
FormatError(TiffFormatError),
|
||||||
|
|
||||||
/// The Decoder does not support this image format
|
/// The Decoder does not support this image format
|
||||||
UnsupportedError(String),
|
UnsupportedError(String),
|
||||||
@@ -15,6 +17,30 @@ pub enum TiffError {
|
|||||||
IoError(io::Error),
|
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 {
|
impl fmt::Display for TiffError {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||||
match *self {
|
match *self {
|
||||||
|
|||||||
+1
-1
@@ -14,7 +14,7 @@ extern crate num_traits;
|
|||||||
pub mod decoder;
|
pub mod decoder;
|
||||||
mod error;
|
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
|
/// An enumeration over supported color types and their bit depths
|
||||||
#[derive(Copy, PartialEq, Eq, Debug, Clone, Hash)]
|
#[derive(Copy, PartialEq, Eq, Debug, Clone, Hash)]
|
||||||
|
|||||||
Reference in new issue
Block a user