1use std::fmt;
18
19#[non_exhaustive]
24#[derive(Clone, Debug, PartialEq, Eq)]
25pub enum PlatformError {
26 Unsupported,
28 NoDisplay {
30 wayland: bool,
33 },
34 LibraryNotFound {
36 library: &'static str,
38 reason: String,
40 },
41 MissingSymbols {
43 library: &'static str,
45 symbols: Vec<&'static str>,
47 },
48 ConnectionFailed {
50 display: String,
52 },
53 CallFailed {
55 call: &'static str,
57 },
58 InvalidName {
61 name: String,
63 },
64 NoHomeDirectory,
67 NotMainThread,
70 GpuCallFailed {
72 call: &'static str,
74 error: String,
77 },
78 NoGlConfig,
82 GlTooOld {
84 found: Option<String>,
87 },
88 GlContextExists,
92 InvalidAudioConfig {
95 field: &'static str,
97 expected: &'static str,
99 },
100 AudioCallFailed {
103 call: &'static str,
105 error: String,
107 },
108}
109
110impl fmt::Display for PlatformError {
111 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112 match self {
113 Self::Unsupported => f.write_str("this operating system has no backend yet"),
114 Self::NoDisplay { wayland: false } => {
115 f.write_str("no X server to connect to: `DISPLAY` is not set")
116 }
117 Self::NoDisplay { wayland: true } => f.write_str(
118 "no X server to connect to: `DISPLAY` is not set. This is a Wayland session, \
119 and the game needs XWayland in it. Turn XWayland on in the compositor's \
120 settings, then start the game again",
121 ),
122 Self::LibraryNotFound { library, reason } => {
123 write!(f, "could not load {library}: {reason}")
124 }
125 Self::MissingSymbols { library, symbols } => write!(
126 f,
127 "{library} lacks functions that the engine calls: {}",
128 symbols.join(", ")
129 ),
130 Self::ConnectionFailed { display } => {
131 write!(f, "could not connect to the X server `{display}`")
132 }
133 Self::CallFailed { call } => write!(f, "{call} failed"),
134 Self::InvalidName { name } => write!(
135 f,
136 "`{name}` cannot be a folder name on Windows, macOS and Linux"
137 ),
138 Self::NoHomeDirectory => {
139 f.write_str("could not find the home directory: set `HOME` to an absolute path")
140 }
141 Self::NotMainThread => f.write_str("the window can only be opened on the main thread"),
142 Self::GpuCallFailed { call, error } => write!(f, "{call} failed: {error}"),
143 Self::NoGlConfig => f.write_str(
144 "the graphics driver has no OpenGL frame buffer format with 8-bit red, green and \
145 blue",
146 ),
147 Self::GlTooOld { found: None } => {
148 f.write_str("the graphics driver does not offer OpenGL 3.3 (core profile)")
149 }
150 Self::GlTooOld { found: Some(found) } => write!(
151 f,
152 "the graphics driver does not offer OpenGL 3.3 (core profile): it gave `{found}`"
153 ),
154 Self::GlContextExists => f.write_str("this thread already has an OpenGL context"),
155 Self::InvalidAudioConfig { field, expected } => {
156 write!(f, "the audio output's `{field}` must be from {expected}")
157 }
158 Self::AudioCallFailed { call, error } => write!(f, "{call} failed: {error}"),
159 }
160 }
161}
162
163impl std::error::Error for PlatformError {}
164
165#[cfg(test)]
166mod tests {
167 use super::*;
168
169 #[test]
170 fn each_message_says_what_went_wrong() {
171 let cases = [
172 (
173 PlatformError::Unsupported,
174 "this operating system has no backend yet",
175 ),
176 (
177 PlatformError::NoDisplay { wayland: false },
178 "no X server to connect to: `DISPLAY` is not set",
179 ),
180 (
181 PlatformError::LibraryNotFound {
182 library: "libX11.so.6",
183 reason: "cannot open shared object file".to_string(),
184 },
185 "could not load libX11.so.6: cannot open shared object file",
186 ),
187 (
188 PlatformError::MissingSymbols {
189 library: "libX11.so.6",
190 symbols: vec!["XPending", "XNextEvent"],
191 },
192 "libX11.so.6 lacks functions that the engine calls: XPending, XNextEvent",
193 ),
194 (
195 PlatformError::ConnectionFailed {
196 display: ":7".to_string(),
197 },
198 "could not connect to the X server `:7`",
199 ),
200 (
201 PlatformError::CallFailed {
202 call: "XSetWMProtocols",
203 },
204 "XSetWMProtocols failed",
205 ),
206 (
207 PlatformError::InvalidName {
208 name: "a/b".to_string(),
209 },
210 "`a/b` cannot be a folder name on Windows, macOS and Linux",
211 ),
212 (
213 PlatformError::NoHomeDirectory,
214 "could not find the home directory: set `HOME` to an absolute path",
215 ),
216 (
217 PlatformError::NotMainThread,
218 "the window can only be opened on the main thread",
219 ),
220 (
221 PlatformError::GpuCallFailed {
222 call: "eglCreateWindowSurface",
223 error: "EGL_BAD_MATCH".to_string(),
224 },
225 "eglCreateWindowSurface failed: EGL_BAD_MATCH",
226 ),
227 (
228 PlatformError::NoGlConfig,
229 "the graphics driver has no OpenGL frame buffer format with 8-bit red, green and \
230 blue",
231 ),
232 (
233 PlatformError::GlTooOld { found: None },
234 "the graphics driver does not offer OpenGL 3.3 (core profile)",
235 ),
236 (
237 PlatformError::GlTooOld {
238 found: Some("3.1 Mesa 24.0.5".to_string()),
239 },
240 "the graphics driver does not offer OpenGL 3.3 (core profile): it gave `3.1 Mesa \
241 24.0.5`",
242 ),
243 (
244 PlatformError::GlContextExists,
245 "this thread already has an OpenGL context",
246 ),
247 (
248 PlatformError::InvalidAudioConfig {
249 field: "channels",
250 expected: "1 to 8",
251 },
252 "the audio output's `channels` must be from 1 to 8",
253 ),
254 (
255 PlatformError::AudioCallFailed {
256 call: "snd_pcm_set_params",
257 error: "Invalid argument (the ALSA device `default`)".to_string(),
258 },
259 "snd_pcm_set_params failed: Invalid argument (the ALSA device `default`)",
260 ),
261 ];
262 for (error, message) in cases {
263 assert_eq!(error.to_string(), message);
264 }
265 }
266
267 #[test]
268 fn a_wayland_session_without_xwayland_is_told_what_to_do() {
269 let message = PlatformError::NoDisplay { wayland: true }.to_string();
270 assert!(
271 message.starts_with("no X server to connect to"),
272 "{message}"
273 );
274 assert!(message.contains("Wayland session"), "{message}");
275 assert!(message.contains("Turn XWayland on"), "{message}");
276 }
277
278 #[test]
279 fn it_is_an_error() {
280 let error: Box<dyn std::error::Error> = Box::new(PlatformError::Unsupported);
281 assert!(error.source().is_none());
282 }
283}