1use std::fmt;
24
25#[derive(Debug)]
26pub enum Error {
27 Fmt(fmt::Error),
28 Io(std::io::Error),
29 General(String),
30 BadMagicNumber,
31 InvalidField(String),
32 ShortFile,
33 UnsupportedFormat,
34 OutOfBounds,
35}
36
37impl fmt::Display for Error {
38 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39 match *self {
40 Error::Fmt(ref e) => write!(f, "{}", e),
41 Error::Io(ref e) => write!(f, "{}", e),
42 Error::General(ref s) => write!(f, "General Error: {}", s),
43 Error::BadMagicNumber => write!(f, "Bad Magic Number"),
44 Error::InvalidField(ref s) => write!(f, "Invalid Field: {}", s),
45 Error::ShortFile => write!(f, "File is cut short"),
46 Error::UnsupportedFormat => {
47 write!(f, "Format is not supported well enough for this operation")
48 }
49 Error::OutOfBounds => write!(f, "Request is out of bounds"),
50 }
51 }
52}
53
54impl std::error::Error for Error {
55 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
56 match *self {
57 Error::Fmt(ref e) => Some(e),
58 Error::Io(ref e) => Some(e),
59 _ => None,
60 }
61 }
62}
63
64impl From<fmt::Error> for Error {
65 fn from(e: fmt::Error) -> Error {
66 Error::Fmt(e)
67 }
68}
69
70impl From<std::io::Error> for Error {
71 fn from(e: std::io::Error) -> Error {
72 Error::Io(e)
73 }
74}
75
76impl From<&str> for Error {
77 fn from(s: &str) -> Error {
78 Error::General(s.to_owned())
79 }
80}
81
82impl From<String> for Error {
83 fn from(s: String) -> Error {
84 Error::General(s)
85 }
86}