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
204
205
206
207
208
209
210
211
212
213
214
//! Error types and conversion functions.
use std::error::Error;
use std::fmt;
use std::string::FromUtf8Error;
use std::sync::Arc;

/// An enum containing all kinds of game framework errors.
#[derive(Debug)]
pub enum GameError {
    /// An error when intializing the graphics system.
    GraphicsInitializationError,
    /// An error in the filesystem layout
    FilesystemError(String),
    /// An error in the config file
    ConfigError(String),
    /// Happens when an `winit::event_loop::EventLoopProxy` attempts to
    /// wake up an `winit::event_loop::EventLoop` that no longer exists.
    EventLoopError(String),
    /// An error trying to load a resource, such as getting an invalid image file.
    ResourceLoadError(String),
    /// Unable to find a resource; the `Vec` is the paths it searched for and associated errors
    ResourceNotFound(String, Vec<(std::path::PathBuf, GameError)>),
    /// Something went wrong in the renderer
    RenderError(String),
    /// Something went wrong when requesting a logical device from the graphics API.
    RequestDeviceError(wgpu::RequestDeviceError),
    /// Something went wrong in the audio playback
    AudioError(String),
    /// Something went wrong trying to set or get window properties.
    WindowError(String),
    /// Something went wrong trying to create a window
    WindowCreationError(Arc<winit::error::OsError>),
    /// Something went wrong trying to read from a file
    #[allow(clippy::upper_case_acronyms)]
    IOError(Arc<std::io::Error>),
    /// Something went wrong trying to load a font
    FontError(glyph_brush::ab_glyph::InvalidFont),
    /// Shader encoding error (not valid utf-8)
    ShaderEncodingError(FromUtf8Error),
    /// Something went wrong applying video settings.
    VideoError(String),
    /// Something went wrong with the `gilrs` gamepad-input library.
    GamepadError(String),
    /// Something went wrong with the `lyon` shape-tesselation library.
    LyonError(String),
    /// Something went wrong when drawing text.
    GlyphBrushError(glyph_brush::BrushError),
    /// Attempted to draw text with a non-existent font name.
    FontSelectError(String),
    /// Something went wrong when asynchronously mapping a GPU buffer.
    BufferAsyncError(wgpu::BufferAsyncError),
    /// Deadlock when trying to lock a mutex.
    LockError,
    /// A custom error type for use by users of ggez.
    /// This lets you handle custom errors that may happen during your game (such as, trying to load a malformed file for a level)
    /// using the same mechanism you handle ggez's other errors.
    ///
    /// Please include an informative message with the error.
    CustomError(String),
}

impl fmt::Display for GameError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            GameError::ConfigError(ref s) => write!(f, "Config error: {s}"),
            GameError::ResourceLoadError(ref s) => write!(f, "Error loading resource: {s}"),
            GameError::ResourceNotFound(ref s, ref paths) => {
                write!(f, "Resource not found: {s}, searched in paths {paths:?}")
            }
            GameError::WindowError(ref e) => write!(f, "Window creation error: {e}"),
            GameError::CustomError(ref s) => write!(f, "Custom error: {s}"),
            GameError::RequestDeviceError(ref e) => {
                write!(f, "Failed to request logical device: {e}")
            }
            GameError::GlyphBrushError(ref e) => write!(f, "Text rendering error: {e}"),
            GameError::FontSelectError(ref e) => write!(f, "No such font '{e}'"),
            GameError::BufferAsyncError(ref e) => write!(f, "Async buffer map error: {e}"),
            _ => write!(f, "GameError {self:?}"),
        }
    }
}

impl Error for GameError {
    fn cause(&self) -> Option<&dyn Error> {
        match *self {
            GameError::RequestDeviceError(ref e) => Some(e),
            GameError::WindowCreationError(ref e) => Some(&**e),
            GameError::IOError(ref e) => Some(&**e),
            GameError::FontError(ref e) => Some(e),
            GameError::GlyphBrushError(ref e) => Some(e),
            GameError::BufferAsyncError(ref e) => Some(e),
            _ => None,
        }
    }
}

