1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#[cfg(all(not(target_arch = "wasm32"), feature = "gamepads"))]
use gilrs;
#[cfg(not(target_arch = "wasm32"))]
use glutin;
use image;
#[cfg(all(not(target_arch = "wasm32"), feature = "rodio"))]
use rodio;
use crate::graphics::{AtlasError, ImageError};
#[cfg(feature = "rusttype")]
use rusttype::Error as FontError;
#[cfg(feature = "saving")]
use crate::saving::SaveError;
#[cfg(feature = "sounds")]
use crate::sound::SoundError;
use std::{fmt, error::Error, io::Error as IOError};

#[derive(Debug)]
/// An error generated by some Quicksilver subsystem
pub enum QuicksilverError {
    /// An error from an image atlas
    AtlasError(AtlasError),
    /// Creating or manipulating the OpenGL Context failed
    ContextError(String),
    /// An error from loading an image
    ImageError(ImageError),
    /// An error from loading a file
    IOError(IOError),
    /// An error when creating a gilrs context
    #[cfg(feature = "gamepads")]
    GilrsError(gilrs::Error),
    /// An error from loading a sound
    #[cfg(feature = "sounds")]
    SoundError(SoundError),
    /// A serialize or deserialize error
    #[cfg(feature = "saving")]
    SaveError(SaveError),
    /// There was an error loading a font file
    #[cfg(feature = "rusttype")]
    FontError(FontError),
}

impl fmt::Display for QuicksilverError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.description())
    }
}

impl Error for QuicksilverError {
    fn description(&self) -> &str {
        match self {
            QuicksilverError::AtlasError(err) => err.description(),
            QuicksilverError::ContextError(string) => string.as_str(),
            QuicksilverError::ImageError(err) => err.description(),
            QuicksilverError::IOError(err) => err.description(),
            #[cfg(feature = "gamepads")]
            QuicksilverError::GilrsError(err) => err.description(),
            #[cfg(feature = "sounds")]
            QuicksilverError::SoundError(err) => err.description(),
            #[cfg(feature = "saving")]
            QuicksilverError::SaveError(err) => err.description(),
            #[cfg(feature = "rusttype")]
            QuicksilverError::FontError(err) => err.description(),
        }
    }

    fn cause(&self) -> Option<&dyn Error> {
        match self {
            QuicksilverError::AtlasError(err) => Some(err),
            QuicksilverError::ContextError(_) => None,
            QuicksilverError::ImageError(err) => Some(err),
            QuicksilverError::IOError(err) => Some(err),
            #[cfg(feature = "gamepads")]
            QuicksilverError::GilrsError(err) => Some(err),
            #[cfg(feature = "sounds")]
            QuicksilverError::SoundError(err) => Some(err),
            #[cfg(feature = "saving")]
            QuicksilverError::SaveError(err) => Some(err),
            #[cfg(feature = "rusttype")]
            QuicksilverError::FontError(err) => Some(err),
        }
    }
}

#[doc(hidden)]
impl From<ImageError> for QuicksilverError {
    fn from(err: ImageError) -> QuicksilverError {
        QuicksilverError::ImageError(err)
    }
}

#[doc(hidden)]
#[cfg(feature = "sounds")]
impl From<SoundError> for QuicksilverError {
    fn from(err: SoundError) -> QuicksilverError {
        QuicksilverError::SoundError(err)
    }
}

#[doc(hidden)]
impl From<AtlasError> for QuicksilverError {
    fn from(err: AtlasError) -> QuicksilverError {
        QuicksilverError::AtlasError(err)
    }
}

impl From<IOError> for QuicksilverError {
    fn from(err: IOError) -> QuicksilverError {
        QuicksilverError::IOError(err)
    }
}

#[cfg(feature = "saving")]
impl From<SaveError> for QuicksilverError {
    fn from(err: SaveError) -> QuicksilverError {
        QuicksilverError::SaveError(err)
    }
}

#[doc(hidden)]
impl From<image::ImageError> for QuicksilverError {
    fn from(img: image::ImageError) -> QuicksilverError {
        let image_error: ImageError = img.into();
        image_error.into()
    }
}

#[doc(hidden)]
#[cfg(all(feature = "sounds", not(target_arch = "wasm32")))]
impl From<rodio::decoder::DecoderError> for QuicksilverError {
    fn from(snd: rodio::decoder::DecoderError) -> QuicksilverError {
        let sound_error: SoundError = snd.into();
        sound_error.into()
    }
}

#[doc(hidden)]
#[cfg(feature = "rusttype")]
impl From<FontError> for QuicksilverError {
    fn from(fnt: FontError) -> QuicksilverError {
        QuicksilverError::FontError(fnt)
    }
}

#[cfg(not(target_arch = "wasm32"))]
const ROBUST_ERROR: &str = r#"Internal Quicksilver error: robustness not supported
Please file a bug report at https://github.com/ryanisaacg/quicksilver that includes:
- A minimum reproducing code snippet
- The error message above
"#;

#[cfg(not(target_arch = "wasm32"))]
fn creation_to_str(err: &glutin::CreationError) -> String {
    match err {
        glutin::CreationError::OsError(string) => string.to_owned(),
        glutin::CreationError::NotSupported(err) => (*err).to_owned(),
        glutin::CreationError::NoBackendAvailable(error) => error.to_string(),
        glutin::CreationError::RobustnessNotSupported => ROBUST_ERROR.to_owned(),
        glutin::CreationError::OpenGlVersionNotSupported => {
            "OpenGL version not supported".to_owned()
        }
        glutin::CreationError::NoAvailablePixelFormat => "No available pixel format".to_owned(),
        glutin::CreationError::PlatformSpecific(string) => string.to_owned(),
        glutin::CreationError::Window(error) => match error {
            glutin::WindowCreationError::OsError(string) => string.to_owned(),
            glutin::WindowCreationError::NotSupported => {
                "Window creation failed: not supported".to_owned()
            }
        },
        glutin::CreationError::CreationErrors(errors) => {
            format!("{:?}", errors)
        }
    }
}

#[doc(hidden)]
#[cfg(not(target_arch = "wasm32"))]
impl From<glutin::CreationError> for QuicksilverError {
    fn from(err: glutin::CreationError) -> QuicksilverError {
        QuicksilverError::ContextError(creation_to_str(&err))
    }
}

#[doc(hidden)]
#[cfg(not(target_arch = "wasm32"))]
impl From<glutin::ContextError> for QuicksilverError {
    fn from(err: glutin::ContextError) -> QuicksilverError {
        match err {
            glutin::ContextError::IoError(err) => QuicksilverError::IOError(err),
            glutin::ContextError::OsError(err) => QuicksilverError::ContextError(err),
            glutin::ContextError::ContextLost => {
                QuicksilverError::ContextError("Context lost".to_owned())
            }
        }
    }
}

#[doc(hidden)]
#[cfg(feature = "gamepads")]
impl From<gilrs::Error> for QuicksilverError {
    fn from(err: gilrs::Error) -> QuicksilverError {
        QuicksilverError::GilrsError(err)
    }
}