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
//! The error a window run returns.
use std::error::Error;
use std::fmt;
use softbuffer::SoftBufferError;
use winit::error::{EventLoopError, OsError};
/// Something that stopped a window from opening, running or presenting a
/// frame.
#[derive(Debug)]
pub enum PlatformError {
/// The event loop could not be created or run to completion.
EventLoop(EventLoopError),
/// The operating system refused to create the window.
Window(OsError),
/// The software presentation surface could not be created, resized or
/// presented.
Surface(SoftBufferError),
/// The renderer could not be set up for the window.
///
/// This holds what went wrong: a value of the `SMUDGE_RENDERER`
/// environment variable that is not `cpu`, `gpu` or `auto`, a request
/// for the GPU renderer without the `gpu` feature, a request for the GPU
/// renderer with no usable adapter, or a window the GPU renderer cannot
/// present to.
Renderer(String),
/// The window options gave a clear color that is not opaque.
///
/// Presentation copies only the color channels of the surface into the
/// window, so a clear color with any transparency cannot be shown.
ClearColor,
}
impl fmt::Display for PlatformError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PlatformError::EventLoop(error) => write!(formatter, "the event loop failed: {error}"),
PlatformError::Window(error) => {
write!(formatter, "the window could not be created: {error}")
}
PlatformError::Surface(error) => {
write!(formatter, "the presentation surface failed: {error}")
}
PlatformError::Renderer(message) => {
write!(formatter, "the renderer could not be set up: {message}")
}
PlatformError::ClearColor => write!(
formatter,
"the clear color must be opaque, because presentation drops \
the alpha channel of the surface"
),
}
}
}
impl Error for PlatformError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
PlatformError::EventLoop(error) => Some(error),
PlatformError::Window(error) => Some(error),
PlatformError::Surface(error) => Some(error),
PlatformError::Renderer(_) => None,
PlatformError::ClearColor => None,
}
}
}