ddsfile/
error.rs

1// The MIT License (MIT)
2//
3// Copyright (c) 2018 Michael Dilger
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21// THE SOFTWARE.
22
23use 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}