Add Error::TooBig and NotSupported.

This commit is contained in:
KAMADA Ken'ichi 2017-07-18 22:42:45 +09:00
parent aa41b66838
commit a4f891ec25
4 changed files with 16 additions and 9 deletions

View File

@ -42,6 +42,10 @@ pub enum Error {
/// whose meanings are defined as "unknown". Such a blank value
/// should be treated the same as the absence of the field.
BlankValue(&'static str),
/// Field values or image data are too big to encode.
TooBig(&'static str),
/// The field type is not supported and cannnot be encoded.
NotSupported(&'static str),
}
impl From<io::Error> for Error {
@ -57,6 +61,8 @@ impl fmt::Display for Error {
Error::Io(ref err) => err.fmt(f),
Error::NotFound(msg) => f.write_str(msg),
Error::BlankValue(msg) => f.write_str(msg),
Error::TooBig(msg) => f.write_str(msg),
Error::NotSupported(msg) => f.write_str(msg),
}
}
}
@ -68,6 +74,8 @@ impl error::Error for Error {
Error::Io(ref err) => err.description(),
Error::NotFound(msg) => msg,
Error::BlankValue(msg) => msg,
Error::TooBig(msg) => msg,
Error::NotSupported(msg) => msg,
}
}
@ -77,6 +85,8 @@ impl error::Error for Error {
Error::Io(ref err) => Some(err),
Error::NotFound(_) => None,
Error::BlankValue(_) => None,
Error::TooBig(_) => None,
Error::NotSupported(_) => None,
}
}
}

View File

@ -47,6 +47,7 @@
//!
//! Major changes between 0.2.3 and 0.3 are listed below.
//!
//! * Enum Error has two new variants: TooBig and NotSupported.
pub use error::Error;
pub use jpeg::get_exif_attr as get_exif_attr_from_jpeg;

View File

@ -155,10 +155,6 @@ impl<'a> Writer<'a> {
///
/// The write position of `w` must be set to zero before calling
/// this method.
///
/// A new `exif::Error` variant will be introduced in the next
/// API-version bump to return write errors. `Error::InvalidFormat`
/// is used until then.
pub fn write<W>(&mut self, w: &mut W, little_endian: bool)
-> Result<(), Error> where W: Write + Seek {
// TIFF signature and the offset of the 0th IFD.
@ -428,7 +424,7 @@ fn write_ifd_and_fields<W, E>(
for f in fields {
let (typ, cnt, mut valbuf) = try!(compose_value::<E>(&f.value));
if cnt as u32 as usize != cnt {
return Err(Error::InvalidFormat("Too long array"));
return Err(Error::TooBig("Too long array"));
}
try!(E::writeu16(&mut ifd, f.tag.number()));
try!(E::writeu16(&mut ifd, typ));
@ -552,7 +548,7 @@ fn compose_value<E>(value: &Value)
Ok((12, vec.len(), buf))
},
Value::Unknown(_, _, _) =>
Err(Error::InvalidFormat("Cannot write unknown field types")),
Err(Error::NotSupported("Cannot write unknown field types")),
}
}
@ -570,7 +566,7 @@ fn pad_and_get_offset<W>(w: &mut W)
-> Result<u32, Error> where W: Write + Seek {
let mut pos = try!(w.seek(SeekFrom::Current(0)));
if pos >= (1 << 32) - 1 {
return Err(Error::InvalidFormat("Offset too large"));
return Err(Error::TooBig("Offset too large"));
}
if pos % 2 != 0 {
try!(w.write_all(&[0]));
@ -583,7 +579,7 @@ fn get_offset<W>(w: &mut W)
-> Result<u32, Error> where W: Write + Seek {
let pos = try!(w.seek(SeekFrom::Current(0)));
if pos as u32 as u64 != pos {
return Err(Error::InvalidFormat("Offset too large"));
return Err(Error::TooBig("Offset too large"));
}
Ok(pos as u32)
}

View File

@ -99,7 +99,7 @@ fn rwr_compare<P>(path: P) where P: AsRef<Path> {
#[cfg(not(test))]
match writer.write(&mut out, reader1.little_endian()) {
Ok(_) => {},
Err(Error::InvalidFormat("Cannot write unknown field types")) => {
Err(Error::NotSupported(_)) => {
println!("{}: Contains unknown type", path.display());
return;
},