Skip to main content

smudge_platform/
error.rs

1//! The error a window run returns.
2
3use std::error::Error;
4use std::fmt;
5
6use softbuffer::SoftBufferError;
7use winit::error::{EventLoopError, OsError};
8
9/// Something that stopped a window from opening, running or presenting a
10/// frame.
11#[derive(Debug)]
12pub enum PlatformError {
13    /// The event loop could not be created or run to completion.
14    EventLoop(EventLoopError),
15    /// The operating system refused to create the window.
16    Window(OsError),
17    /// The software presentation surface could not be created, resized or
18    /// presented.
19    Surface(SoftBufferError),
20    /// The renderer could not be set up for the window.
21    ///
22    /// This holds what went wrong: a value of the `SMUDGE_RENDERER`
23    /// environment variable that is not `cpu`, `gpu` or `auto`, a request
24    /// for the GPU renderer without the `gpu` feature, a request for the GPU
25    /// renderer with no usable adapter, or a window the GPU renderer cannot
26    /// present to.
27    Renderer(String),
28    /// The window options gave a clear color that is not opaque.
29    ///
30    /// Presentation copies only the color channels of the surface into the
31    /// window, so a clear color with any transparency cannot be shown.
32    ClearColor,
33}
34
35impl fmt::Display for PlatformError {
36    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            PlatformError::EventLoop(error) => write!(formatter, "the event loop failed: {error}"),
39            PlatformError::Window(error) => {
40                write!(formatter, "the window could not be created: {error}")
41            }
42            PlatformError::Surface(error) => {
43                write!(formatter, "the presentation surface failed: {error}")
44            }
45            PlatformError::Renderer(message) => {
46                write!(formatter, "the renderer could not be set up: {message}")
47            }
48            PlatformError::ClearColor => write!(
49                formatter,
50                "the clear color must be opaque, because presentation drops \
51                 the alpha channel of the surface"
52            ),
53        }
54    }
55}
56
57impl Error for PlatformError {
58    fn source(&self) -> Option<&(dyn Error + 'static)> {
59        match self {
60            PlatformError::EventLoop(error) => Some(error),
61            PlatformError::Window(error) => Some(error),
62            PlatformError::Surface(error) => Some(error),
63            PlatformError::Renderer(_) => None,
64            PlatformError::ClearColor => None,
65        }
66    }
67}