1 //! Error types that can be emitted from this library
2 
3 use std::convert;
4 use std::error;
5 use std::fmt;
6 use std::io;
7 
8 /// Generic result type with ZipError as its error variant
9 pub type ZipResult<T> = Result<T, ZipError>;
10 
11 /// Error type for Zip
12 #[derive(Debug)]
13 pub enum ZipError
14 {
15     /// An Error caused by I/O
16     Io(io::Error),
17 
18     /// This file is probably not a zip archive
19     InvalidArchive(&'static str),
20 
21     /// This archive is not supported
22     UnsupportedArchive(&'static str),
23 
24     /// The requested file could not be found in the archive
25     FileNotFound,
26 }
27 
28 impl ZipError
29 {
detail(&self) -> ::std::borrow::Cow<str>30     fn detail(&self) -> ::std::borrow::Cow<str>
31     {
32         use std::error::Error;
33 
34         match *self
35         {
36             ZipError::Io(ref io_err) => {
37                 ("Io Error: ".to_string() + (io_err as &error::Error).description()).into()
38             },
39             ZipError::InvalidArchive(msg) | ZipError::UnsupportedArchive(msg) => {
40                 (self.description().to_string() + ": " + msg).into()
41             },
42             ZipError::FileNotFound => {
43                 self.description().into()
44             },
45         }
46     }
47 }
48 
49 impl convert::From<io::Error> for ZipError
50 {
from(err: io::Error) -> ZipError51     fn from(err: io::Error) -> ZipError
52     {
53         ZipError::Io(err)
54     }
55 }
56 
57 impl convert::From<ZipError> for io::Error
58 {
from(err: ZipError) -> io::Error59     fn from(err: ZipError) -> io::Error
60     {
61         io::Error::new(io::ErrorKind::Other, err)
62     }
63 }
64 
65 impl fmt::Display for ZipError
66 {
fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error>67     fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error>
68     {
69         fmt.write_str(&*self.detail())
70     }
71 }
72 
73 impl error::Error for ZipError
74 {
description(&self) -> &str75     fn description(&self) -> &str
76     {
77         match *self
78         {
79             ZipError::Io(ref io_err) => (io_err as &error::Error).description(),
80             ZipError::InvalidArchive(..) => "Invalid Zip archive",
81             ZipError::UnsupportedArchive(..) => "Unsupported Zip archive",
82             ZipError::FileNotFound => "Specified file not found in archive",
83         }
84     }
85 
cause(&self) -> Option<&error::Error>86     fn cause(&self) -> Option<&error::Error>
87     {
88         match *self
89         {
90             ZipError::Io(ref io_err) => Some(io_err as &error::Error),
91             _ => None,
92         }
93     }
94 }
95