exif/
error.rs

1//
2// Copyright (c) 2016 KAMADA Ken'ichi.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9//    notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11//    notice, this list of conditions and the following disclaimer in the
12//    documentation and/or other materials provided with the distribution.
13//
14// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17// ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24// SUCH DAMAGE.
25//
26
27use std::error;
28use std::fmt;
29use std::io;
30
31/// An error returned when parsing of Exif data fails.
32#[derive(Debug)]
33#[non_exhaustive]
34pub enum Error {
35    /// Input data was malformed or truncated.
36    InvalidFormat(&'static str),
37    /// Input data could not be read due to an I/O error and
38    /// a `std::io::Error` value is associated with this variant.
39    Io(io::Error),
40    /// Exif attribute information was not found in an image file
41    /// such as JPEG.
42    NotFound(&'static str),
43    /// The value of the field is blank.  Some fields have blank values
44    /// whose meanings are defined as "unknown".  Such a blank value
45    /// should be treated the same as the absence of the field.
46    BlankValue(&'static str),
47    /// Field values or image data are too big to encode.
48    TooBig(&'static str),
49    /// The field type is not supported and cannnot be encoded.
50    NotSupported(&'static str),
51    /// The field has an unexpected value.
52    UnexpectedValue(&'static str),
53}
54
55impl From<io::Error> for Error {
56    fn from(err: io::Error) -> Error {
57        Error::Io(err)
58    }
59}
60
61impl fmt::Display for Error {
62    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63        match *self {
64            Error::InvalidFormat(msg) => f.write_str(msg),
65            Error::Io(ref err) => err.fmt(f),
66            Error::NotFound(ctn) => write!(f, "No Exif data found in {}", ctn),
67            Error::BlankValue(msg) => f.write_str(msg),
68            Error::TooBig(msg) => f.write_str(msg),
69            Error::NotSupported(msg) => f.write_str(msg),
70            Error::UnexpectedValue(msg) => f.write_str(msg),
71        }
72    }
73}
74
75impl error::Error for Error {
76    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
77        match *self {
78            Error::InvalidFormat(_) => None,
79            Error::Io(ref err) => Some(err),
80            Error::NotFound(_) => None,
81            Error::BlankValue(_) => None,
82            Error::TooBig(_) => None,
83            Error::NotSupported(_) => None,
84            Error::UnexpectedValue(_) => None,
85        }
86    }
87}