/// A convenient result type consisting of a return type and a `GameError`
pub type GameResult<T = ()> = Result<T, GameError>;

impl From<std::io::Error> for GameError {
    fn from(e: std::io::Error) -> GameError {
        GameError::IOError(Arc::new(e))
    }
}

impl From<toml::de::Error> for GameError {
    fn from(e: toml::de::Error) -> GameError {
        let errstr = format!("TOML decode error: {e}");

        GameError::ConfigError(errstr)
    }
}

impl From<toml::ser::Error> for GameError {
    fn from(e: toml::ser::Error) -> GameError {
        let errstr = format!("TOML error (possibly encoding?): {e}");
        GameError::ConfigError(errstr)
    }
}

impl From<zip::result::ZipError> for GameError {
    fn from(e: zip::result::ZipError) -> GameError {
        let errstr = format!("Zip error: {e}");
        GameError::ResourceLoadError(errstr)
    }
}

#[cfg(feature = "audio")]
impl From<rodio::decoder::DecoderError> for GameError {
    fn from(e: rodio::decoder::DecoderError) -> GameError {
        let errstr = format!("Audio decoder error: {e:?}");
        GameError::AudioError(errstr)
    }
}

#[cfg(feature = "audio")]
impl From<rodio::PlayError> for GameError {
    fn from(e: rodio::PlayError) -> GameError {
        let errstr = format!("Audio playing error: {e:?}");
        GameError::AudioError(errstr)
    }
}

impl From<image::ImageError> for GameError {
    fn from(e: image::ImageError) -> GameError {
        let errstr = format!("Image load error: {e}");
        GameError::ResourceLoadError(errstr)
    }
}
impl From<winit::error::OsError> for GameError {
    fn from(s: winit::error::OsError) -> GameError {
        GameError::WindowCreationError(Arc::new(s))
    }
}

#[cfg(feature = "gamepad")]
impl From<gilrs::Error> for GameError {
    fn from(s: gilrs::Error) -> GameError {
        let errstr = format!("Gamepad error: {s}");
        GameError::GamepadError(errstr)
    }
}

impl From<lyon::lyon_tessellation::TessellationError> for GameError {
    fn from(s: lyon::lyon_tessellation::TessellationError) -> GameError {
        let errstr =
            format!("Error while tesselating shape (did you give it an infinity or NaN?): {s:?}");
        GameError::LyonError(errstr)
    }
}

impl From<lyon::lyon_tessellation::geometry_builder::GeometryBuilderError> for GameError {
    fn from(s: lyon::lyon_tessellation::geometry_builder::GeometryBuilderError) -> GameError {
        let errstr =
            format!("Error while building geometry (did you give it too many vertices?): {s:?}");
        GameError::LyonError(errstr)
    }
}

impl From<wgpu::RequestDeviceError> for GameError {
    fn from(s: wgpu::RequestDeviceError) -> GameError {
        GameError::RequestDeviceError(s)
    }
}

impl From<Arc<winit::error::OsError>> for GameError {
    fn from(s: Arc<winit::error::OsError>) -> GameError {
        GameError::WindowCreationError(s)
    }
}

impl From<Arc<std::io::Error>> for GameError {
    fn from(s: Arc<std::io::Error>) -> GameError {
        GameError::IOError(s)
    }
}

impl From<glyph_brush::ab_glyph::InvalidFont> for GameError {
    fn from(s: glyph_brush::ab_glyph::InvalidFont) -> GameError {
        GameError::FontError(s)
    }
}

impl From<glyph_brush::BrushError> for GameError {
    fn from(s: glyph_brush::BrushError) -> GameError {
        GameError::GlyphBrushError(s)
    }
}

impl From<wgpu::BufferAsyncError> for GameError {
    fn from(s: wgpu::BufferAsyncError) -> GameError {
        GameError::BufferAsyncError(s)
    }
}